Finding input variables errors – Computer Sir Ki Class
  

Login
[lwa]



Solved Problem! #CPP#3190 siteicon   siteicon   siteicon  

Problem Statement - Finding input variables errors

Find error in the following program and suggest corrections. Assume that includes and namespace is already given.

TC++ #3190

int main()
{
int x;
cout<<"Input two integers: ";
cin>>x>>y;
y=x+y;
cout<<"Sum of given numbers is "<<x<<endl;
return 0;
}

Test it !

Solution


int x;    //Variable y must also be declared here.
Corrected: int x,y;

cout<<“Input two integers: “;           //OK
cin>>x>>y;                                          //OK
y=x+y;                                                   //OK
cout<<“Sum of given numbers is “<<x<<endl; //Will Print x, but as per program logic it should be y
Corrected:  cout<<“Sum of given numbers is “<<y<<endl;

 

Notes

  • As given in this example, in some cases some errors are of type of incorrect interpretation of logic and such errors can not be defined under syntax error, semantics error or run-time error, Still for correct results they must be attended. For these kind of errors compiler not even give warnings.


Share

sunmitra| Created: 30-Jan-2018 | Updated: 30-Jan-2018|






Back