- Feb 8, 2011
- 23
- 0
- 0
I'm really new to code in general (this is my first programming class) and I can't figure out why the code I wrote wont calculate correctly. I'll put it in the code tags below and explain what is working underneath.
The problem I have is when i run the code, for example I put in 10 for length and 10 for width, the total area gives me an number like 145040594 and the cost ends up being something like 12091248. This is totally not like the int variables I set them to be. If i don't set an area variable or totalCost variable and just manually put in length * width variables instead of area and length * width * 8 instead of totalCost, it works just fine. I have to have a constant totalCost so I cant just skip variables all together. Can someone tell me what I'm doing wrong?
Code:
#include <iostream>
using namespace std;
int main () {
int length;
int width;
int area = length * width;
const int totalCost = area * 8;
cout << "What is the length of the room (in feet)? ";
cin >> length;
cout << endl;
cout << "What is the width of the room (in feet)? ";
cin >> width;
cout << endl;
cout << "The total area of the room is " << area <<
" square feet" << endl;
cout << "It will cost $" << totalCost << " to buy carpet for the whole room" << endl;
}