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; }
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
+ 15
put sum var declaration after cin>>a
+ 4
Try to ask for input first after declaring the var



