【问题标题】:loop not working in my c code循环在我的 c 代码中不起作用
【发布时间】:2017-03-19 22:15:11
【问题描述】:

下面是我的 c 代码,for(;;) 循环不会根据我使用 if 语句给出的条件而中断。有什么做错了吗?我的代码如下:

 #include <stdio.h>
 main()
{
/*
 * Program starts
 */

 int       tracks;                             /* tracks is declared as a variable for the number of tracks */
 float     price;                              /* Price is declared as variable for number of tracks */
 char      title[100];                         /* title is declared as a varibel for the title of thr CD*/
 char      album_single[2];                    /* album_single is a variable declared  for either the CD is a single or an album */
 char      artiste[50];


 printf("Welcome to the CD database\n\n");
 printf("Please enter the details of the CD below...\n");

/*
 * First, title
 */
 printf("Title? ");
 scanf("%[^\n]", title);

/*
 * Next, Artiste
 */
 printf("Artiste? ");
 fflush(stdin);
 scanf("%[^\n]", artiste);

/*
 * Next, number of tracks
 */
 printf("Number of Tracks? ");
 fflush(stdin);
 scanf("%d",&tracks);

/*
 * Next, Type(album or single)
 */

 for(; ;)
 {
     printf("ALbum or a single (Enter 'a' for an album and 's' for a single): ");
     fflush(stdin);
     scanf("%c", &album_single);

     if(album_single == "a" || album_single == "s")
          break;

     printf("Error!\n");
 }


/*
 * Conditions to assign the right type(album/single) to the variable album_single
 */
 if(strcmp(album_single, "a") == 0)
 {

      strcpy(album_single,"Album");
 }
  else
  {
     if(strcmp(album_single, "s") == 0)
         strcpy(album_single, "single");
  }

/*
 * Finally, Price
 */
 printf("Retail Price(e.g $4.66)? ");
 fflush(stdin);
 scanf("%f", &price);

/*
 * Details, finallly output
 */
 printf("\n\nDetails of  %s's CD\n", title);
 printf("========================\n");
 printf("Title: %s\n",title);
 printf("Artiste: %s\n", artiste);
 printf("Number of tracks: %d\n",tracks);
 printf("Album/Single: %s\n", album_single);
 printf("Price:$ %.2f\n", price);
 printf("========================\n");

/*
 * User Friendly exit of the program
 */
 printf("\n Press ENTER to exit the program.");

/* 
 * Program end
 */
 fflush(stdin);
 getchar();
}

下面是 for(;;) 循环中没有中断的部分:

 for(; ;)
 {
     printf("ALbum or a single (Enter 'a' for an album and 's' for a single): ");
     fflush(stdin);
     scanf("%c", &album_single);

     if(album_single == "a" || album_single == "s")
          break;

     printf("Error!\n");
 }

即使输入是'a'或's',这个循环也会继续循环。我在这段代码中做错了什么?

【问题讨论】:

  • album_single 是一个 char 数组,而你的 scanf 并检查假设它是一个 char - 你有 未定义的行为 那里(因为读入scanf)
  • 启用编译器警告。
  • main() 是一个已弃用的签名(自 ca. 28 年以来,自 1999 年以来它已成为过时的功能)。使用标准的int main(void)
  • fflush(stdin) 调用未定义的行为。这不是您在 C 中比较字符串的方式。
  • 并且,不要尝试使用 == 运算符比较字符串。那一个只比较字符串的地址。您需要使用 strcmp() 或类似名称。

标签: c loops if-statement break


【解决方案1】:

试试这个:

char      album_single; 

while (album_single != 'a' && album_single != 's')
{
     printf("Album or a single (Enter 'a' for an album and 's' for a single): ");
     scanf("%c", &album_single);
     scanf("%c"); // discard carriage return
}

实验注释掉最后一个 scanf() 语句,看看会发生什么。

【讨论】:

  • fflush(stdin) 是标准 C 中未定义的行为。
  • @DavidBowling 很公平,我删除了电话。然而,它确实在 gcc 上编译和执行。
  • @Nav——它在标准中明确表示为 UB,但这并不能阻止单个实现定义行为。您可能会仔细检查 fflush(stdin) 是否与 GCC 一样正常工作;尽管 Linux 手册页建议定义了 fflush(stdin),但在我测试它时它从未起作用,并且有一个论点是当 stdin 是终端 in this excellent discussion of the issue 时这可能不起作用。尽管如此,它肯定是不可移植的。
【解决方案2】:

此代码在比较中使用strcmp() 函数而不是== 解决了问题。

正确的代码 if(strcmp(album_single, "a") == 0 || strcmp(album_single, "s") == 0 )

错误的代码 if(album_single == "a" || album_single == "s")

感谢你们的贡献。!!

【讨论】:

    【解决方案3】:

    您不能使用 == 运算符比较字符串。要么将 Album_single 声明为字符,要么使用 strcmp() 函数。

    if(strcmp(album_single, "a")==0||strcmp(album_single, "s")==0) break;
    

    记得包含正确的头文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2022-12-10
      • 2021-05-04
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多