Loops

I'm so lost. The closer I get to the end of the program, the harder it gets. I know this is a terrible mess, but I really need help. I don't know where to start fixing. This program is supposed to estimate the number of boxes of tile for a job for a company.

When I run it, I get the correct answers up until the AddTotalTiles function runs. That's all wrong. How do I get the program to add each instance of totalTiles (from the GetTiles function)? I need to be able to add up each room's tile amount for a grand total....So, for instance, if room 1 needs 180 tiles and room 2 needs 144 tiles, I need a grand total of 324 total tiles needed.
This seemed so much easier when I wrote down "how" to do it all on paper...
Please help.

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
//This is a program that calculates the number of boxes of floor
//tile needed to complete x amount of rooms, given the size of
//the tile to be used.

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

ofstream outfile;

//Variable declarations
    int rooms;
    int tileSize;
    int lengthFeet;
    int lengthInches;
    int widthFeet;
    int widthInches;
    int totalTiles;
    int grandTotalTiles;

//Function prototypes
int GetTiles(int lengthInches, int lengthFeet, int widthInches, int widthFeet);
void AddTotalTiles(int countTotalTiles);

int main()
{
    //Open outfile for answers
    outfile.open("answers.out");
    if(!outfile)                //Check to make sure outfile opens correctly
    {
        cout << "Error opening output file." << endl;   //If it doesn't, display error message
        return 0;                                       //and quit
    }

    cout << "Enter number of rooms: " << endl;          //Prompt for number of rooms to tile
    cin  >> rooms;                                      //Read in number of rooms to tile
    outfile << "Number of rooms entered is: " << rooms << endl;
    cout << "Enter size of tile in inches: " << endl;   //Prompt for size of tile in inches
    cin  >> tileSize;                                   //Read in size of the tile in inches
    outfile << "Size of tile entered (in inches) is: " << tileSize << endl; //Print out size of tile entered

    for( rooms; rooms > 0; rooms--)           //While room number is greater than 0
    {
        cout << "Enter length of room (in feet and inches, separated by a space.): " << endl;//Prompt for room length
        cin  >> lengthFeet >> lengthInches;             //Read in room length in feet and inches
        outfile << "Length of room entered (in feet and inches) is: " << lengthFeet          //Print out entered amounts
                << " " << lengthInches << endl;
        cout << "Enter width of room (in feet and inches, separated by a space.): " << endl; //Prompt for room width
        cin  >> widthFeet >> widthInches;               //Read in room width in feet and inches
        outfile << "Width of room entered (in feet and inches) is: " << widthFeet            //Print out entered amounts
                << " " << widthInches << endl;
        GetTiles(lengthInches, lengthFeet, widthInches, widthFeet);         //Call function
        AddTotalTiles(totalTiles);

    }
     return 0;                  //Quit
}
//*********************************************************************************
//GetTiles function
int GetTiles(int lengthInches, int lFeetToConvert, int widthInches, int wFeetToConvert)
{
  float numTilesL = 0;
  numTilesL = ((lFeetToConvert * 12) + lengthInches) / tileSize; //Multiply feet by 12 and add Inches to get total inches length

  numTilesL = numTilesL +1;                                      //Round up answer
  cout << "You will need " << numTilesL << " tiles for length." << endl;
  float numTilesW = 0;
  numTilesW = ((wFeetToConvert * 12) + widthInches) / tileSize;  //Multiply feet by 12 and add Inches to get total inches width
  numTilesW = numTilesW + 1;                                     //Round up answer
  cout << "You will need " << numTilesW << " tiles for width." << endl;
  float totalTiles = 0;
  totalTiles = numTilesL * numTilesW;               //Multiply total length by width to get total tiles
  cout << "You will need " << totalTiles << " tiles total for this room." << endl << endl;

return (totalTiles);
}

void AddTotalTiles (int countTotalTiles)
{
  countTotalTiles = countTotalTiles + totalTiles;
  cout << "You will need " << countTotalTiles << " total tiles to cover all rooms." << endl << endl;
  return ;
}
Someone please help. This is due by tonight and I just keep making it worse...
Could you give us an idea of what you are supposed to be doing?
You have done yourself no favours by having a big bunch of global variables. This is why people advise against such things; it's too easy to just get confused and lose track of what's being done where, and what needs to be done. Let's throw away that addTotalTiles function.

