• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C++: "Less Than 30 Lines"

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!

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.";
}
 
If you want to keep doing it the way you're doing it, you can remove all the inputPrompt = stuff, and just take your string and put it in the function. You can also remove all those string variables by just harcoding in the string in the function call.
 
Move all of the user input code out of main? I'm assuming this requirement is to prevent people from writing one monolithic main function. -- Oh all functions must be less than 30 lines. Just try to find a semi logical division in your input function.

Yeah, just move your input routines into an input function and dump the weird handling of string constants.
 
just remove all the line breaks from your source code file before turning it in ;)
 
So, nothing, then.

Even though this is a simple example, the opportunities are here to learn something about reusable code, parameter passing, factoring, efficiency, software metrics, and so on.
 
Ha... thanks for your help everyone... I even took out a few line breaks ;)

Here's my final code.
Code:
#include <iostream>
#include <iomanip>
#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,
      Boxes, Extra, TotalTilesCounter = 0;

   SizeOfTile = GetPositiveInteger("Enter size of tile in inches: ", 
      "Invalid size of tile in inches: ", false);
   NumberOfRooms = GetPositiveInteger("Enter number of rooms: ", 
      "Invalid number of rooms: ", false);
   cout << endl;

   for (RoomCounter = ZERO; RoomCounter < NumberOfRooms; RoomCounter++)
   {
      RoomWidthFeet = GetPositiveInteger("Enter room width feet: ", 
         "Invalid room width feet: ", false);
      RoomWidthInches = GetPositiveInteger("Enter room width inches: ", 
         "Invalid room width inches: ", true);
      RoomLengthFeet = GetPositiveInteger("Enter room length feet: ", 
         "Invalid room length feet: ", false);
      RoomLengthInches = GetPositiveInteger("Enter room length inches: ", 
         "Invalid room length inches: ", true);
      TilesForRoom = NumberOfTilesForRoom(SizeOfTile, RoomWidthFeet, 
         RoomWidthInches, RoomLengthFeet, RoomLengthInches);
      TotalTilesCounter += TilesForRoom;
      cout << "Room requires " << TilesForRoom << " tiles. \n \n";
   }
   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.";
}
 
I find it interesting that they are putting a set limit like that for the length of your functions. I guess they are trying to teach you to subdivide your code into reusable chunks, but I'd rather have to read more lines in a function than having an overuse of them.
 
I find it interesting that they are putting a set limit like that for the length of your functions. I guess they are trying to teach you to subdivide your code into reusable chunks, but I'd rather have to read more lines in a function than having an overuse of them.

I think now they'll learn a good, logical way to subdivide.

However, I must tell the OP that it looks good. You met the requirements nicely. Just make sure you understand that its good to make your code reusable like this, but not necessarily setting up requirements like this for yourself. You'll learn more about it later.
 
Small point - people usually have their variables with the first letter non capitalised "camel case"
Even in the world of ides, having conventions such as this help people to understand code more easily.
 
Back
Top