C语言的Swtich案例问题

问题描述 投票:0回答:1

我正在代码中的嵌套switch语句的帮助下制作一个微型项目ic 错误在代码的第15行中,此处%c不起作用,但% s正在运行,请帮助我解决此查询,并告诉我如何使用%c运行此代码,这是我的代码:-

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch;
char choice;
clrscr();
printf("1.Calculator\n2.Convrter\n\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
    case 1:printf("1.Addition(A)\n2.Subtraction(S)\n3.Multiplication(M)\n4.Division(D)\n5.Module(P)\n\n");
    printf("Enter your choice : ");
    **scanf("%c",&choice);**
    switch(choice)
    {
    case 'A':printf("Provide the value of a : ");
    scanf("%d",&a);
    printf("Provide the value of b : ");
    scanf("%d",&b);
    printf("\n");c=a+b;
    printf("%d",c);
    break;
    case 'S':printf("Provide the value of a : ");
    scanf("%d",&a);
    printf("Provide the value of b : ");
    scanf("%d",&b);
    printf("\n");c=a-b;
    printf("%d",c);
    break;
    case 'M':printf("Provide the value of a : ");
    scanf("%d",&a);
    printf("Provide the value of b : ");
    scanf("%d",&b);
    printf("\n");c=a*b;
    printf("%d",c);
    break;
    case 'D':printf("Provide the value of a : ");
    scanf("%d",&a);
    printf("Provide the value of b : ");
    scanf("%d",&b);
    printf("\n");c=a/b;
    printf("%d",c);
    break;
    case 'P':printf("Provide the value of a : ");
    scanf("%d",&a);
    printf("Provide the value of b : ");
    scanf("%d",&b);
    printf("\n");c=a%b;
    printf("%d",c);
    break;
    default:printf("Invalid Input");
    }
    break;
}
getch();
}
c switch-statement
1个回答
0
投票

您只需要在前面添加getchar();scanf()

因为当您选择\n作为字符选择时因此,一个getchar将解决您的问题

    case 1:printf("1.Addition(A)\n2.Subtraction(S)\n3.Multiplication(M)\n4.Division(D)\n5.Module(P)\n\n");
        printf("Enter your choice : ");
        getchar();
        scanf("%c", &choice); 
            switch (choice)
© www.soinside.com 2019 - 2024. All rights reserved.