简单加法问题
#include<stdio.h>
void main()
{
int a,b;
char op;
printf("输入a,b\n");
scanf("%d,%d",&a,&b);
printf("asd");
while((op=getchar())!='\n')
switch (op)
{
case '+' :printf("%d",a+b);
break;
default:
break;
}
}
为什么不能输入运算符号?
[解决办法]
因为第一个scanf后必然有个回车,getchar将读到该回车,所以while语句直接结束。
[解决办法]
- C/C++ code
#include<stdio.h>int main(){ int a,b; char op; printf("输入a,b\n"); scanf("%d,%d",&a,&b); printf("asd"); getchar(); while((op=getchar())!='\n') switch (op) { case '+' :printf("%d",a+b); break; default: break; }}
[解决办法]
- C/C++ code
#include<stdio.h>void main(){int a,b;char op; printf("输入a,b\n");scanf("%d,%d",&a,&b);printf("asd");getchar(); //此处添加一行,即可while((op=getchar())!='\n')switch (op){case '+' :printf("%d",a+b);break;default:break;}}
[解决办法]
那个回车,狠迷人
[解决办法]
写如下的几行代码测试下,你就明白了!
- C/C++ code
#include <stdio.h>#define N 10int mian(){char buffer[N];scanf("%s", buffer);printf("%d", getchar()); // 输出为10, 即'\n', 所以, 3#的方案是可行的