Hello Tsunami,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
You could do something like this:
1 2 3 4 5 6 7 8 9 10
|
const std::string PROGRAMMER_NAME("Programmer");
std::cout
<< std::left
<< std::setw(100) << std::setfill('*') << "" << std::setfill(' ') << '\n'
<< std::setw(51) << "" << /*std::setw(55) <<*/ "college" << '\n'
<< std::setw(50) << "" << /*std::setw(55) <<*/ "class Y01" << '\n'
<< std::setw(52) << "" << /*std::setw(55) <<*/ "Lab 1" << '\n'
<< std::setw(50) << "" << /*std::setw(55) <<*/ PROGRAMMER_NAME << '\n'
<< std::setw(100) << std::setfill('*') << "" << std::setfill(' ') << "\n\n";
|
This gives the output of:
****************************************************************************************************
college
class Y01
Lab 1
programmer
****************************************************************************************************
Press Enter to continue:
|
Some points:
You do not need separate "cout" statements. All can be chained together in 1 statement.
"std::left" only needs done once. It will affect every "setw" that follows until changed.
All of the "std::setw(55)s" are not needed. Once you use the "setw"s to create your spaces there is no need to to put the type in a block. Just print it out.
I changed the "setw" in line 5 to match line 10.
In lines 6 - 9 I changed the "setw"s to better center what was printed.
Using "PROGRAMMER_NAME" you only have 1 place to make a change when need without having to look through the whole program.
I did try this code to center "Progranner", but it did not work with the previous statements the way they are written.
<< std::setw((50 - (PROGRAMMER_NAME.size() / 2))) << "" << PROGRAMMER_NAME << '\n'
. It has the right idea, but every linw of the heading would have to match to work correctly.
Along with using "code tags" and proper indenting you also need some blank lines to break up the code and make it easier to read.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int main()
{
// Assign the length and width of the room values
int length[] = { 22, 12, 28 };
int width[] = { 18, 12, 30 };
// Constants to define carpet cost , tack cost and install cost
const double CARPET_COST_PER_YARD = 15.49, TACK_COST_PER_FOOT = 0.33, INSTALL_COST_PER_YARD = 4.25;
//Variables to hold calculated values
double carpetCostOfRoom, tackCostOfRoom, installCostOfRoom, totalCostOfRoom;
int perimeterOfRoom, areaOfRoom, squareYardsInRoom;
int numberOfRooms;
cout << fixed << setprecision(2);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int main()
{
// Constants to define carpet cost , tack cost and install cost
constexpr int MAXSIZE{ 3 }; // <--- Size of the arrays.
constexpr double CARPET_COST_PER_YARD{ 15.49 }, TACK_COST_PER_FOOT{ 0.33 }, INSTALL_COST_PER_YARD{ 4.25};
const std::string PROGRAMMER_NAME("Programmer"); // <--- Change as needed.
// Assign the length and width of the room values
int length[MAXSIZE]{ 22, 12, 28 }; // <--- The "=" is not needed.
int width[MAXSIZE]{ 18, 12, 30 };
//Variables to hold calculated values
double carpetCostOfRoom{}, tackCostOfRoom{}, installCostOfRoom{}, totalCostOfRoom{};
int perimeterOfRoom{}, areaOfRoom{}, squareYardsInRoom{};
int numberOfRooms{};
std::cout << std::fixed << std::setprecision(2);
}
|
Probably a personal choice, but I like to put constant variables at the beginning. I think it is quicker to find, but not required.
It is always a good idea to initialize your variables when they are defined.Especially any variable used for a total. You do not want to have "totalCostOfRoom" start with a value of something like "-9.2559631349317831e+64". Your code appears to overwrite the variable, but something like
totalCostOfRoom += something;
would not work.
Also initializing the variables gives you the piece of mind to know that the variables start with zero and not a garbage value.
Other parts of the code are to give you an idea of what you could do.
Andy