+ 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
4 Réponses
+ 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;
}
+ 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;
+ 1
Hahha you should to be fine in maths ;-)
0
Iostream.h