Join 137,266 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,487 people online right now. Registration is fast and FREE... Join Now!
Here is my problem I am trying to write a program that will estimate the number of boxes of tile needed for a job. A job is estimated by taking the dimensions of each room in feet and inches and converting these 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 should have 20 tiles, so I need to divide the total number needed by 20 and rounded up to get the number of boxes (they are square). Here is what I have so far:
int main() { // Input Variables float roomNumber; float tileSize; int roomWidth; int roomLength; float tileNo;
// Declare and open output file ofstream dataOut; dataOut.open("tile.out"); if ( !dataOut ) { // no cout << "Can't open output file." << endl; return 1; }
PrintHeading(dataOut);
// Prompt for and read Room Number cout << "Input the total number of rooms to be tiled: " << endl; cin >> roomNumber;
// Loop calculating room number while (roomNumber >= 0) { // Prompt for and read tile size, room width, room length. cout << "Input the size of tile in inches:" << endl; cin >> tileSize; cout << "Input the width of room (feet and inches, seperated by" << "a space):" << endl; cin >> roomWidth; cout << "Input the length of room (feet and inches, seperated" << " by a space):" << endl; cin >> roomLength;
DetermineTileno(roomNumber, tileSize, roomWidth, roomLength, tileNo); PrintResults(dataOut, roomNumber, roomWidth, roomLength, tileSize, tileNo); // Prompt for and read loan amount cout << " Input and total number of rooms to be tiled;" << endl; cin >> roomNumber; }
dataOut.close(); return 0; } //************************************************************* void DetermineTileno ( /* in */ float roomNumber, //Room Number /* in */ float tileSize, //Size of Tile /* in */ int roomWidth, // Room Width /* in */ int roomLength, // Room Length /* inout */ float& tileNo ) // Tile Number
// Calculate the number of tile for room amount using // the formula lenth and width // Precondition // Arguments have been assigned a value // Post condtition // tileNo contains the tile number as calculated by the formula
{ // local variables float roomSize; int tileCoverage;
void PrintResults( /* inout */ ofstream& dataOut, // Output File /* in */ float roomNumber, // Room Number /* in */ float tileSize, // Tile size /* in */ int roomWidth, // Room Width /* in */ int roomLength, // Room Lenght /* in */ float tileNo ) // Tile Number
// Prints the tile number, room number, tile size, room width, // room legth, and tile number on file dataOut // Precondtition: // File dataOut has been opened successfully && // All arguments have been assigned values // Postcodtion: // room number, tile size, room width, room length, // have all been printed on outData with proper documentation.
// Prints the heading on file dataOut for each column in the table. // Precondition: // File dataOut has been opened successfully // Postcondtion: // "Room Number", " Tile Size", "Room Width", "Room Length", // "Tile Number", have been written of file dataOut.
Here is my problem I am trying to write a program that will estimate the number of boxes of tile needed for a job. A job is estimated by taking the dimensions of each room in feet and inches and converting these 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 should have 20 tiles, so I need to divide the total number needed by 20 and rounded up to get the number of boxes (they are square). Here is what I have so far:
It will compile but it does not work. Can someone please help me get this program running.
Thanks in advance.
Can you expand on what you mean by doesn't work?
The analogy is driving your car to a mechanic and saying "my car is not working"....
Sorry, yes, the major "doesn't work" problem is that it will not tell me how many tiles I need per room entered. I can enter the room number, the room size, the tile size, but it just loops on, it never give me the tileNO. result..
For instance it says
"Input the total number or rooms to be tiled:" then I enter "1" "Input the size of the tile in inches" "12" "Input the width of the room in inches (ft and inches seperated bya space)" 12 0 ((((here is where it goes haywire)))))) "Input the legth of the room in inches (ft and inches seperated by a space): Input the total number of rooms to be tiled;"
then it just reapeats each question the same way with the "room to tiled;" right below it.
Sorry, yes, the major "doesn't work" problem is that it will not tell me how many tiles I need per room entered. I can enter the room number, the room size, the tile size, but it just loops on, it never give me the tileNO. result..
For instance it says
"Input the total number or rooms to be tiled:" then I enter "1" "Input the size of the tile in inches" "12" "Input the width of the room in inches (ft and inches seperated bya space)" 12 0 ((((here is where it goes haywire)))))) "Input the legth of the room in inches (ft and inches seperated by a space): Input the total number of rooms to be tiled;"
then it just reapeats each question the same way with the "room to tiled;" right below it.
It also gives me no answer.
First question: does the project restrict how you write the functions? ie are you forced to write specific functions or is that up to you?
First and foremost, you are asking for feet and inches separated by a space, but taking the input using cin...the cin operator will only accept input until the first whitespace.
I have to use functional decompostition to solve, and code the solution using function wherever it makes sense to use them. It has to check for inches grater than 11, number of rooms less than one, and non positive deminsions .
QUOTE(gregoryH @ 13 Oct, 2006 - 04:01 PM)
QUOTE(eadams20 @ 13 Oct, 2006 - 03:46 PM)
Sorry, yes, the major "doesn't work" problem is that it will not tell me how many tiles I need per room entered. I can enter the room number, the room size, the tile size, but it just loops on, it never give me the tileNO. result..
For instance it says
"Input the total number or rooms to be tiled:" then I enter "1" "Input the size of the tile in inches" "12" "Input the width of the room in inches (ft and inches seperated bya space)" 12 0 ((((here is where it goes haywire)))))) "Input the legth of the room in inches (ft and inches seperated by a space): Input the total number of rooms to be tiled;"
then it just reapeats each question the same way with the "room to tiled;" right below it.
It also gives me no answer.
First question: does the project restrict how you write the functions? ie are you forced to write specific functions or is that up to you?
I guess I will ask them to round to the nearest inch.. maybe that will make more sense
QUOTE(Amadeus @ 13 Oct, 2006 - 04:02 PM)
First and foremost, you are asking for feet and inches separated by a space, but taking the input using cin...the cin operator will only accept input until the first whitespace.
I have to use functional decompostition to solve, and code the solution using function wherever it makes sense to use them. It has to check for inches grater than 11, number of rooms less than one, and non positive deminsions .
eadams,
Armadeus made a valid observation.
CODE
cout << "Input the width of room (feet and inches, seperated by" << "a space):" << endl; cin >> roomWidth; cout << "Input the length of room (feet and inches, seperated" << " by a space):" << endl; cin >> roomLength;
This code may behave very badly because the roomWidth included additional data which became roomLength
if you make roomWidth and roomLenght a structure like this:
CODE
struct _roomdim { int feet , inches; } roomLength, roomWidth
your code would change to:
CODE
cout << "Input the width of room (feet and inches, seperated by" << "a space):" << endl; cin >> roomWidth.feet >> roomWidth.inches; cout << "Input the length of room (feet and inches, seperated" << " by a space):" << endl; cin >> roomLength.feet >> roomLength.inches
Which is much easier to pass around and deal with, and fits your instructions correctly
I have to use functional decompostition to solve, and code the solution using function wherever it makes sense to use them.
I see you have three functions, indicating some use of functional decomp.
One of the objectives of coding is to make your code readable and maintainable. If you make more functions, you can easily take care of improved functionality.
eg
int getNumRooms ( ) ; // prevents <=0 or > limit rooms being entered
void getRoomSize( _roomdim & ) ; // confirms dimensions are sensible before returning
int getSquareTileSize ( ); // confirms size is > 0 before returning
int getTilesInBox ( ) ; // all helps to remove user typos....
If a bug crops up, you will know exactly which function to go and fix...
This post has been edited by gregoryH: 13 Oct, 2006 - 04:39 PM
int main() { // Input Variables float roomNumber; float tileSize; int roomLength; int roomWidth; float tileNo;
PrintHeading();
// Prompt for and read Room Number cout << "Input the total number of rooms to be tiled: " << endl; cin >> roomNumber; // the tile size is the same for all rooms // Prompt for and read tile size, room width, room length. cout << "Input the size of tile in inches:" << endl; cin >> tileSize;
// Loop calculating room number while (roomNumber >= 1) { } struct _roomdim; { int feet, inches; roomLength, roomWidth;
} cout << "Input the width of room (feet and inches, seperated by " << "a space):" << endl; cin >> roomWidth.feet >> roomWidth.inches; cout << "Input the length of room (feet and inches, seperated" << " by a space):" << endl; cin >> roomLength.feet >> roomLength.inches;
Compiling... ch7ex2cpp.cpp c:\Documents and Settings\Emily Adams\My Documents\Visual Studio Projects\Ch7ex2cpp\ch7ex2cpp.cpp(45) : error C2228: left of '.feet' must have class/struct/union type type is 'int' c:\Documents and Settings\Emily Adams\My Documents\Visual Studio Projects\Ch7ex2cpp\ch7ex2cpp.cpp(45) : error C2228: left of '.inches' must have class/struct/union type type is 'int' c:\Documents and Settings\Emily Adams\My Documents\Visual Studio Projects\Ch7ex2cpp\ch7ex2cpp.cpp(48) : error C2228: left of '.feet' must have class/struct/union type type is 'int' c:\Documents and Settings\Emily Adams\My Documents\Visual Studio Projects\Ch7ex2cpp\ch7ex2cpp.cpp(48) : error C2228: left of '.inches' must have class/struct/union type type is 'int' c:\Documents and Settings\Emily Adams\My Documents\Visual Studio Projects\Ch7ex2cpp\ch7ex2cpp.cpp(67) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data c:\Documents and Settings\Emily Adams\My Documents\Visual Studio Projects\Ch7ex2cpp\ch7ex2cpp.cpp(68) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
Build log was saved at "file://c:\Documents and Settings\Emily Adams\My Documents\Visual Studio Projects\Ch7ex2cpp\Debug\BuildLog.htm" Ch7ex2cpp - 4 error(s), 2 warning(s)