joecrouch_75
n00b
- Joined
- Jun 29, 2006
- Messages
- 13
For my college C++ class I have to write a program. Here is the objectives...
You are working for a company that lays ceramic floor tile, and they need a program that estimates the number of boxes of tiles 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 intially prompt the user for the size of the tile in inches, and the number of rooms to be input. It should then input the dimensions for each room, and output the tiles needed for that room. After the last room is input, the program also should output the the total number of tiles needed, the number of boxes needed, and how many extra tiles will be left over.
The program works fine without any differences when I submit it to the server for grading, however now that we've learned functions... each function, including main, cannot be longer than 30 lines including the braces. So I'm looking for suggestions on how I can shorten my main function! Any help is greatly appreciated!
You are working for a company that lays ceramic floor tile, and they need a program that estimates the number of boxes of tiles 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 intially prompt the user for the size of the tile in inches, and the number of rooms to be input. It should then input the dimensions for each room, and output the tiles needed for that room. After the last room is input, the program also should output the the total number of tiles needed, the number of boxes needed, and how many extra tiles will be left over.
The program works fine without any differences when I submit it to the server for grading, however now that we've learned functions... each function, including main, cannot be longer than 30 lines including the braces. So I'm looking for suggestions on how I can shorten my main function! Any help is greatly appreciated!
Code:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const int INCHES_IN_A_FOOT = 12;
const int ZERO = 0;
const int TILES_PER_BOX = 20;
int GetPositiveInteger(string inputPrompt, string errMessage, bool flagForInches);
int NumberOfTilesForRoom(int SizeOfTile, int RoomWidthFeet, int RoomWidthInches,
int RoomLengthFeet, int RoomLengthInches);
void DisplayResults(int tiles, int boxes, int extra);
int NumberOfBoxes(int numTiles);
void main()
{
int SizeOfTile, NumberOfRooms, RoomCounter, RoomWidthFeet, RoomWidthInches,
RoomLengthFeet, RoomLengthInches, TilesForRoom, TotalTilesCounter, Boxes, Extra;
string inputPrompt, errMessage;
string errMessage_SizeOfTile = "Invalid size of tile in inches: ";
string errMessage_NumberOfRooms = "Invalid number of rooms: ";
string errMessage_RoomWidthFeet = "Invalid room width feet: ";
string errMessage_RoomWidthInches = "Invalid room width inches: ";
string errMessage_RoomLengthFeet = "Invalid room length feet: ";
string errMessage_RoomLengthInches = "Invalid room length inches: ";
string inputPrompt_SizeOfTile = "Enter size of tile in inches: ";
string inputPrompt_NumberOfRooms = "Enter number of rooms: ";
string inputPrompt_RoomWidthFeet = "Enter room width feet: ";
string inputPrompt_RoomWidthInches = "Enter room width inches: ";
string inputPrompt_RoomLengthFeet = "Enter room length feet: ";
string inputPrompt_RoomLengthInches = "Enter room length inches: ";
bool flagForInches = false;
TotalTilesCounter = 0;
//Input Size of Tile In Inches Segment
inputPrompt = inputPrompt_SizeOfTile;
errMessage = errMessage_SizeOfTile;
SizeOfTile = GetPositiveInteger(inputPrompt, errMessage, flagForInches);
//Input Number of Rooms Segment
inputPrompt = inputPrompt_NumberOfRooms;
errMessage = errMessage_NumberOfRooms;
NumberOfRooms = GetPositiveInteger(inputPrompt, errMessage, flagForInches);
cout << endl;
//Input Room Dimensions & Computer # of Tiles Segment
for (RoomCounter = ZERO; RoomCounter < NumberOfRooms; RoomCounter++)
{
//Input Room's Width in Feet Segment
inputPrompt = inputPrompt_RoomWidthFeet;
errMessage = errMessage_RoomWidthFeet;
RoomWidthFeet = GetPositiveInteger(inputPrompt, errMessage, flagForInches);
//Input Room's Width in Inches Segment
inputPrompt = inputPrompt_RoomWidthInches;
errMessage = errMessage_RoomWidthInches;
flagForInches = true;
RoomWidthInches = GetPositiveInteger(inputPrompt, errMessage, flagForInches);
flagForInches = false;
//Input Room's Length in Feet Segment
inputPrompt = inputPrompt_RoomLengthFeet;
errMessage = errMessage_RoomLengthFeet;
RoomLengthFeet = GetPositiveInteger(inputPrompt, errMessage, flagForInches);
//Input Room's Length in Inches Segment
inputPrompt = inputPrompt_RoomLengthInches;
errMessage = errMessage_RoomLengthInches;
flagForInches = true;
RoomLengthInches = GetPositiveInteger(inputPrompt, errMessage, flagForInches);
flagForInches = false;
TilesForRoom = NumberOfTilesForRoom(SizeOfTile, RoomWidthFeet, RoomWidthInches,
RoomLengthFeet, RoomLengthInches);
TotalTilesCounter = TotalTilesCounter + TilesForRoom;
cout << "Room requires " << TilesForRoom << " tiles." << endl << endl;
}
Boxes = NumberOfBoxes(TotalTilesCounter);
Extra = (Boxes * TILES_PER_BOX) - TotalTilesCounter;
DisplayResults(TotalTilesCounter, Boxes, Extra);
}
// The function displays the input promt inputPrompt and reads in an integer value.
// If the input value is not positive, or the value is 12 or above when flagForInches
// is true, then the function displays the error message errMessage, and prompts for
// an input again until a valid value is input, which is returned by the function.
// Parameters: (in, in, in)
int GetPositiveInteger(string inputPrompt, string errMessage, bool flagForInches)
{
int ValidValue;
cout << inputPrompt;
cin >> ValidValue;
while (ValidValue <= ZERO || (flagForInches && ValidValue >= 12))
{
cout << errMessage << ValidValue << "." << endl;
cout << inputPrompt;
cin >> ValidValue;
}
return ValidValue;
}
// The function computes the number of tiles required for each individual room.
// Parameters: (in, in, in, in, in)
int NumberOfTilesForRoom(int SizeOfTile, int RoomWidthFeet, int RoomWidthInches,
int RoomLengthFeet, int RoomLengthInches)
{
int Tiles = (ceil(((RoomWidthFeet * INCHES_IN_A_FOOT) + RoomWidthInches) / float(SizeOfTile)))
* (ceil(((RoomLengthFeet * INCHES_IN_A_FOOT) + RoomLengthInches) / float(SizeOfTile)));
return Tiles;
}
// The function computes and returns the number of boxes needed to get numTiles tiles.
// Parameters: (in)
int NumberOfBoxes(int numTiles)
{
int Boxes;
Boxes = ceil(float(numTiles) / TILES_PER_BOX);
return Boxes;
}
// The function displays the computed results for a job: the total number of tiles,
// the number of boxes needed, and the extra tiles left over.
// Parameters: (in, in, in)
void DisplayResults(int tiles, int boxes, int extra)
{
cout << "Total tiles required is " << tiles << "." << endl;
cout << "Number of boxes needed is " << boxes << "." <<endl;
cout << "There will be " << extra << " extra tiles.";
}