+ 2
Can any one tells me why answer is not correct?
#include <iostream> using namespace std; int main() { int a; int b; int sum = a + b; cout << "Please enter a value \n"; cin >> a; cout << "Please enter b value \n"; cin >> b; cout << sum; return 0; }
4 odpowiedzi
+ 1
The expression
int sum=a+b; is invalid
+ 8
https://code.sololearn.com/cw2bcD6oQOXT/#cpp
^There you go.
It doesn't work because you declared sum as "int sum = a + b;" before you even received the input for a & b variables. So, instead you'll want to declare sum as "int sum;" and then AFTER you obtain the input values for a & b, assign "sum = a + b;" before you print output for sum.
Hope that helps.
int main()
{
int a;
int b;
int sum;
cout << "Please enter a value \n";
cin >> a;
cout << "Please enter b value \n";
cin >> b;
sum = a + b;
cout << sum;
return 0;
}
+ 1
your sum variable should after last cin
0
🙂🙂