How to continually add user defined amount of numbers?

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;

return 0;
}

int CalcArea( double areaLength, double areaWidth)
{
double tempArea;
double sumArea;

tempArea=0;

sumArea = tempArea+(areaLength * areaWidth);
tempArea = sumArea;

return tempArea;
}

Hello!

Consider this little change:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.
No problem!

To have the += in the other function you mentioned, this might also work. But only if the variable holding the sum is static.

static double sumArea = 0.0;

sumArea += (areaLength * areaWidth);

return sumArea;

Something to that effect. If you set a breakpoint on this return statement, you should see how it accumulates and returns that sum.

A suggestion, if you happen to read this post is, that you might also want to consider using double as return type from the calcArea function.

double CalcArea( double areaLength, double areaWidth)
{
....
}

The other function can be void as well, there is nothing to be returned to the caller from this function. Meaning:

void CalcSquareFeet( int numberRooms)
{
....
no return statement here.
}
Last edited on
Topic archived. No new replies allowed.