C++ code not calculating correctly

Fandango21

Junior Member
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.
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;

}
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?
 

BigDH01

Golden Member
Jul 8, 2005
1,630
82
91
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.
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;

}
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?

Have you tried putting this:

int area = length * width;

in the line above where you cout your answers, but after where you set your variables? Also, set your constant equal to 8 and calculate cost after you do as I suggested.
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
It's cute that you described area declaratively to be "length * width" ahead of time. In some programming languages, it wouldn't matter what order you declared things in. But in C++ it does. You are not supposed to use a variable before giving it the value that you want it to have. If you use it without giving it a value, then it may have any arbitrary value (such as 145040594).

What you need to do in this case to move the assignment of "area" and "totalCost" variables to a program point that comes after you read in numbers for "length" and "width".
 

Fandango21

Junior Member
Feb 8, 2011
23
0
0
Awesome! You guys rock! Here's what I did below and it works fine now. I kinda guessed I had something out of order b/c it compiled but bad math. Thanks alot for the quick response and help.

Code:
 #include <iostream>

using namespace std;

int main () {

        int length;
        int width;

        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;

        int area = length * width;

        cout << "The total area of the room is " << area <<
                " square feet" << endl;

        const int cost = 8;

        cout << "It will cost $" << cost * area << " to buy carpet for the whole room" << endl;

}
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
When you declare a variable, it assigns a memory location to it. So if you use it without assigning it something first, you get whatever garbage is in that location.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |