Description
You are working for a company that lays ceramic floor tile, and its employees need a program that estimates the number of boxes of tile for a job. A job is estimated by taking the dimensions of each room in feet and inches, and converting these dimensions into a multiple of the tile size (rounding up any partial multiple) before multiplying to get the number of tiles for the room. A box contains 20 tiles, so the total number needed should be divided by 20 and rounded up to get the number of boxes. The tiles are assumed to be square.
The program should initially prompt the user for the size of the tile in inches and the number of rooms to be covered with tile. It should then input the dimensions for each room and output the number of tiles needed for that room. After data for the last room is input, the program should output the total number of tiles needed, the number of boxes of tile needed, and how many extra tiles will be left over.
Here is an example of how a run might appear:
Enter number of rooms: 2
Enter size of tile in inches: 12
Enter room width (feet and inches, separated by a space): 17 4
Enter room length (feet and inches, separated by a space): 9 3
Room requires 180 tiles.
Enter room width (feet and inches, separated by a space): 11 6
Enter room length (feet and inches, separated by a space): 11 9
Room requires 144 tiles.
Total tiles required is 324.
Number of boxes needed is 17.
There will be 16 extra tiles.
Your program should check for invalid data such as non-positive dimensions, number of rooms less than 1, number of inches greater than 11, and so on. It should prompt the user for corrected input whenever it detects invalid input. Use proper indentation and style, meaningful identifiers, and appropriate comments.
Background:
For those of you who have never installed ceramic tile, you may not realize that each tile needs to line up such that the grout lines (the filler between tiles) are even. For instance, the below diagram represents the correct way the tile should appear after it is installed.
Room length: 7’ 5”
Room width: 5’ 6”
Size of each tile: 12” square
Notice that the last (bottom row) does not require an entire tile to fill up each space in the row. The same is true for every tile in the last column. For each tile in the bottom row and last column, you have to cut the tiles to fit. Assume that any leftovers from each individual tile you have to cut must be thrown away. That is, consider the leftovers as scrap for this assignment.
Thus, you must approach the algorithm in such a way that you start the tile from one corner of a room and install a whole column (cutting the last tile as needed to fit). Once you finish the first column, move on to the second column, following the same algorithm, and so on. When you get to the last column, be aware that you might have to cut each tile in the column to fit the width of the room as well. Again, if you have to cut a tile, just consider any excess to be scrap.
In the example above, how many tiles would you need to install the first column? Would 7 be enough? No, you would end up with a gap at the bottom. Therefore, you have to use 8 tiles for each column. Now, how about the rows? How many tiles are needed for each row? The answer is 6. Only using 5 tiles would also leave a gap. Finally, to find out how many tiles are needed for the entire room, you multiply the number of tiles needed for each row by the number of tiles needed for each column (8 x 6), or 48 tiles.
Given that the problem allows you to install tiles in any number of rooms, you have to go through this procedure for every room that the user wants to tile, keeping a running total of the number of tiles needed for all of the rooms. Finally, (in case you haven’t had to purchase tile yet), ceramic tile is normally sold by the box. The instructions tell us that every box contains 20 tiles. Therefore, by dividing the running total by 20, you can easily determine the total number of boxes you need to buy. You also have to calculate the number of leftover (whole) tiles that you will have. [Hint: I encourage you to use a constant to keep track of the number of tiles per box. That way, if the store decides to stock tiles from a different manufacturer that packages tiles in 25 tiles per box, you only have to make the change in one place in your code.]
Error checking:
As in all applications, you should design your code to be robust enough to handle anything that a user may enter. Never, (and I repeat NEVER), design your program to assume that the user meant something other than what he entered. For example, your code should prompt the user to enter the number of rooms to tile. Naturally, we wouldn’t expect a user to enter 2.3 rooms. In this problem, assume you can’t have a portion of a room (the same as you can’t have a fraction of a person). Therefore, if you declare the variable that holds numberOfRooms to be an integer, what happens to the decimal part that the user enters? Yes, it gets truncated. This is an example of your program assuming the user made a mistake and your code fixing it to be what you think it should be. This can be VERY dangerous. So, instead of allowing the user to enter a real number and simply truncating the decimal portion, make sure you catch this error and have the user re-enter the number as an integer. I can tell you from experience that sometimes I will intend to enter a “0”, and I accidentally hit the period (“.” ). Instead of 2.3, the user may have intended 203 – yikes! That’s a big difference.
In addition to making sure that the user entered an integer, you also need to make sure that he entered a reasonable integer. For example, what if he entered -5 rooms? What is a negative room? It doesn’t make sense. In this program, you don’t have to put any upper limit on the number of rooms to tile. For example, what if this application is being used to calculate the number of tiles needed for a hotel? The number of rooms to tile could be quite large. Therefore, you only need to check for zero (which doesn’t make sense) and negative numbers.
Regarding numeric input, you must make sure that the user enters numbers when numbers are needed. For example, when I’m testing, if you tell me to enter a positive integer for the number of rooms to tile, you KNOW that I’m going to try to break it by entering “abc” at the prompt. This action will cause “cin” to enter a fail state, which can produce erroneous results and potentially even cause your program to crash. Therefore, you must trap for non-numeric data whenever the program is expecting a number. One way to trap for this error is to check if “cin” has entered the fail state. If it has, then you first have to clear the cin buffer and then issue the “ignore” command before re-prompting the user. For those of you who have not written code like this before, below is a pretty good piece of code that will handle such situations. Note that you can tweak it as necessary if you want to customize your error messages. This code traps for non-numeric data as well as negative data in the same condition. The drawback is that it produces a single generic error message to the user such that the user may not realize what he did wrong to cause the problem.
int num;
cout << “Enter an integer: ” << endl;
cin >> num;
while (cin.fail() || num < 0)
{
cout << “You must enter a number, and that number must be positive. Please try again. ” << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ‘\n’);
cin >> num;
}
The last error check that you must include is a special case for this particular application. When you prompt the user to enter the room dimensions, you specify that it must be entered as number of feet, followed by number of inches (separated by a space). If the user enters 5 6, we know that means 5 feet, 6 inches. But what if he enters 5 16? Ask yourself, how many inches are in a foot? The answer is 12. Therefore, 5 16 inches is not the correct way to enter this measurement. Instead, if the inches part is 12 or greater, you need to alert the user that this is an invalid input because the number of inches entered exceeds 12. If I were writing this code, I would likely calculate what the user should have entered (i.e. 6 4 – in place of 5 16), and ask the user if that is what he intended. The easiest way, however, would be to just alert the user of the mistake and allow him to re-enter the information in the proper format.
Good luck on this assignment! Have fun with it, and as always, let me know if you have any questions.
Deliverables:
- Complete the programming assignment described above and submit your completed assignment in accordance with the lab submission policies.

