Setfill and repeat

I am working on a lab and my code works with no errors. The problem is, she wants setfill used for dividers, instead of setw/string, which is what I have. I am supposed to show calculations for 3 rooms, which I do, but she doesn't want it looped, it needs to be repeated. I'm confused which part of the code I should
copy for each room. Any help would be appreciated.

//---------------------------------------GRADING BLOCK---------------------------------------------
//Programmer's Name: name
//Program: Lab 1
//Grade: 0/100
//Professor's Comments:
//
//
//
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------IPO BLOCK---------------------------------------------------
//Programmer's Name: programmer
//Program: Lab 1
//Program Flow (IPO):
//
//Set formatting to 2 decimals for floating point values on the screen
//Ouput the divider to the screen
//Output the course heading to the screen
//Output the divider to the screen
//Move the output pointer down 1 line on the screen
//Output the column headings to the screen
//Output the divider to the screen
//Initialize the number of rooms
//
//Repeat for each of 3 rooms:
// Increment the number of rooms
// Assign the length & width of the room values
// Calculate the area
// Calculate the perimeter
// Calculate the square yards
// Calculate the carpet costs
// Calculate the cost of the tack strip
// Calculate the installation charge
// Calculate the total cost of carpeting this room
// Output the room count, length & width to the screen
// Output the area, perimeter & square yards to the screen
// Output the carpet cost, the cost of the tack strip, the cost of installation
// & the total cost of carpeting this room
//End Repeat
//
// Output the divider to the screen
//-------------------------------------------------------------------------------------------------

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const string COLLEGE = "college";
const string COURSE = "class";
const string LAB_NAME = "Lab 1";
const string PROGRAMMER_NAME = "programmer";

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);

//Display Heading
cout << left << setw(110) << string(110,'*') << endl;
cout << left << setw(50) << "" << left << setw(55) << "college" << endl;
cout << left << setw(50) << "" << left << setw(55) << "class Y01" << endl;
cout << left << setw(53) << "" << left << setw(55) << "Lab 1" << endl;
cout << left << setw(50) << "" << left << setw(55) << "programmer" << endl;
cout << left << setw(100) << string(110, '*') << "" << endl << endl;

cout << right << setw(10) << "Room #" << right << setw(10) << "Length" << right << setw(10) << "Width" << right << setw(10) << "Area" << right << setw(15) << "Perimeter"
<< right << setw(10) << "Square" << right << setw(10) << "Carpet" << right << setw(10) << "Tack" << right << setw(10) << "Install" << right << setw(10) << "Total" << endl;
cout << left << setw(55) << "" << right << setw(10) << "Yards" << right << setw(10) << "Cost" << right << setw(10) << "Cost" << right << setw(10) << "Cost" << endl;
cout << left << setw(100) << string(110, '*') << "" << endl << endl;
numberOfRooms = 0;
// loop 3 times
while (numberOfRooms < 3)
{
numberOfRooms++;
// calculate area of room in sq ft.
areaOfRoom = length[numberOfRooms - 1] * width[numberOfRooms - 1];
// calculate area of room in ft
perimeterOfRoom = 2 * (length[numberOfRooms - 1] + width[numberOfRooms - 1]);
// calculate area of room in sq. yards
squareYardsInRoom = areaOfRoom / 9;
// calculate carpet costs
carpetCostOfRoom = squareYardsInRoom*CARPET_COST_PER_YARD;
// calculate cost of the tack strip
tackCostOfRoom = perimeterOfRoom*TACK_COST_PER_FOOT;
// calculate the installation charge
installCostOfRoom = squareYardsInRoom*INSTALL_COST_PER_YARD;
// calculate total cost of carpeting this room
totalCostOfRoom = carpetCostOfRoom + tackCostOfRoom + installCostOfRoom;
// Output the room count, length & width to the screen
cout << right << setw(10) << numberOfRooms << right << setw(10) << length[numberOfRooms - 1] << right << setw(10) << width[numberOfRooms - 1] << right << setw(10)
// Output the area, perimeter & square yards to the screen
<< areaOfRoom << right << setw(15) << perimeterOfRoom << right << setw(10) << squareYardsInRoom << right << setw(10) << carpetCostOfRoom
// Output the carpet cost, the cost of the tack strip, the cost of installation & the total cost of carpeting this room
<< right << setw(10) << tackCostOfRoom << right << setw(10) << installCostOfRoom << right << setw(10) << totalCostOfRoom << endl;
}
// display the divider
cout << left << setw(100) << string(110, '*') << "" << endl;
return 0;


}
You can easily replace string(110, '*') with setfill('*'). See:
1
2
//Display Heading
cout << left << setw(110) << setfill('*') << '\n' << setfill(' ') << endl;
Note that you need to reset it to space when space is required.

Do you know how to create functions?

And please use code tags.
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
I appreciate everybody's help. It's my second lab in my online Into to Programming class, and we have little guidance. I'll make sure to use code tags next time I post. Again, thank you, it has been really helpful.
Hello Tsunami,

Now that I have had the chance to work with all the code I came up with this. Look at it more as a guide of possibilities and not something that you need to change to.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <iomanip>
#include <string>

//using namespace std;  // <--- Best not to use, but OK for now.

