Whats wrong with this code?

Cant get this code to compile right... im not sure why... any help would be amazing.

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
60
61
62
63
64
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void getTiles(int& tileSize, int& numberOfTiles);

int main()
{
	/******************** Variables ***************************/
	int rooms;
	int tileSize;
	int numOfTiles = 0;
	int totalTiles;
	int extraTiles;
	int numOfBoxes;
	int lastTileNum = 0;

	cout << "Enter number of rooms";
	cin >> rooms;
	cout << "Enter tile size in inches";
	cin >> tileSize;
	int x = rooms;

	for (x; x <= rooms; x++)
	{
		getTiles(tileSize, totalTiles);
		totalTiles = numOfTiles + lastTileNum;
		lastTileNum = numOfTiles;
	}
	numOfBoxes = totalTiles/20;
	extraTiles = totalTiles - numOfBoxes * 20;
	if (totalTiles%20 > 0)
		numOfBoxes++;

	cout << totalTiles;
	cout << numOfBoxes;
	cout << extraTiles;

	
	return 0;
}//EOMain

void getTiles(int& tileSize, int& numOfTiles)
{
	int area;
	int widthFeet;
	int lenghtFeet;
	int widthInches;
	int lengthInches;

	cout << "Enter room width (feet and inches, separated by a space)";
	cin >> widthFeet >> widthInches;
	widthInches = widthFeet * 12 + widthInches;
	cout << "Enter room length (feet and inches, separated by a space)";
	cin >> lenghtFeet >> lengthInches;
	lengthInches = lenghtFeet * 12 + lengthInches;

	area = widthInches * lengthInches;

	numOfTiles = area/tileSize;
	if (area%tileSize > 0)
		numOfTiles ++;
}


It said i was trying to use numOfTiles with out initializing it first.. so i assigned it a zero... it stopped the error but nothing is printing
Last edited on
line 6 you call the second argument numberOfTiles and on line 44 you call it numOfTiles.

Also, why did you make the function void, and why did you pass the first argument by reference? It doesn't look like your changing its value in the function. Why don't you just declare the function as int getTiles(int tileSize) and have it return numOfTiles as an integer?
Also:
23
24
25
int x = rooms;

for (x; x <= rooms; x++)

I don't know what you're trying to do here, but this definitely can't be right.
long double main is right. I think you meant to initialize x to 1, not rooms.

Also, I noticed in the for loop that you wrote totalTiles=numOfTiles+lastTileNum;, yet you initialized both numOfTiles and lastTileNum to 0. You have to realize that the variable numOfTiles in the function getTiles is not the same as the variable numOfTiles in the main function, even though you gave them the same name. The function getTiles does not change either the value of numOfTiles or lastTileNum in the main function, so basically line 28 always gives totalFiles a value of 0. It changes the value of totalTiles since that is the variable that you pass by reference on line 27, and that is the only argument that you modify in the function (Again, I don't understand why you passed tileSize by reference).

On line 27, perhaps you meant to pass numOfTiles as the second argument, and not totalTiles? THAT would change the value of numOfTiles in the main function.

Furthermore, you should reconsider your calculations. Are the tiles square? If so, then shouldn't line 61 say numOfTiles=area/(tileSize*tileSize);? What if this number doesn't divide evenly? For instance, what if area is equal to 50 and tileSize is equal to 2? 50/(2*2)=12.5. Since you declare numOfTiles as an integer, 12.5 will round down to 12, the nearest integer. But don't you really need 13 tiles?
Last edited on
Thank you CJ.
I was tired last night and didnt eve notice some of those mistakes....
Got it working heres the finished program.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

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

void getTiles(int& tileSize, int& numOfTiles);

int main()
{
	/*************** Variables ******************/
	int rooms;
	int tileSize;
	int numOfTiles;
	int totalTiles;
	int extraTiles;       
	int numOfBoxes;
	int lastTileNum = 0;  //Used to store the last room tile number

	/*********** Get Number of Rooms ***********/
	cout << "Enter number of rooms" << endl;
	cin >> rooms;
	
	/************ Get Size of tiles ************/
	cout << "Enter tile size in inches" << endl;
	cin >> tileSize;

	/*************** Main Loop ******************
	* This loop will aks for the size of the room(s)
	* and calculate the number of tiles needed. 
	********************************************/
	int x = 1;
	for (x; x <= rooms; x++)
	{
		/************* Room Lable **************/
		if (x >= 1 && x <= rooms)
			cout << endl << "-Room " << x << "-" << endl;

		getTiles(tileSize, numOfTiles);
		totalTiles = numOfTiles + lastTileNum;
		lastTileNum = numOfTiles;
		
	}//EOMain Loop
	
	/*********** Get Number of Boxes ***********/
	numOfBoxes = totalTiles/20;
	if (totalTiles%20 > 0) // rounds up
		numOfBoxes++;
	extraTiles = numOfBoxes * 20 - totalTiles; //Gets left over tiles

	/************ Out Print to User ************/
	cout << endl;
	cout << "The total tiles required for all rooms is " << totalTiles << "." << endl;
	cout << "The number of box of tiles needed is " << numOfBoxes << "." << endl;
	cout << "There will be  " << extraTiles << " extra tiles." << endl;

	system("PAUSE");
	return 0;
}//EOMain

/********** Room Calculation Function **********/
void getTiles(int& tileSize, int& numOfTiles)
{
	/************** Variables ******************/
	int roomArea;
	int widthFeet;
	int lenghtFeet;
	int widthInches;
	int lengthInches;

	/*********************** Get Dimensions of Rooms ****************************/
	cout << "Enter room width (feet and inches, separated by a space)"  << endl;
	cin >> widthFeet >> widthInches;
	widthInches = widthFeet * tileSize + widthInches;     //turns feet/inches into just inches
	cout << "Enter room length (feet and inches, separated by a space)"  << endl;
	cin >> lenghtFeet >> lengthInches;
	lengthInches = lenghtFeet * tileSize + lengthInches;  //turns feet/inches into just inches

	/************** The Math ******************/
	roomArea = widthInches * lengthInches;
	numOfTiles = roomArea/(tileSize*tileSize);
	if (roomArea%tileSize > 0) // adds a box even if there is only 1 extra tile needed. 
		numOfTiles ++;
	cout << "This room requires " << numOfTiles << " tiles." << endl;
}//EOgetTiles 
Topic archived. No new replies allowed.