I am writing a program that would have the user input the amount of rooms in their home along with their measurements (Length * Width). The program would then calculate the area of each room and add them together before finally displaying the total area at the end. My issue is that the area is not being continually added together and only giving me the value of the last area. Thank you! Note: I have to keep it in the format where the length and width inputs as well as the area calculations are in different functions.
#include <iostream>
using namespace std;
int CalcSquareFeet( int numberRooms);
int CalcArea( double areaLength, double areaWidth);
int main()
{
int numberRooms;
cout << "How many rooms? ";
cin >> numberRooms;
CalcSquareFeet(numberRooms);
return 0;
}
int CalcSquareFeet( int numberRooms)
{
int counter;
double area;
counter = 1;
do
{
double length;
double width;
cout << "Room #" << counter << ":\n";
cout << " Enter length: ";
cin >> length;
cout << " Enter width: ";
cin >> width;
area = CalcArea(length, width);
counter = counter + 1;
}while(counter <= numberRooms);
cout << "The total square feet is " << area <<endl;
int CalcSquareFeet( int numberRooms)
{
// It is good practice to initialize your variables with a value
int counter = 0;
double area = 0.0;
counter = 1;
do
{
double length = 0.0;
double width = 0.0;
cout << "Room #" << counter << ":\n";
cout << " Enter length: ";
cin >> length;
cout << " Enter width: ";
cin >> width;
area += CalcArea(length, width);
counter = counter + 1;
}while(counter <= numberRooms);
}
Now, your area variable accumulates the sum and in the end outputs the area of all rooms.
Also, you should surround code with [-code-] [-/code-] tags. This makes it easier to read your code. Leave out the - (see an example when you open up a new topic, in each new topic there is these tags, inside which the code goes.) :)
Thank you so much. I was looking around and saw similar examples with that + added and tried putting it in the last function where area is calculated, but did it did not work. Makes much more sense for it be there as the function continually receives inputs from the second function.