int main()
{
    // Constants to define carpet cost , tack cost and install cost
    constexpr int MAXSIZE{ 5 };  // <--- Used for size of array and in other places, like the while loop.
    constexpr int WIDTH{ 110 };  // <--- Width of output to the screen.

    const std::string COLLEGE_NAME{ "college" };
    const std::string COURSE_NAME{ "class" };
    const std::string LAB_NAME{ "Lab 1" };
    const std::string PROGRAMMER_NAME{ "programmer" };

    constexpr double CARPET_COST_PER_YARD{ 15.49 }, TACK_COST_PER_FOOT{ 0.33 }, INSTALL_COST_PER_YARD{ 4.25 };

    // Assign the length and width of the room values
    int roomLength[MAXSIZE]{ 22, 12, 28 };
    int roomWidth[MAXSIZE]{ 18, 12, 30 };
    double grandTotal{};  // <--- Added.

    //Variables to hold calculated values
    int numberOfRooms{};

    std::cout << std::fixed << std::setprecision(2);

    //Display Heading
    std::cout
        << std::left
        << '\n' << std::setfill('*') << std::setw(WIDTH) << "" << std::setfill(' ') << "\n\n"
        << std::setw(((WIDTH / 2) - (COLLEGE_NAME.size() / 2))) << "" << COLLEGE_NAME << '\n'
        << std::setw(((WIDTH / 2) - (COURSE_NAME.size() / 2))) << "" << COURSE_NAME << '\n'
        << std::setw(((WIDTH / 2) - (LAB_NAME.size() / 2))) << "" << LAB_NAME << '\n'
        << std::setw(((WIDTH / 2) - (PROGRAMMER_NAME.size() / 2)) + 1) << "" << PROGRAMMER_NAME << "\n"
        << '\n' << std::setfill('*') << std::setw(WIDTH) << "" << std::setfill(' ') << "\n\n";;

    std::cout
        << std::setw(62) << "" << std::setw(10) << "Square" << std::setw(11) << "Carpet" << std::setw(9) << "Tack" << std::setw(8) << "Instal" << '\n'

        << std::setw(15) << "   Room #" << std::setw(12) << "Length" << std::setw(10) << "Width" << std::setw(10) << "Area" << std::setw(15) << "Perimeter"
        << std::setw(11) << "Yards" << std::setw(10) << "Cost" << std::setw(10) << "Cost" << std::setw(11) << "Cost" <<  std::setw(10) << "Total" << '\n'

        << std::setfill('*') << std::setw(WIDTH) << "" << std::setfill(' ') << "\n";;

    //numberOfRooms = 0;  // <--- Not needed if initialized when defined.

    // loop 3 times
    while (numberOfRooms < MAXSIZE && roomLength[numberOfRooms] != 0)
    {
        double carpetCostOfRoom{}, tackCostOfRoom{}, installCostOfRoom{};

        // calculate area of room in sq ft.
        int areaOfRoom = roomLength[numberOfRooms] * roomWidth[numberOfRooms];

        // calculate area of room in ft
        int perimeterOfRoom = 2 * (roomLength[numberOfRooms] + roomWidth[numberOfRooms]);

        // calculate area of room in sq. yards
        int squareYardsInRoom = areaOfRoom / 9;

        // calculate carpet costs
        double totalCostOfRoom = carpetCostOfRoom = squareYardsInRoom * CARPET_COST_PER_YARD;

        // calculate cost of the tack strip
        totalCostOfRoom += tackCostOfRoom = perimeterOfRoom * TACK_COST_PER_FOOT;

        // calculate the installation charge
        totalCostOfRoom += installCostOfRoom = squareYardsInRoom * INSTALL_COST_PER_YARD;

        // Output the room count, length & width to the screen
        std::cout
            << std::right
            << std::setw(7) << numberOfRooms + 1
            << std::setw(13) << roomLength[numberOfRooms]
            << std::setw(11) << roomWidth[numberOfRooms]

            // Output the area, perimeter & square yards to the screen
            << std::setw(10) << areaOfRoom
            << std::setw(12) << perimeterOfRoom
            << std::setw(12) << squareYardsInRoom
            << std::setw(13) << carpetCostOfRoom

            // Output the carpet cost, the cost of the tack strip, the cost of installation & the total cost of carpeting this room
            << std::setw(9) << tackCostOfRoom
            << std::setw(11) << installCostOfRoom
            << std::setw(12) << totalCostOfRoom << '\n';

        grandTotal += totalCostOfRoom;

        numberOfRooms++;
    }

    // display the divider
    std::cout << std::setfill('*') << std::setw(WIDTH) << "" << std::setfill(' ') << "\n";  // <--- "left" or "right" here makes no difference.

    std::cout
        << std::left
        << std::setw(WIDTH + 2) << "Grand Total" << std::right << std::setw(8) << grandTotal;

    return 0;
}


The output it produces is:

**************************************************************************************************************

                                                    college
                                                     class
                                                     Lab 1
                                                   programmer

**************************************************************************************************************

                                                              Square    Carpet     Tack     Instal
   Room #      Length      Width     Area      Perimeter      Yards      Cost      Cost      Cost       Total
**************************************************************************************************************
      1           22         18       396          80          44       681.56    26.40     187.00      894.96
      2           12         12       144          48          16       247.84    15.84      68.00      331.68
      3           28         30       840         116          93      1440.57    38.28     395.25     1874.10
**************************************************************************************************************
Grand Total                                                                                            3100.74


If you have any questions let me know.

Andy
Topic archived. No new replies allowed.