need help with void function and pass by refrence

Hello everyone,
My Programing Assignment is to write a program that will take in the information on how may rooms will be tiled, how big the tiles are, and the width and length of each room.
My output should be, tile amount of each room, total amount of all rooms added, how many boxes of tiles are needed (1 box has 20 tiles) and how many tiles would be left.
We need to use the void function for this assignment. I have written the program and it all works, except of one problem. If I have 3 rooms I will need to calculate the tile amount of all 3 rooms. However, I am not able to figure out how to do that. After I input each room’s information, it will only print out the last room’s tile amount for my total.
My question is, how do I save each calculated tile amount of each room, and then call it to add for my total. I included my program I have written so far. I hope someone can help me. Thanks

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
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <iomanip> 
#include <cmath>
using namespace std;

void CalcuateTiles (int tileSize,int roomWidthFeet,int roomWidthInch,int roomLengthFeet,int roomLengthInch,	float& numTiles);
void DeterminTileAmont (float numTiles, float tileTotal, float boxTotal, float leftTiles);

int main()
{
	int numRooms, tileSize,roomWidthFeet,roomWidthInch,roomLengthFeet,roomLengthInch;	
	float numTiles = 0;
	float leftTiles = 0;
	float tileTotal = 0;
	float boxTotal = 0;
	
	cout << "Enter numbers of Rooms beeing tiled"<< endl;
	cin >> numRooms;
	cout << "Enter inch size of Tile used for job" << endl;
	cin >> tileSize;
	
	for (int count = 1; count <= numRooms; count++)
	{	
	cout << "Enter Rooms width in feet and inches (sepreat numbers with a space)" <<endl;
	cin >> roomWidthFeet >> roomWidthInch;
	cout << "Enter Room length in feet and inches (sepreat numbers with a space)" <<endl;
	cin >> roomLengthFeet >> roomLengthInch;
	CalcuateTiles (tileSize,roomWidthFeet,roomWidthInch,roomLengthFeet,roomLengthInch,numTiles);
	}

	DeterminTileAmont (numTiles,tileTotal, boxTotal,leftTiles);

	
	cin.get();
	cin.get();
	return 0;
}
void DeterminTileAmont (float numTiles, float tileTotal, float boxTotal,float leftTiles)
{
	tileTotal= numTiles;
	boxTotal = tileTotal / 20;
	leftTiles = (boxTotal * 20) - tileTotal;
		cout << "Total tiles requierd for job is: " << tileTotal << endl;
		cout << "Number of boxes nedded is: " << boxTotal << endl;
		cout << "Tiles left are: " << leftTiles << endl;
}


void CalcuateTiles (int tileSize,int roomWidthFeet,int roomWidthInch,
					int roomLengthFeet,int roomLengthInch,float& numTiles)
{
	float sumWidth;
	float sumLength;

	sumWidth = ((roomWidthFeet *12) + roomWidthInch) / tileSize;
	sumLength = ((roomLengthFeet*12) + roomLengthInch) / tileSize;
	numTiles = (sumWidth + 1) * (sumLength + 1);
	cout << "The Room requiers " << numTiles << " Tiles" << endl;
}
Last edited on
CalculateTiles() is to find the number of tiles needed and return it via the parameter numTiles, correct?

In that case, you'll need to store those output values into an array or something so you can access them all later.
I am not quite sure how that would work. I have not learned about array's, I know the chapter talks about parameters and pass by reference, however, it does not work for me. If someone could maybe show me an example of a program (similar to my Assignment above), that would take an output number and add it up to create a total. I really would appreciate.
By passing values by reference then :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void sumThem(int a, int b, int& c);  // Note the & symbol, this means c is by reference
int main()
{
  int a = 3, b = 4, c;
  
  sumThem(a, b, c);

  cout << a << " + " << b << " = " << c;

  cout <<'\n\nHit enter to quit"; cin.get();
  return 0;
}

void sumThem(int a, int b, int& c)
{
  c = a + b;
} 


In this example, sumThem has made brand new a and b variables. Their scope is only in the sumThem function. However, when you passed by reference, you passed the memory address of c, so that the sumThem function can actually effect the "real" c.
Last edited on
As your are passing those variables by value in the functions (except numtiles), you need to store those values calculated in the function if not in array then in variables, otherwise they will be lost every time program control comes out of function body! or you can pass those values by reference so that program can change their original values after calculations!!!
Topic archived. No new replies allowed.