请帮忙什么是逻辑错误...编译并运行代码并检查.. Y是否会出现意外错误 [英] Please help whats is the logical error ... Compile and run the code and check .. Y does it give unexpected error
本文介绍了请帮忙什么是逻辑错误...编译并运行代码并检查.. Y是否会出现意外错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的
#包括< stdio.h中>
#include< conio.h>
#include< stdlib.h>
#include< math.h>
int main()
{
int a = 0,b = 0;
int sum,diff,mult,R;
float div;
char c,ch;
do
{
printf(\ nn \ nn\\\\\\\\\\\\\\\\\\\\\\\
scanf(%d,& a); //取一个例如7
printf的值(\ n \\\\\\\\\\\\\\\\\\\\\\\\
scanf(%d,& b); //取b的值,例如4
printf(\\\\ _______________________________);
printf(\t |输入所需的操作员|);
printf(\t _______________________________);
printf(\\ \\ n \ n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\新闻* \ n \ n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\
\\\
);
scanf(%s,& c);
printf(\\\\\ n要执行的操作是'%c'按任何键继续,c);
getch();
if(c =='+'|| c ==' - '|| c =='*'|| c =='/'|| c =='%'|| c =='0')
{
printf(\ nn\ n | RESULTS |);
if(c ==' - ')
{
diff = a-b;
printf(\ n \ nDDFERFER OF%d AND%d IS:%d \ n \ n,a,b,diff); //这里它应该输出为7-4 = 3但它给出了7-0 = 7。为什么这么
getch();
}否则if(c =='+')
{
sum = a + b;
printf(\ n \\ n \\ n \\ n \\ n \\ n \\ n分配%d和%d IS:%d \ n \\ n,a,b,sum);
getch();
}否则if(c =='*')
{
mult = a * b;
printf(\ n \ n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
getch();
}否则if(c =='/')
{
div = a /(float)b;
printf(\ n \ n \\\\\\\,%d和%d IS:%f \ n \ n,a,b,div);
getch();
}否则if(c =='%')
{
R = a%b;
printf(\ n \\\\\\\\\\\\\\\\\\\\\\\\
getch();
}
}
else {
printf(\ n \ nn \ n \ tt \ t \ t无效运营商输入正确的运营商);
getch();
}
} while(c!='0');
返回0;
}
我的尝试:
i尝试在begning初始化值,即int a = 7,b = 4;
通过这样做它给出正确的东西但是当我们使用scanf从用户获取输入时它不会作品
解决方案
仔细阅读 scanf文档 。另请查看示例代码以了解字符串的使用。
您的c是字符而不是字符串。
所以正确的代码是:scanf( %C跨度>,和C);
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> int main() { int a=0,b=0; int sum,diff,mult,R; float div; char c,ch; do { printf("\n\n\tENTER THE FIRST operand "); scanf("%d",&a); //takes value for a for eg 7 printf("\n\n\tENTER THE SECOND operand "); scanf("%d",&b);//takes value for b for eg 4 printf("\t\n\n_______________________________"); printf("\t | ENTER THE DESIRED OPERATOR |"); printf("\t_______________________________"); printf("\n\n \t\tFOR SUMMATION PRESS + \n\n \t\tFOR DIFFERENCE PRESS - \n\n \t\tFOR PRODUCT PRESS * \n\n \t\tFOR DIVISION PRESS / \n\n \t\tTO FIND REMAINDER ONLY PRESS %% \n\n \t\tPRESS 0 TO EXIT\n\n"); scanf("%s",&c); printf("\t\n\n OPERATION TO BE PERFORMED IS '%c' PRESS ANY KEY TO CONTINUE",c); getch(); if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='0') { printf("\n\n |RESULTS|"); if(c=='-') { diff=a-b; printf("\n\nDIFFERENCE OF %d AND %d IS : %d \n\n",a,b,diff);// here it should give output as 7-4=3 but it gives 7-0=7 . why is it so getch(); } else if(c=='+') { sum=a+b; printf("\n\n\tSUMMATION OF %d AND %d IS : %d \n\n",a,b,sum); getch(); } else if(c=='*') { mult=a*b; printf("\n\n\tPORDUCT OF %d AND %d IS : %d \n\n",a,b,mult); getch(); } else if(c=='/') { div=a/(float)b; printf("\n\n\tDIVISION OF %d AND %d IS : %f \n\n",a,b,div); getch(); } else if(c=='%') { R=a%b; printf("\n\n\tREMAINDER WHEN %d IS DIVIDED BY %d IS : %d \n\n",a,b,R); getch(); } } else { printf("\n\n\n\t\t\t INVALID OPERATOR ENTER CORRECT OPERATOR "); getch(); } }while(c!= '0'); return 0; }
What I have tried:
i tried to initialise values at the begning i.e int a=7,b=4;
by doing this it gives correct thing but when we use scanf for getting input from user it doesnt works
解决方案
Read the scanf documentationcarefully. Take also a look on the example code to understand the string use.
Your c is a char not a string.
So correct code would be:scanf("%c",&c);
这篇关于请帮忙什么是逻辑错误...编译并运行代码并检查.. Y是否会出现意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文