0

Im new to C++, for some reason my first sum becomes a really big negative number while that part works perfect on its own. Help

#include<iostream> using namespace std; int main() { int myVariable = 10; int b; int c = 0; int sum = myVariable + b + c; cout << "10 + b = ? \nEnter a number for b: "; cin >> b; cout << "The sum is: " << sum << endl; if(sum>=100){ cout << "What a big number! " << sum << " !"; } while(sum<100){ cout << "try adding a number to get a sum greater then 99" << endl; cin >> c; cout << "the sum is now: " << sum << endl; return 0; } }

6th Jan 2018, 1:00 PM
Dylan Rongé
2 Antworten
+ 8
you must initialize a variable before using it, otherwise b is going to store some garbage
6th Jan 2018, 1:41 PM
Botol
Botol - avatar
+ 7
@sachin its not a problem, b is not initialized, so sum stores negative number(cuz b is -29348383949 smth like that)☺
6th Jan 2018, 1:53 PM
Botol
Botol - avatar