Your maths isn't quite right (for example, with one inch tiles, your code suggests that a room 1 foot by 1 foot will need 13x13=169 tiles, rather than 12x12=144 tiles.


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
//This is a program that calculates the number of boxes of floor
//tile needed to complete x amount of rooms, given the size of
//the tile to be used.

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

ofstream outfile;

//Variable declarations
    int rooms;
    int tileSize;
    int lengthFeet;
    int lengthInches;
    int widthFeet;
    int widthInches;
    int totalTiles;
    int grandTotalTiles;

//Function prototypes
int GetTiles(int lengthInches, int lengthFeet, int widthInches, int widthFeet);

int main()
{
  grandTotalTiles = 0;
    //Open outfile for answers
    outfile.open("answers.out");
    if(!outfile)                //Check to make sure outfile opens correctly
    {
        cout << "Error opening output file." << endl;   //If it doesn't, display error message
        return 0;                                       //and quit
    }

    cout << "Enter number of rooms: " << endl;          //Prompt for number of rooms to tile
    cin  >> rooms;                                      //Read in number of rooms to tile
    outfile << "Number of rooms entered is: " << rooms << endl;
    cout << "Enter size of tile in inches: " << endl;   //Prompt for size of tile in inches
    cin  >> tileSize;                                   //Read in size of the tile in inches
    outfile << "Size of tile entered (in inches) is: " << tileSize << endl; //Print out size of tile entered

    for( ; rooms > 0; rooms--)           //While room number is greater than 0
    {
        cout << "Enter length of room (in feet and inches, separated by a space.): " << endl;//Prompt for room length
        cin  >> lengthFeet >> lengthInches;             //Read in room length in feet and inches
        outfile << "Length of room entered (in feet and inches) is: " << lengthFeet          //Print out entered amounts
                << " " << lengthInches << endl;
        cout << "Enter width of room (in feet and inches, separated by a space.): " << endl; //Prompt for room width
        cin  >> widthFeet >> widthInches;               //Read in room width in feet and inches
        outfile << "Width of room entered (in feet and inches) is: " << widthFeet            //Print out entered amounts
                << " " << widthInches << endl;
        GetTiles(lengthInches, lengthFeet, widthInches, widthFeet);         //Call function
       

    }
    cout << "You will need " << grandTotalTiles << " to do all rooms"; 
     return 0;                  //Quit
}
//*********************************************************************************
//GetTiles function
int GetTiles(int lengthInches, int lFeetToConvert, int widthInches, int wFeetToConvert)
{
  float numTilesL = 0;
  numTilesL = ((lFeetToConvert * 12) + lengthInches) / tileSize; //Multiply feet by 12 and add Inches to get total inches length

  numTilesL = numTilesL +1;                                      //Round up answer
  cout << "You will need " << numTilesL << " tiles for length." << endl;
  float numTilesW = 0;
  numTilesW = ((wFeetToConvert * 12) + widthInches) / tileSize;  //Multiply feet by 12 and add Inches to get total inches width
  numTilesW = numTilesW + 1;                                     //Round up answer
  cout << "You will need " << numTilesW << " tiles for width." << endl;
  float totalTiles = 0;
  totalTiles = numTilesL * numTilesW;               //Multiply total length by width to get total tiles
  cout << "You will need " << totalTiles << " tiles total for this room." << endl << endl;
  grandTotalTiles +=totalTiles;
return (totalTiles);
}

Last edited on
L B, I'm sorry. I thought I did. I'm using these numbers to run the program when prompted:
# of rooms: 2
Tile size: 12
Room length in feet and inches: 17 4
Room width in feet and inches: 9 3

//At this point, the program DOES calculate the correct # of tiles, which is 180.
//Then it goes on to prompt for the second room's #'s.

Room length: 11 6
Room width: 11 9

//Then it calculates this room's needed tiles, which is also correct, at 144.

My problem, is that I need to find a way to add up these two amounts for a Grand Total of Tiles needed. I know I need to use a loop somehow, but I can't figure it out. Been at it all morning and afternoon. I'm terrible with parameters and passing values, so I know I need help. I attempted to write a function to do this (AddTotalTiles) but it is not working. It says 0 tiles are needed when the program runs.
Last edited on
Start testing with the simplest case. A room 1 foot 0 inches, and tiles of size 1 inch.

