Write an interactive C++ program that gets a single input of an integer. Use nes
ID: 3911865 • Letter: W
Question
Write an interactive C++ program that gets a single input of an integer. Use nested repetition control structures better known as "loops" to output a hollow square shape using just the specific characters +, - and |, similar to the sample output provided above. Notice that the number of characters (ie., width and height) is specified by the user input of a single integer value. Don't use the setw manipulator or output hard-coded strings containing a rigid number of specific characters and spaces – all elements must be created dynamically using loops!
Your task is to achieve the following goals:
ask the user to enter a positive integer value greater than 2 (n);
check the entered value and if it isn't legal, output a message stating the reason;
draw a square with a side of n using the characters +, - and | (see sample output above)
Find a reasonable upper limit for n (depending on your screen's dimensions or an acceptable range) and embed it into your input validation code.
Input Validation: Ensure that the user inputs a positive integer value greater than 2 and does not exceed an acceptable range determined by you. Do not accept negative or non numeric values.
Drawing Squares Program * Algorithm generates a hollow square, using the character +, - and|* *Acceptable size dimension: Any value from 3 to 20. Choose carefully. Side size 3 To try my shape generator program again type Y for Yes and N for No: Y Side size2 OOPS! Looks like you typed some bad data here! The acceptable dimension should range from 3 to 20, so choose carefully... side size = 21 OOPS! Looks like you typed some bad data here! The acceptable dimension should range from 3 to 20, so choose carefully... Side size -Ada Lovelace OOPS! Looks like you typed some bad data here! The acceptable dimension should range from 3 to 20, so choose carefully... Side size-8 To try my shape generator program again type Y for Yes and N for No: N Now exiting the shape generator program....... Process returned 0 (0x0) execution time 46.838 s Press any key to continue.Explanation / Answer
#include <iostream>
using namespace std;
int main() {
char choice = 'y';
while(choice == 'y')
{
// TAKING user input and validating against the conditions we have
int n,i,j;
cout << " Side size = ";
cin >> n;
while(cin.fail() || n<3 || n >20)
{
cin.clear();
cin.ignore(256,' ');
cout << " OOPS! Looks like you typed some bad data here! The acceptable dimension should range from 3 to 20, so choose carefully... Side size = ";
cin >> n;
}
// first line
cout << "+";
for(i=0; i<n-2; i++)
cout << "-";
cout << "+"<< endl;
// middle part
for(i=0; i<n-2; i++)
{
cout << "|";
for(j=0; j<n-2; j++)
cout << " ";
cout << "|" << endl;
}
// last line
cout << "+";
for(i=0; i<n-2; i++)
cout << "-";
cout << "+"<< endl;
// asking for choice of doing it again
cout << " To try my shape generator program again type y for Yes and N for No: ";
cin >> choice;
}
}
/*SAMPLE OUTPUT
Side size = 3
+-+
| |
+-+
To try my shape generator program again type y for Yes and N for No: y
Side size = 2
OOPS! Looks like you typed some bad data here!
The acceptable dimension should range from 3 to 20, so choose carefully...
Side size = chegg
OOPS! Looks like you typed some bad data here!
The acceptable dimension should range from 3 to 20, so choose carefully...
Side size = 24
OOPS! Looks like you typed some bad data here!
The acceptable dimension should range from 3 to 20, so choose carefully...
Side size = 8
+------+
| |
| |
| |
| |
| |
| |
+------+
To try my shape generator program again type y for Yes and N for No: n
*/
// Note: You can run this code online using link https://repl.it/@sindhuvegi/or-box-between-n-of-3-and-20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.