Create a menu-driven application that takes the length and width of a square as
ID: 3869438 • Letter: C
Question
Create a menu-driven application that takes the length and width of a square as input and returns either the area, perimeter, or a drawing of a square on the screen. This application should use the following functions: void getInput() should ask the user for the length and width and store them in reference parameter variables. This function should be called once by main. void displayMenu() should display the following text: COSC 1436 SQUARE PROGRAM ------------------------------------------------- 1) Calculate Perimeter 2) Calculate Area 3) Draw Square 4) Exit ------------------------------------------------- Enter choice [1 – 4] : int calculateArea() should calculate and return the area of a square. This function should be called by main and should be passed length and width. int calculatePerimeter() should calculate and return the perimeter of a square. This function should be called by main and should be passed length and width. void drawSquare() should display a pattern that represents a square using the length and width given by the user. This function should be called by main and should be passed length and width. For example, given a length of 5 and width of 3, the following output using nested for loops should be displayed: Length: 5 Width: 3 ***** ***** ***** int main() should do the following: 1. Create all appropriate variables. 2. Get input from the user by calling the getInput() function. 3. Display the menu, and get choice from the user. 4. Based on user choice, call the appropriate function. HINT: you may use a switch 5. Clear the screen 6. While choice does not equal 4, repeat steps 3, 4, and 5 using sentinel-controlled loop
Explanation / Answer
#include <iostream>
using namespace std;
void getInput(int &l, int &w){
cout<<"Enter the length: ";
cin >>l;
cout<<"Enter the widith: ";
cin >>w;
}
void displayMenu() {
cout<<"COSC 1436 SQUARE PROGRAM 1) Calculate Perimeter 2) Calculate Area 3) Draw Square 4) Exit Enter choice [1 – 4] :"<<endl;
}
int calculateArea(int l, int w) {
return l * w;
}
int calculatePerimeter(int l, int w) {
return 2 * (l+w);
}
void drawSquare(int l, int w) {
for(int i=0;i<w;i++) {
for(int j=0;j<l;j++){
cout<<"* ";
}
cout<<endl;
}
}
int main()
{
int length, width;
int choice;
getInput(length, width);
displayMenu();
cin >> choice;
while(choice != 4) {
switch(choice) {
case 1: cout<<"Perimeter is " <<calculatePerimeter(length, width)<<endl;break;
case 2: cout<<"Area is "<<calculateArea(length, width)<<endl;break;
case 3: drawSquare(length, width);break;
}
displayMenu();
cin >> choice;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the length: 2
Enter the widith: 3
COSC 1436 SQUARE PROGRAM
1) Calculate Perimeter 2) Calculate Area 3) Draw Square 4) Exit
Enter choice [1 – 4] :
1
Perimeter is 10
COSC 1436 SQUARE PROGRAM
1) Calculate Perimeter 2) Calculate Area 3) Draw Square 4) Exit
Enter choice [1 – 4] :
2
Area is 6
COSC 1436 SQUARE PROGRAM
1) Calculate Perimeter 2) Calculate Area 3) Draw Square 4) Exit
Enter choice [1 – 4] :
3
* *
* *
* *
COSC 1436 SQUARE PROGRAM
1) Calculate Perimeter 2) Calculate Area 3) Draw Square 4) Exit
Enter choice [1 – 4] :
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.