发现它没有给出输出 [英] Findd y it doesnt give output

查看:82
本文介绍了发现它没有给出输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#包括< stdio.h中> 
#include< conio.h>
#include< stdlib.h>
#include< math.h>

main()
{

int a = 0,b = 0;
int sum;
char c;




printf(\ n \\\ nnn,第一个操作数);
scanf(%d,& a);
printf(\ n \\\\\\\\\\\\\\\\\\\\
scanf(%d,& b);

printf(\ nn \ n \\\\\\\\\\\\\\\\\\\\\\\\

scanf(%s,& c);

if(c =='+')
{


printf(\ nnanth | RESULTS |);




sum = a + b;
printf(\ n \\ n \\ n \\ n \\ n \\ n \\ n分配%d和%d IS:%d \ n \\ n,a,b,sum);

}
else {

printf(\ n \ nn \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ );



}
}





我是什么已经尝试过:



试图通过itlf将第二个变量放到0

解决方案

< blockquote>可能是这导致了这个问题:

 char c; 
...
scanf(%s,& c);



当您使用格式%s调用scanf时,它期望 - 并且取出 - 一个以空字符结尾的字符串,而不是单个字符。因为你只为字符串分配一个字符,它会溢出你给它的空间,实际上接下来发生的事情是在编译器编写器的一圈 - 这是一个非法的操作,因此没有定义效果。 />


将c的定义更改为字符数组:

  char   operator  [ 100 ]; 

并读取一个字符串来自用户:

 scanf( %s  operator ); 

然后检查用户输入的第一个字符:

  if  operator  [ 0 ] == '  +'

它应该更好。


%s格式说明符带有 char 变量是一个错误。

试试,例如

  #include   <   stdio.h  >  
#include < stdlib.h >

int main()
{
int a = 0 ,b = 0 ;
int sum;

printf( \ nn \ nn\\\\\\\\\\\\\\\跨度>);
scanf( %d,& a);
printf( \ n \ nn \ tnTERTER SECOND operand);
scanf( %d,& b);

printf( \ n \ n \\\\\\\\\\\\\\\\\\\\\\ );

while (getchar()!= ' +'
{
printf( \ n无效操作员输入正确的操作员); \\ n \\ n \\ n \\ n \\ n \\\\\
}
printf( \ n\ n | RESULTS | );
sum = a + b;
printf( \ n \ n \\\\\ \\\
\\\
,A,b,和);
return 0 ;
}


您已在请帮忙什么是逻辑错误...编译并运行代码并检查.. Y是否会产生意外错误 [ ^ ],并收到一些建议。请不要重新发布相同的问题。


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

 main()
{

	int a=0,b=0;
	int sum;
	char c;


	

	printf("\n\n\tENTER THE FIRST operand   ");
	scanf("%d",&a);
	printf("\n\n\tENTER THE SECOND operand   ");
	scanf("%d",&b);
	
	printf("\n\n \t\tFOR SUMMATION PRESS +  ");

	scanf("%s",&c);
	
 if(c=='+')
{


	printf("\n\n |RESULTS|");

	
	
	
	sum=a+b;
	printf("\n\n\tSUMMATION OF %d AND %d IS : %d \n\n",a,b,sum);

}
else {

	printf("\n\n\n\t\t\t  INVALID OPERATOR ENTER CORRECT OPERATOR ");



}
}



What I have tried:

tried to assighn values bt puts second variable to 0 by itslf

解决方案

Probably it's this that causes the problem:

char c;
...
scanf("%s",&c);


When you call scanf with a format of "%s", it expects - and fetches - a null terminated string, not a single character. Since you only allocate a single character for the string, it will overflow the space you gave it, and in practice what happens next is in the lap of the compiler writer - it's an illegal operation and so the effects aren't defined.

Change the definition of c to an array of characters:

char operator[100];

And read a string from the user:

scanf("%s",operator);

Then check the first character the user typed:

if (operator[0] == '+')

It should work better.


Quote:

scanf("%s",&c);

using the "%s" format specifier with a char variable is an error.
Try, for instance

#include<stdio.h>
#include<stdlib.h>

int  main()
{
  int a=0,b=0;
  int sum;

  printf("\n\n\tENTER THE FIRST operand   ");
  scanf("%d", &a);
  printf("\n\n\tENTER THE SECOND operand   ");
  scanf("%d",&b);

  printf("\n\n \t\tFOR SUMMATION PRESS +  ");

  while ( getchar() != '+')
  {
    printf("\n\n\n\t\t\t  INVALID OPERATOR ENTER CORRECT OPERATOR ");
  }
  printf("\n\n |RESULTS|");
  sum=a+b;
  printf("\n\n\tSUMMATION OF %d AND %d IS : %d \n\n",a,b,sum);
  return 0;
}


You already posted this at Please help whats is the logical error ... Compile and run the code and check .. Y does it give unexpected error[^], and received some suggestions. Please do not repost the same question.


这篇关于发现它没有给出输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