user-0 提问: 如下代码，如果输入字符，为什么会造成死循环？

回复:
user: user-1
content: 引用 6 楼 sheao11 的回复:我已经解决了，代码如下： /* Balances a checkbook */  #include <stdio.h>  int main(void) {     char cmd;     float balance = 0.0f, credit, debit;      printf("*** ACME checkbook-balancing program ***\n");     printf("Commands: a=clear, b=credit, c=debit, ");     printf("d=balance, e=exit\n\n");      printf("Enter command: ");     for (;;) {         cmd = getchar();         if (cmd == '\n') {             printf("Enter command: ");             continue;         } else {          }           switch (cmd) {             case 'a':                 balance = 0.0f;                 break;             case 'b':                 printf("Enter amount of credit: ");                 scanf("%f", &credit);                 balance += credit;                 break;             case  'c':                 printf("Enter amount of debit: ");                 scanf("%f", &debit);                 balance -= debit;                 break;             case 'd':                 printf("Current balance: $%.2Lf\n",balance);                 break;             case 'e':                 return 0;             default:                 printf("Commands: a=clear, b=credit, c=debit, ");                 printf("d=balance, e=exit\n\n");                 break;         }     } }  关键就是会有个\n符，解决了这个问题就行

user: user-0
content: 我已经解决了，代码如下： /* Balances a checkbook */  #include <stdio.h>  int main(void) {     char cmd;     float balance = 0.0f, credit, debit;      printf("*** ACME checkbook-balancing program ***\n");     printf("Commands: a=clear, b=credit, c=debit, ");     printf("d=balance, e=exit\n\n");      printf("Enter command: ");     for (;;) {         cmd = getchar();         if (cmd == '\n') {             printf("Enter command: ");             continue;         } else {          }           switch (cmd) {             case 'a':                 balance = 0.0f;                 break;             case 'b':                 printf("Enter amount of credit: ");                 scanf("%f", &credit);                 balance += credit;                 break;             case  'c':                 printf("Enter amount of debit: ");                 scanf("%f", &debit);                 balance -= debit;                 break;             case 'd':                 printf("Current balance: $%.2Lf\n",balance);                 break;             case 'e':                 return 0;             default:                 printf("Commands: a=clear, b=credit, c=debit, ");                 printf("d=balance, e=exit\n\n");                 break;         }     } }

user: user-2
content: case 'e':                  return 0;              default:                  printf("Commands: a=clear, b=credit, c=debit, ");                  printf("d=balance, e=exit\n\n");                  break; 你这也没break啊肯定会一直循环的，要出来for循环再return0：

user: user-1
content: 原因是比如你输入a，但是实际接收到了a 和 回车的\n，于是循环时先执行了case 'a':，第二轮循环缓冲区还有\n，于是执行了default  可以在cmd = getchar();后面再加一个getchar();把\n给接收了

user: user-0
content: 上面代码有误，本来switch语句是接收int型的，有死循环；有人建议改成接收字符，上面没改完全，现在改完全了，但是会重复输出，输出如下 *** ACME checkbook-balancing program *** Commands: a=clear, b=credit, c=debit, d=balance, e=exit  Enter command: b Enter amount of credit: 10000 Enter command: Commands: a=clear, b=credit, c=debit, d=balance, e=exit  Enter command: d Current balance: $10000.00 Enter command: Commands: a=clear, b=credit, c=debit, d=balance, e=exit  Enter command: 0 Commands: a=clear, b=credit, c=debit, d=balance, e=exit  Enter command: Commands: a=clear, b=credit, c=debit, d=balance, e=exit  Enter command:   代码如下： /* Balances a checkbook */  #include <stdio.h>  int main(void) {     char cmd;     float balance = 0.0f, credit, debit;      printf("*** ACME checkbook-balancing program ***\n");     printf("Commands: a=clear, b=credit, c=debit, ");     printf("d=balance, e=exit\n\n");      for (;;) {         printf("Enter command: ");         cmd = getchar();          switch (cmd) {             case 'a':                 balance = 0.0f;                 break;             case 'b':                 printf("Enter amount of credit: ");                 scanf("%f", &credit);                 balance += credit;                 break;             case  'c':                 printf("Enter amount of debit: ");                 scanf("%f", &debit);                 balance -= debit;                 break;             case 'd':                 printf("Current balance: $%.2Lf\n",balance);                 break;             case 'e':                 return 0;             default:                 printf("Commands: a=clear, b=credit, c=debit, ");                 printf("d=balance, e=exit\n\n");                 break;         }     } }

user: user-2
content: for (;;) {          printf("Enter command: ");          i = scanf("%d", &cmd);          if (i == 0) {              printf("Input error!\n");              continue;          } continue 不对，应该是break

user: user-0
content: 哦，源代码应该是： /* Balances a checkbook */  #include <stdio.h>  int main(void) {     int cmd, i;     double balance = 0.0, credit, debit;      printf("*** ACME checkbook-balancing program ***\n");     printf("Commands: 0=clear, 1=credit, 2=debit, ");     printf("3=balance, 4=exit\n\n");      for (;;) {         printf("Enter command: ");         i = scanf("%d", &cmd);         if (i == 0) {             printf("Input error!\n");             continue;         }          switch (cmd) {             case 'a':                 balance = 0.0L;                 break;             case 'b':                 printf("Enter amount of credit: ");                 scanf("%f", &credit);                 balance += credit;                 break;             case  'c':                 printf("Enter amount of debit: ");                 scanf("%f", &debit);                 balance -= debit;                 break;             case 'd':                 printf("Current balance: $%.2Lf\n",balance);                 break;             case 'e':                 return 0;             default:                 printf("Commands: 0=clear, 1=credit, 2=debit, ");                 printf("3=balance, 4=exit\n\n");                 break;         }     } }