切换案例在turbo c编译器中不起作用

时间:2014-06-04 17:12:43

标签: c++ c

#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 for additionb for subwrong choice

问题是切换条件甚至没有评估过一次.. 编译:windows上的turbo c

1 个答案:

答案 0 :(得分:3)

刷新输入缓冲区以消耗缓冲区中的\n 或试试这个

 scanf(" %c",&ch);  
        ^
        | A space before %c can skip any number of whitespaces