gpt4 book ai didi

c - 为什么这个 C 程序不能正常执行?

转载 作者:太空宇宙 更新时间:2023-11-03 23:43:08 24 4
gpt4 key购买 nike

#include<stdio.h>
int main()
{
int x,y;
unsigned char operator;
printf("Enter two numbers and one operater \n");
scanf("%d %d %c ",&x,&y,&operator); // check " and commas
printf("Output ");
switch(operator)
{
case '+':printf("%d",x+y); // dont jst copy paste read and paste
break;

case '-':printf("%d",x-y);
break;

case '*':printf("%d",x*y);
break;

case '/':printf("%d",x/y);
break;

default: printf("invalid operator");

}
return 0;
}

执行后,我输入了两个数字 5 、 10 和一个运算符 + 。但我没有得到任何输出。但在我输入任何其他值后,我得到了输出。示例:

Enter two numbers and one operater 
40 20
+
10
Output 60

最佳答案

它的发生是因为你的 scanf("%d %d %c ",&x,&y,&operator); 语句,只需删除 %c 之后的空格。将其用作 scanf("%d %d %c",&x,&y,&operator);

Resone就是我们在scanf方法中添加空格的时候去继续扫描。

关于c - 为什么这个 C 程序不能正常执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40421440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com