#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
float a, b, c;
clrscr();
printf("enter value of a");
scanf ("%f", &a);
printf("enter value if b");
scanf("%f", &b);
printf(" a for addition");
printf("b for sub");
scanf("%c", &ch);
switch (ch)
{
case 'a':
c = a + b;
printf("addition=%f", c);
break ;
case 'b':
c = a - b;
printf("\nsubtraction=%f", c);
break;
default :
printf ("wrong choice");
}
getch();
}
这是我得到的结果: 输入a4的值 如果 b4 输入值 a 表示附加 b 表示次错误选择
问题是开关条件甚至没有被评估一次。 编译:Windows 上的 Turbo C
最佳答案
刷新输入缓冲区以消耗缓冲区中剩余的 \n
。
或者试试这个
scanf(" %c",&ch);
^
| A space before %c can skip any number of whitespaces
关于c++ - Switch case 在 Turbo C 编译器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24043572/