我正在代码中的嵌套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();
}
您只需要在前面添加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)