It doesn't add up

I'm running Vista and a Dev-C++ compiler.

My initial program runs through the compiler fine but it isn't calculating the answer correctly, I'm at a loss as to why. If I input something as simple as 1 for each question I get a 10 digit answer for the total.

#include <iostream>
#include <stdlib.h>

int main ()
{
using namespace std;
int PB, EB, CC;
int Total;
cout << "What is the phone bill for this month? ";
cin >> PB;
cout << "What is the electric bill for this month? ";
cin >> EB;
cout << "What is the Credit card bill for this month? ";
cin >> CC;
Total = PB+EB+CC;
cout << "Total: " << Total << endl;
system("pause");
return 0;
}


Thanks ahead for any replies.
Last edited on
Well, you're performing the calculation before you even get the data from the user, so there's no way C++ could have the correct values; you're probably getting garbage values because the variables have no initial values when you add them up.
Thanks for the reply. I moved "Total = PB+EB+CC;" to the line following the last question input. I'm still getting a garbage total.
also make sure "using namespace std;" is a declaration outside of class functions.

eg:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <stdlib.h>
using namespace std;

int main ()
{
    int PB, EB, CC;
    ....
        ....
    ....
} 
Last edited on
I figured it out.

I changed this:
Total = PB+EB+CC

to this:
Total = PB + EB + CC

and it works fine now.
What the... how? The compiler ignores whitespaces...
WTF? How did that fix it?
My guess is that OP forgot to recompile.
Topic archived. No new replies allowed.