James B.
asked 10/17/21You must code this in c++. I have Included the full question in description.
Write a program that prompts users to enter:
o A character to use (any character besides *)
o The width of the display
o The height of the display
to produce an E shape. The middle horizontal line must print at line (height+1)/2. Validate user input to only be positive values and height must be at least 5 and width 2. Any invalid input must loop until a valid value is entered.
This is an example for width of 10 and height of 9
**********
*
*
*
**********
*
*
*
**********
You must use separate functions to display the vertical and the horizontal lines.
1 Expert Answer
#include <iostream>
using namespace std;
//function to get the character
char getCharacter() {
char ch;
while (true) {
cout << "Enter a character to use (any character besides *): ";
cin >> ch;
if (ch != '*') {
return ch;
}
cout << "Invalid input. Please enter a character that is not '*'." << endl;
}
}
int getPositiveInteger(const string& prompt, int minValue) {
int value;
while (true) {
cout << prompt;
cin >> value;
if (cin.fail() || value < minValue) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a positive integer of at least " << minValue << "." << endl;
} else {
return value;
}
}
}
void displayHorizontalLine(char ch, int width) {
for (int i = 0; i < width; ++i) {
cout << ch;
}
cout << endl;
}
void displayVerticalLine(char ch, int width, int height) {
for (int i = 0; i < height; ++i) {
cout << ch;
for (int j = 1; j < width; ++j) {
cout << ' ';
}
cout << endl;
}
}
void displayEShape(char ch, int width, int height) {
int middleLine = (height + 1) / 2;
displayHorizontalLine(ch, width);
for (int i = 1; i < height; ++i) {
if (i == middleLine - 1) {
displayHorizontalLine(ch, width);
} else {
displayVerticalLine(ch, width, 1);
}
}
displayHorizontalLine(ch, width);
}
int main() {
char ch = getCharacter();
int width = getPositiveInteger("Enter the width of the display (minimum 2): ", 2);
int height = getPositiveInteger("Enter the height of the display (minimum 5): ", 5);
displayEShape(ch, width, height);
return 0;
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Preston Z.
Are you still looking for help with this coding problem? I would be happy to help you in a tutoring session, but as this is a direct coding problem I do not want to just post the completed code for you.11/01/21