开关情况不工作在turbo c编译器

Switch case not working in turbo c compiler

本文关键字:turbo 编译器 工作 情况 开关      更新时间:2023-10-16
#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,请输入valuea表示加法b表示子错误选择

问题是连一次开关条件都没有求值。

刷新您的输入缓冲区以消耗缓冲区中剩余的n
或者试试

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