Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
the following code is given here:
C++
#include<iostream.h>
#include<conio.h>
void main()
{
   int number1,num2,res;
   clrscr();
   cout<<"enter num1";
   cin>>number1;
   cout<<"enter num2";
   cin>>num2;
   cout<<"sum is:"<<res;
   getch();
   }
When I entered the 2 values of number1,num1=2,4
this display the result = 1562.
Posted
Updated 17-Aug-13 7:24am
v2

Where are you actually doing the sum?

Because you declare the variables, but don't initialize them, they hold random data (whatever was left in those addresses before you acquired them).

You need to add them somewhere, res = number1 + num2;

Please get into the habit of initializing your variables when you declare them. This problem would be a lot easier (but it was easy to begin with) if you initialized them.

You may want to learn good debugging skills, learn to run through code by hand. A simple following of the code written out on paper would have immediately shown you where the problem was.
 
Share this answer
 
v2
If you set the value of your res variable nowhere then it will be filled up with garbage, a random value.
 
Share this answer
 
Comments
H.Brydon 18-Aug-13 23:02pm    
Probably the easiest +5 you've ever received... :-)
pasztorpisti 19-Aug-13 3:44am    
Thank you! To be honest I feel ashamed of answering this question, unfortunately I was quite bored... :-)
res = number1 + num2 //Missing
Garbage value is stored in uninitialized variables.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900