My problem, is that I need to find a way to add up these two amounts for a Grand Total of Tiles needed.

The code I posted does that. I didn't post it just because I like it :)
Ah, Moschops! I didn't even see your response when I wrote that last post. Okay, about global variables. I had them inside main before, but then a few of them said they were not declared. I thought doing it that way would make them declared, that's why I moved them. I'm going to go study your post now...
I thought doing it that way would make them declared, that's why I moved them.


I suspect that your problem was that the insides of your functions didn't know about them. The good solution is to make them parameters and pass as necessary, and to have sensible return values that you can use. The bad solution is to make everything global :(
Thank you very much for your input. With my math problems, I was trying to take whatever the answers were and if there was any decimal portion, to round up to the next integer. I tried using the ceiling function, but it wasn't doing anything. After a morning messing with that, I just added one to each answer so I could try and move on.
Would the ceiling function have worked here, perhaps?
numTilesL = numTilesL +1; //Instead of adding 1, could I have set it equal to:
numTilesL = ceil(numTilesL);

I tried that, but it didn't round anything...I also tried it in the lines above that total line, just trying to round up each individual answer (length and width totals) but they didn't round up either....

And I have two questions about your code:
1) in the for loop, I see that your first part there is blank. You only have a ;
I didn't know you could do that, I thought something had to be in there. So what does that "say"?

2) Down at the bottom of GetTiles right before the return statement you have:
grandTotalTiles +=totalTiles;

what does that "say"? I've never seen that before either.
Thank you.
Last edited on
You're suffering a loss of precision here:
((lFeetToConvert * 12) + lengthInches) / tileSize;
Because all these numbers are of type int, you'll get back an int, rounded down.

Try this:

((lFeetToConvert * 12) + lengthInches) / (double)tileSize;
Last edited on
1) in the for loop, I see that your first part there is blank. You only have a ;
I didn't know you could do that, I thought something had to be in there. So what does that "say"?


A for loop is

for (initialization-statement; stop-when-this-condition-is-false; thing-to-do-after-each-loop)

The initialization is simply a statement that gets executed once, at the start. If you've got nothing that you need done, then it can be blank. Your initialization statement was rooms;. That does nothing. It's like having some code that says
1
2
int x=7;
x; // What does this do? Nothing 



grandTotalTiles +=totalTiles; is shorthand for grandTotalTiles = grandTotalTiles + totalTiles;

Last edited on
My very last question about this is: (Thank god for this forum. I learn more in these posts than I learn in class. Pretty sad, I know. Thank you very very much for your help and explanations.)

Now that I have my total tiles, and my # of boxes needed, how do I use the mod function (%, right?) to take the remainder from my boxes (before I rounded it up, so the 16.2 number)?
I need to calculate how many extra tiles I will have in that last box. So, I know that the answer I need is 16 extra tiles, but I'm not getting the programming right.

I know I need to take 20(which is a full box of tiles) and subtract the remainder of the full box (which I have mathematically as .2(20).)
20 - .2(20)
20 - 4 = 16 extra tiles.
How do I begin? This is the end of my main function where I want to add this in:

1
2
3
4
5
6
7
8
9
10
11
cout << "You will need " << grandTotalTiles << " to do all rooms." << endl;     //Display grand total
    float boxes = (float)grandTotalTiles / 20;                              //Calculate # of boxes needed
    outfile << boxes;                   //Print # of boxes
    boxes = ceil(boxes);                //Round # of boxes up
    cout << "You will need " << boxes << " boxes of tile." << endl; //Display # of boxes needed
    //I need to take the remainder of boxes and calculate how many extra tiles there will be here...
    float extraTiles = (float)grandTotalTiles % 20.00;
    cout << extraTiles;

         return 0;                  //Quit
}


I tried the following, but this is the error I get:
float extraTiles = (float)grandTotalTiles % 20.00;

|65|error: invalid operands of types 'float' and 'double' to binary 'operator%'|


Last edited on
remainder = (numberOfBoxesNeeded*20) - grandTotalTiles;
Why do I always make things more complicated and retarded than they are? That is so straight forward and simple and makes perfect sense. Meanwhile, I always think it has to be all difficult. Ugh. I hope one of these days I will start to see things in a more logical and simple way. You're my freakin hero, Moschops. Thank you so much.
Topic archived. No new replies allowed.