我试图让scanf以数字的形式接收来自用户的输入然后使用我要求的两个数字来执行一些简单的数学我遇到的问题是无论我为数字输入什么他们总是吐出1任何帮助将不胜感激
int main(int argc, char const *argv[])
{
float in1,in2,value;
char c;
printf("Enter the operation of your choice:\n");
printf("A. add S. subtract\n");
printf("M. multiply D. divide\n");
printf("Q. quit\n");
c=getchar();
while( c !='Q' && c!='q')
{
printf("please enter your first number:");
scanf("%g", &in1);
printf("%g\n",in1 );
printf("please enter your second number:");
scanf("%g", &in2);
printf("%g\n",in2 );
if(c == 'S' || c =='s')
if(in2 == 0)
{
printf("you have selected subtration however\n");
printf("the value you entered was 0\n");
printf("please enter a value greater than 0\n");
scanf("%lg",&in2);
}
if(c == 'a' || c == 'A')
{
value = in1+in2;
printf("%lg + %lg = %lg\n",in1,in2,value );
}
if(c == 's' || c == 'S')
{
value = in1-in2;
printf("%lg - %lg = %lg\n",in1,in2,value );
}
if(c == 'm' || c == 'M')
{
value = in1*in2;
printf("%lg * %lg = %lg\n",in1,in2,value );
}
if(c == 'd' || c == 'D')
{
value = in1/in2;
printf("%lg / %lg = %lg\n",in1,in2,value );
}
if (c !='Q' && c !='q')
{
printf("Enter the operation of your choice:\n");
printf("A. add S. subtract\n");
printf("M. multiply D. divide\n");
printf("Q. quit\n");
}
c=getchar();
}
return 0;
}
答案 0 :(得分:0)
您能否将变量定义为double
而不是float
?另外,我觉得在while循环结束时你还需要一个getchar()
。当您第一次输入第二个数字时,输入键将作为c = getchar()
的一部分;要再次阅读该选项,您必须添加另一个c = getchar()
。