For loop will not accumulate correctly

I'm trying to design a for loop that makes a calculation, stores it to a value, and then does the same calculation again given a set number of times. It's for a painting program, where you first insert the number of rooms to be painted. Let's say I enter 3, so the program will generate 3 rooms. Then, you enter the area for each room. It's 8 hours of labor for every 115 feet, so the formula is hoursOfLabor = area / 115 * 8. I need the loop to store the total hours for room 1, then room 2, then room 3 = then add them together to give me the total hours worked for all three rooms. This seems like a fairly novice task, but I can't wrap my head around it. I have my attempted code below. I know I need to use two nested for loops, but it only seems to calculate the area for the last room entered.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double hoursOfLabor;
int numOfRooms; // stores nuumber of rooms
double area; // area of room


cout << "enter number of rooms; ";
cin >> numOfRooms;

//loops number the number rooms entered
for (int i = 0; i < numOfRooms; i++)
{
cout << "area of room # ";
cin >> area;
cout.setf(ios::fixed);
// I thought I could try the for loop below,
// but this doesn't seem to work.
for (int n = 0; n < numOfRooms; n++) //
{
hoursOfLabor = area / 115 * 8;
}

}

cout << setprecision(2) << hoursOfLabor;

}
closed account (48T7M4Gy)
Please use code tags < > so we can run your program and give you some feedback
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
27
28
29
30
31
32
33
34
  

#include <iostream> 
#include <iomanip>
using namespace std;

int main()
{
double hoursOfLabor;
int numOfRooms; // stores nuumber of rooms 
double area; // area of room


cout << "enter number of rooms; ";
cin >> numOfRooms;

//loops number the number rooms entered 
for (int i = 0; i < numOfRooms; i++)
{
cout << "area of room # ";
cin >> area;
cout.setf(ios::fixed); 
// I thought I could try the for loop below, 
// but this doesn't seem to work. 
for (int n = 0; n < numOfRooms; n++) //
{
hoursOfLabor = area / 115 * 8; 
}

}

cout << setprecision(2) << hoursOfLabor; 

}



Hope that helps
closed account (48T7M4Gy)
It sure does help. Now somebody can run it by pressing the gear wheel and see what is going on. I can't at the moment so you'll have to stand by but as a start I would delete lines 22 to 26.

To accumulate the hours,
totalHours += hoursOfLabor;
Okay, so thanks to your suggestion, I think I have it working properly. I added a function (because I forgot that it was a requirement), but things seem to be looking okay. The only thing bugging me is that I got it to work on complete accident, and I'm not yet sure I fully understand what's going on here. I have 2 global constants (also a requirement), and set totalHours = 0, because it acts as a counter that accumulates each calculation based on the number of rooms. I THINK that is what is happening, but I could be wrong.

EDIT: I just noticed my pass by reference variable "double& totalRoomArea" doesn't appear to work like I think it is supposed to. As I understand, pass by reference passes the address of the allocated memory, as apposed to the value. I thought if I removed the ampersand sign, that it would only calculate the hours of the last room entered,but it still calculates all rooms (I entered 3). Am I over thinking this, or missing the mark completely?


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream> 
#include <iomanip>
using namespace std; 
double getTotalHours(double& totalRoomArea); 

double const HOURS = 8; //global constant for hour worked on rooms > 150 sqrft. 
double const HOURLY_RATE = 19.99; // hourly charge

int main()
{
	
	int numOfRooms;
	double area;
	double totalHours = 0; //accumulates each calculation of total hours. 
	cout << "enter number of rooms; ";
	cin >> numOfRooms;

	for (int i = 1; i <= numOfRooms; i++)
	{
		cout << "area of room # " << i << " (in square feet): ";
		cin >> area;
		cout << endl; 
		if (area < 150) //this checks to ensure that room size is at least 150 sqrft. 
		{
			cout << "Your room size is too small \n"; 
			cout << "Enter a size greater than 150 square feet: "; 
			cin >> area; 
		}

		totalHours += getTotalHours(area);
	}

	cout.setf(ios::fixed); 
	cout << setprecision(2) << totalHours; 
	

	
}

double getTotalHours(double& totalRoomArea)
{
	
	return  (totalRoomArea / 115 * HOURS); 
	
}
Last edited on
closed account (48T7M4Gy)
Your interpretation is pretty much accurate.
Your code at line 30 obscures it a little and to make it clearer you might change it to:
latestHours = getHours( pass by value, not by reference );
totalHours += latestHours;

All I've done is added a new variable and changed name of the function. No magic.
The reason for passing by value rather than reference is that it is only luck that you don't have to know the total area. If you did print it out you'd find it didn't make sense due to it being replaced each time with totalHours, which is a bad name anyway.
I have to have at least one pass by reference function as a requirement, so that's why I included it. It seems to run okay, but I'm just wondering if it's being used correctly.
closed account (48T7M4Gy)
Yeah I thought that might be the case. Stick with what you had but the ambiguity in the use of the variables I mentioned still remains. It's not the end of the world.

This is the sort of thing I am driving at:

1
2
3
4
5
6
double getCosts(double& totalRoomArea, double area)
{
	totalRoomArea += area; // area remains useful elsewhere in main, and area is totalled
	return  (totalRoomArea / 115 * HOURS); // cost is worked out
	
}


Up to you of course :)
Topic archived. No new replies allowed.