Hello CheesyForde1,
My first tip is not to change anything in what you have posted, but to post a new message. It tends to confuse people and makes the thread hard to follow. That said adding the code to your original post, OP as you will later see in other messages which can refer to either the original message or the person, in this case is not a problem.
Do not feel bad about your code I have seen worse. To that I offer this suggestion:
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 86
|
#include <iostream>
#include <iomanip> // <--- For "std::setprecision" and "std::setw".
#include <limits>
#include <cmath>
//using namespace std; // <--- Best not to use.
int main()
{
constexpr double GROUT{ 25.0 };
constexpr double PRICE_GROUT{ 60.0 };
constexpr double THINSET{ 50.0 };
constexpr double PRICE_THINSET{ 45.0 };
constexpr double SQUAREFOOT(144.0); //<--- Added.
constexpr double AREA_COVERED_BY_THINSET{ 90.0 }; //<--- Added.
constexpr double SQR_FOOT_GROUT_COVERAGE{ 0.13 }; //<--- Added.
double feet_long{}, feet_wide{}, inches_long{}, inches_wide{};
double /*grout{}, */area{}, area_tile{}/*, price_grout{}*/;
//double thinset{};
//double price_thinset{};
double number_tiles{};
double one_tile{};
double total{};
double bags_thinset{};
double pound_grout{};
std::cout << "Please enter the length of the room: ";
std::cin >> feet_long;
std::cout << "Please enter the width of the room: ";
std::cin >> feet_wide;
std::cout << "Please enter the size of the tile: "; // <--- Changed.
std::cin >> inches_long;
inches_wide = inches_long; // <--- Changed.
//std::cout << "Please enter the width of the tile: ";
//std::cin >> inches_wide;
std::cout << "Cost of one tile ";
std::cin >> one_tile;
area = feet_long * feet_wide;
area_tile = inches_long * inches_wide;
number_tiles = (area * SQUAREFOOT) / area_tile;
bags_thinset = ceil(area / AREA_COVERED_BY_THINSET) * 50;
pound_grout = ceil(SQR_FOOT_GROUT_COVERAGE * area_tile);
total = (one_tile * number_tiles) + (PRICE_GROUT * pound_grout) + (PRICE_THINSET * bags_thinset);
std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Added.
std::cout << '\n' << std::setw(20) << "Length of the room: " << feet_long << " feet" << std::endl;
std::cout << std::setw(20) << "Width of the room: " << feet_wide << " feet" << std::endl;
std::cout << '\n' << std::setw(20) << "Length of the tile: " << inches_long << " inches" << std::endl;
std::cout << std::setw(20) << "Width of the tile: " << inches_wide << " inches" << std::endl;
std::cout << '\n' << std::setw(20) << "Area of the room: " << area << " square feet" << std::endl;
std::cout << std::setw(20) << "Area of the tiles: " << area_tile << " square inches" << std::endl;
std::cout << std::setw(20) << "Number of tiles: " << number_tiles << std::endl;
std::cout << '\n' << std::setw(20) << "Pounds of Grout needed: " << pound_grout << std::endl;
std::cout << std::setw(20) << "Bags of Thinset needed: " << bags_thinset << std::endl;
std::cout << '\n' << std::setw(20) << "Total: " << total << std::endl;
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
|
At the top of the program are all the "#include" files. If you do enough reading here and elsewhere you will see that the "#include"s generally come first.
Next is usually any header file that is wrapped in Quotes ("").
You should learn to avoid using line 6 as it
WILL get you in trouble some day. As you read through the program you will see how and what I qualified in the standard name space. Right now it would seem to make coding easier, but it does not in the future.
Inside "main" I created constant variables with the key work "constrxpr". If this is a problem for your compiler the older version of "const" works just fine. Also notice that the name is in all caps. This helps to remind you that the variable is a constant that can not be changed in the program. The compiler will also give you an error if you try to change a constant variable.
Also notice the empty {}s after the variable name. This is the uniform initializer. The empty {}s will initialize the variable to zero. In this case a double would end up as 0.0.
Lines 19 and 20 are just another way of defining several variables on one line to save some space. If you do not have many variables I suggest using one line per variable. Some variables do not really need initialized because the next part of the program will use "cin" to give them a value, but I like to initialize a variable when it is defined just to be safe.
The variables that are commented out I left to show you that they are no longer needed because they are replaced by the constant variables.
In the input section I changed the input for tile dimensions to just one input then set the width equal to the length. This way you do not have to change the program later to deal with just one number. As far as I have ever seen tiles are square, so one number will fit all sides. If you need to enter it twice it is easy to change back.
In the section for the calculations the two for area are OK, but I am nor sure about the rest as of now. In your original code you used what is call "magic numbers". It does work, but not the best way to write a program. As you can see in my code I have replaced these "magic numbers" with the constants that I defined at the beginning of the function. The only line I am not sure of is
bags_thinset = ceil(area / AREA_COVERED_BY_THINSET) * 50;
. I am not sure what to call a variable for the "50" or even what it is used for right now.
The last section is the "cout" statements. I made some changes to these lines to format what is displayed on the screen, but this is good for testing, but it does not create the invoice that the instructions ask for. This can easily fixed.
The last four lines before the "return" is what I sue to keep the console window open when I run the program from my IDE. You can use it or disregard it if you do not need it.
Last point. The way you wrote your original code as one block is OK the compiler does not case. If you look at my code the blank lines break up the code and make it easier to read. The easier it is to read the easier it is to work with. And if you add a comment line for each section it will help everyone to find things. Sorry the only part i did not think of at the time i was working on the program.
Hope that helps,
Andy