c - 循环在我的 C 代码中不起作用

标签 c loops if-statement break

下面是我的 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”,该循环也会继续循环。我在这段代码中做错了什么?

最佳答案

试试这个:

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() 语句,看看会发生什么。

关于c - 循环在我的 C 代码中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42892957/

相关文章:

python - 如何避免在Python中嵌套“for循环”?

c - While 循环条件不满足

Python 问题 : Try and Excepts with if

javascript - 如何在 javascript 中创建一个 if 语句,该语句将在变量未定义时运行?

c++ - C 预处理器和操作顺序

c - 《C 编程语言》(版本 2)中的示例 1-7

jquery - 创建一个数组并循环显示每个 HTML

C2065 我所有的 for 循环都出现错误

C Lib 设计 - 结构和内存管理 [最佳实践]

C#循环并以编程方式获取表单中所有datetimepicker的值