+ 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; }

22nd Sep 2017, 2:47 PM
Umair Rasool
Umair Rasool - avatar
4 Réponses
+ 1
The expression int sum=a+b; is invalid
23rd Sep 2017, 5:22 AM
Shivneep Brar
Shivneep Brar - avatar
+ 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; }
22nd Sep 2017, 2:56 PM
AgentSmith
+ 1
your sum variable should after last cin
22nd Sep 2017, 6:12 PM
Shantnu Chaubey
Shantnu Chaubey - avatar
0
🙂🙂
22nd Sep 2017, 4:37 PM
Riya An
Riya An - avatar