0

c++ code help? (super simple)

Could someone please explain me why this code doesn't give me the sum between a and b? #include <iostream> using namespace std; int main() { int a; int b = 2; int sum = a + b; cout << "Please enter a number \n"; cin >> a; cout << sum; return 0; }

23rd Jun 2017, 10:29 AM
Caterina Autuori
Caterina Autuori - avatar
3 Answers
+ 2
#include <iostream> using namespace std; int main() { int a; int b = 2; cout << "Please enter a number \n"; cin >> a; int sum = a + b; cout << sum; return 0; } sum will perform only after the values assigned for it ...so first initialize the variables or else get the input value before before use the variable after that ..perform the arithmetic operation
23rd Jun 2017, 11:06 AM
gobiga rani
gobiga rani - avatar
+ 15
put sum var declaration after cin>>a
23rd Jun 2017, 10:31 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
Try to ask for input first after declaring the var
23rd Jun 2017, 11:14 AM
Siddharth Saraf