Hi everyone,
I will try to give you a breakdown of what my assignment is in the fastest terms. I need to write a program where I create my own functions and use single while loops. The user is to enter a "user choice," which is only supposed to be 1 or 2. If the user picks 1, then the user is supposed to enter a height and a symbol as well, and then the program is supposed to print a right triangle out of the symbol and height entered. If the user picks 2, then they also enter the symbol and height, but also the width. This choice is supposed to print a rectangle. If the user picks something other than 1 or 2, then the program is supposed to provide an error message and ask again UNTIL the user enters either 1 or 2. In addition, only the first function I create can have inputs, after that the other functions can only have single while loops and no inputs. I know that this is a lot of information, but I'm just trying to be as detailed as possible.
Here is my code:
Code:
#include <iostream>
using namespace std;
// variable declarations
int height; // used to store the height
int width; // used to store the width
int userChoice; // used to store the user's choice
int num; // used to store the number of symbols
char symbol; // used to store the symbol the user enters
// function prototypes
void DisplayMenu();
void DrawLineOfSymbol(int num, char symbol);
void DrawTriangle(int height, char symbol);
void GetRectangleDimension(int& height, int& width);
void DrawRectangle(int height, int width);
// funtion definitions
void DisplayMenu() // function heading
// Precondition: None
// Postcondition: Menu is printed
// function body for DisplayMenu
{
// display a menu and read and process the user's choice
cout << "______________________________________" << endl;
cout << " " << endl;
cout << "This program reads and processes the" << endl
<< "user's choice until the user wants to" << endl
<< "quit." << endl;
cout << "______________________________________" << endl;
cout << " " << endl;
cout << "Please enter your choice (1 or 2): " << endl;
cin >> userChoice;
cout << "Please enter the number of symbols: " << endl;
cin >> num;
// function call for DrawLineOfSymbol function
//DrawLineOfSymbol(int num, char symbol);
// get height and symbol if user choice is 1
if (userChoice == 1)
{
cout << "Please enter the height: " << endl;
cin >> height;
cout << "You entered: " << height << endl;
cout << "Please enter the symbol: " << endl;
cin >> symbol;
cout << "You entered: " << symbol << endl;
DrawTriangle(height, symbol); // function call
}
// call DrawRectangle function if user choice is 2
if (userChoice == 2) // Function call for DrawRectangle Function
{
GetRectangleDimension(height, width);
DrawRectangle(height, width);
}
// Print error message if user choice is not 1 or 2
if (userChoice !=1 || userChoice !=2)
{
cout << "Invalid choice. Please try again." << endl;
}
}
void DrawLineOfSymbol(int num, char symbol) // function heading
// Precondition: DisplayMenu function has been called
// Postcondition: Prints line of symbols
// Function body for DrawLineOfSymbol
// Pass-by-reference parameter
{
while (num > 0) // test
{
cout << symbol;
num--; // update
}
}
void DrawTriangle(int height, char symbol) // function heading
// Precondition: userChoice == 1
// Postcondition: Draws right triangle of symbol
// Function body for DrawTriangle
// Pass-by-reference parameter
{
int counter = 0; // initalize counter
while(counter <= height)
{
DrawLineOfSymbol(num, symbol);
counter++;
}
}
void GetRectangleDimension(int& height, int& width) // function heading
// Precondition: userChoice == 2
// Postcondition: Gets the dimension of a rectangle from the user
// Function body for GetRectangleDimension
// Pass-by-reference parameter
{
cout << "Please enter the height: " << endl;
cin >> height;
cout << "You entered: " << height << endl;
cout << "Please enter the width: " << endl;
cin >> width;
cout << "You entered: " << width << endl;
}
void DrawRectangle(int height, int width) // function heading
// Precondition: userChoice == 2
// Postcondition: Draws a rectangle of the user's symbol and dimensions
// Function body for DrawRectangle
// Pass-by-reference parameter
{
int counter = 0; // initalize counter
while(counter <= height)
{
DrawLineOfSymbol(num, symbol);
counter++;
}
}
// Main function
int main()
{
DisplayMenu(); // function call
return 0;
}
This code is just a work in progress, so I probably have some additional errors, but the first one I am running into is that it says that I am not entering a valid symbol in my output. Here is my output:
Code:
______________________________________
This program reads and processes the
user's choice until the user wants to
quit.
______________________________________
Please enter your choice (1 or 2):
1
Please enter the number of symbols:
6
Please enter the height:
5
You entered: 5
Please enter the symbol:
*
You entered: *
************************************Invalid choice. Please try again.
Can someone please help me with my code and show me the right way to have this so it outputs correctly? Thank you for helping me!