3. Now prompt for user input for the number of building sections. For now assume the user types in 2 in response to this prompt, as shown below: Choose from among the following options: 1. Exit the program 2. Display building Your choice -> 2 Number of building sections -> 2 Modify your program so that instead of hard-coding each line of output for each section, you use a for or while loop to display the lines in each section. Think of each line as having the following pieces: 1. Number of leading spaces (so that it ends up being centered) 2. The left wall ' 3. Some number of spaces 4. The first angle piece, which should be ' if you are in the top half of the section or should be '" if you are in the bottom half of the section. 5. Some number of spaces 6. The second angle piece, which should be '/' if you are in the top half of the section or should be "' if you are in the bottom half of the section. 7. Some number of spaces 8. The right wall ' Some of the above steps involve displaying multiple spaces. You can do this using a loop, or you can use, for example cout <« setw( 5) <« "|"; which would display the ' |' character right-justified within a field width of 5 characters. Running your program should now give the following output, where all building sections are centered above the bottom section:
Code so far is this.
// @brief Program 1 - ASCII Building
// REPLACE ME AND THE LINES AROUND ME!
// Adapted from prior programs developed by Professor Reed.
/* Running the program looks like:
Choose from among the following options:
1. Exit the program
2. Display building
Your choice -> 1
*/
#include <iostream> // for cin and cout
#include <iomanip> // for setw() and setfill()
using namespace std; // so that we don't need to preface every cin and cout with std::
void printFirstTwoBuildingSection(int n, int startSpacing)
{
int start = n / 2, end = 0;
if (n <= 2)
{
start = 0;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < startSpacing; j++)
{
cout << " ";
}
cout << "|";
if (i < (n / 2))
{
for (int k = 0; k < i; k++)
{
cout << " ";
}
cout << "\\";
for (int k = 0; k < start; k++)
{
cout << " ";
}
cout << "/";
start -= (n / 2);
for (int k = 0; k < i; k++)
{
cout << " ";
}
cout << "|" << endl;
}
else
{
for (int k = n - i - 1; k > 0; k--)
{
cout << " ";
}
cout << "/";
for (int k = 0; k < end; k++)
{
cout << " ";
}
cout << "\\";
end += (n / 2);
for (int k = n - i - 1; k > 0; k--)
{
cout << " ";
}
cout << "|" << endl;
}
}
for (int j = 0; j < startSpacing; j++)
{
cout << " ";
}
cout << "/";
for (int i = 0; i < n; i++)
{
cout << "-";
}
cout << "\\" << endl;
}
int main()
{
int menuOption = 0;
cout << "Choose from among the following options:\n"
<< "1. Exit the program\n"
<< "2. Display building\n"
<< "Your choice -> ";
cin >> menuOption;
cout << endl; // Leave a blank line after getting the user input for the menu option.
// See if exit was chosen
if (menuOption == 1)
{
exit(0);
}
// Menu 2
if (menuOption == 2)
{
cout << " /\\ " << endl;
cout << " || " << endl;
cout << " || " << endl;
cout << " -- " << endl;
cout << " |++|" << endl;
cout << " ====" << endl;
printFirstTwoBuildingSection(2, 1);
printFirstTwoBuildingSection(4, 0);
}
cout << endl;
return 0;
}
Step by step
Solved in 2 steps with 3 images