+ 1

please tell me any mistake #include<iostream> int main() { int a,b; int sum = a+b; cout<<"value of a"<<endl; cin>>a; cout<<"value of b"<<endl; cin>>b; cout<< sum<<endl; return 0; } //if i simply write a+b in place of sum only then it works other wise it shows sum huge number

just look at the code

2nd Oct 2017, 3:08 PM
saurav
4 Antworten
+ 14
#include<iostream> using namespace std; int main() { int a,b; cout<<"value of a = "; cin>>a; cout<<"value of b = "; cin>>b; int sum = a + b; cout<< sum<<endl; }
2nd Oct 2017, 3:14 PM
Babak
Babak - avatar
+ 9
Common mistake for beginners. I remember doing this as well. When you declare sum, you initialized it with the sum of a and b. At this point in the program, the values of a and b are undefined, because your cin statements come after the assignment of a + b to sum. int a, b, sum; cin >> a; cin >> b; // at this point, values a and b are already provided by the user. sum = a + b;
2nd Oct 2017, 3:14 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Hahha you should to be fine in maths ;-)
5th Oct 2017, 12:47 AM
Firas Zrik
Firas Zrik - avatar
0
Iostream.h
9th Oct 2017, 12:58 PM
medha garg
medha garg - avatar