我正在尝试做一个简单的程序,我将首先给出操作名称,第二个将给出两个数字。根据操作名称,它将显示结果。 它将在无限循环内完成。
但是我的问题是,代码正在进行第一次迭代,从第二次起,它无法正常工作。 gets()没有接受输入。
我已经把这里的消息来源告诉了我所犯的错误。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char command[80];
float i,j;
for( ; ; )
{
printf("what operation you want???\n");
gets(command);
if( !strcmp(command,"quit") )
{
break;
}
printf("enter first number :\n");
scanf("%f",&i);
printf("enter second number :\n");
scanf("%f",&j);
if(!strcmp(command,"add"))
printf("%f\n",i+j);
else if(!strcmp(command,"divide"))
printf("%f\n",i/j);
else if(!strcmp(command,"multiply"))
printf("%f\n",i*j);
else if(!strcmp(command,"subtract"))
printf("%f\n",i-j);
else printf("unknown command\n");
}
return 0;
}