Question

In: Computer Science

Design c++ program so that it correctly meets the program specifications given below.   Specifications: Create a...

  1. Design c++ program so that it correctly meets the program specifications given below.  

Specifications:

Create a menu-driven program that finds and displays areas of 3 different objects.

The menu should have the following 4 choices:

1 -- square

2 -- circle

3 -- right triangle

4 -- quit

  • If the user selects choice 1, the program should find the area of a square.
  • If the user selects choice 2, the program should find the area of a circle.
  • If the user selects choice 3, the program should find the area of a right triangle.
  • If the user selects choice 4, the program should quit without doing anything.
  • If the user selects anything else (i.e., an invalid choice) an appropriate error message should be printed.

Sample Run

Program to calculate areas of objects

        1 -- square

        2 -- circle

        3 -- right triangle

        4 -- quit

2

Radius of the circle: 3.0

Area = 28.2743

Solutions

Expert Solution

I have written two codes separately with output-

code 1- if you want the menu to appear again instead of program getting exit just after one choice this is your code.

#include<iostream>
#include<stdlib.h>
using namespace std;
  

int main()
{
int choice; //choice variable for the options to choose
   float radius,area,length,width; //variable used for area calculations

   do //do loop begins from here
{

   std::cout << "Please to calculate areas of objects\n"; //menu outputs
   std::cout << "1 -- Square\n";
   std::cout << "2 -- Circle\n";
   std::cout << "3 -- Right triangle\n";
   std::cout << "4 -- Quit\n";

std::cin >> choice; //choice input from the menu

   switch(choice) //switch case for doing the operqations on the basis of user choice
{
case 1:   
std::cout << "Enter length of square: ";
std::cin >> length;
std::cout << "Area=" << length*length << "\n\n";
break;
case 2:
std::cout << "Enter the radius of the circle: ";
std::cin >> radius;
area = 3.142 * radius * radius;
cout << "Area="<<area<< "\n\n";
break;
case 3:
std::cout << "Enter the length of the right triangle:";
std::cin >> length;
std::cout << "Enter the width of the right triangle: ";
std::cin >> width;
std::cout << "Area=" << length*width/2 << "\n\n";
break;

case 4:
exit(0);
break;

default: //invalid choice message
cout<<"Invalid choice"<< "\n\n";
}

} while(choice!=4); //while with condition or quit option number 4
return 0;
}


code 2- if you don't want the menu to reappear and program should just execute one operation and exit.

#include<iostream>
#include<stdlib.h>
using namespace std;
   int main()
{
int choice;   //choice variable for the options to choose
   float radius,area,length,width;   //variable used for area calculations


   std::cout << "Please to calculate areas of objects\n"; //menu outputs
   std::cout << "1 -- Square\n";
   std::cout << "2 -- Circle\n";
   std::cout << "3 -- Right triangle\n";
   std::cout << "4 -- Quit\n";

std::cin >> choice; //choice input from the menu

   switch(choice)
{
case 1:
std::cout << "Enter length of square: ";
std::cin >> length;
std::cout << "Area=" << length*length << "\n\n";
break;
case 2:
std::cout << "Enter the radius of the circle: ";
std::cin >> radius;
area = 3.142 * radius * radius;
cout << "Area="<<area<<"\n\n";
break;
case 3:
std::cout << "Enter the length of the right triangle:";
std::cin >> length;
std::cout << "Enter the width of the right triangle: ";
std::cin >> width;
std::cout << "Area=" << length*width/2 << "\n\n";
break;

case 4:
exit(0);
break;

default:
cout<<"Invalid choice"<<"\n\n";    //invalid choice message
}


return 0;
}


If you still have any query reguarding this code, do reply.


Related Solutions

Create a C++ program that follows the specifications below: *Define a struct with 4 or more...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more members. *Application must have at least one user-defined function *Declare an array of your struct using a size of 10 or more *Load the date for each element in your array from a text file *Display the data in your array in the terminal *provide brief comments for each line of code
Design a comb filter in MATLAB that meets the following specifications: it eliminates 5 harmonics of...
Design a comb filter in MATLAB that meets the following specifications: it eliminates 5 harmonics of 100Hz it eliminates dc (zero frequency) |h(t)| <= 0.001 for t> 0.5
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: · Your C++ source code file. (The file with the .CPP extension).No other files will be accepted. A screenshot of your program running. Program Instructions: Consider the following incomplete C++ program: #include int main() { … } 1. Write a statement that includes the header files fstream, string, and iomanip in this program. 2. Write statements that declare inFile to be an ifstream variable and...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. Student has a String name, a double GPA, and a reasonable equals() method. Course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses binary file I/O to save and retrieve Students and Lists of Students. CoursePersister does...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. The student has a String name, a double GPA, and a reasonable equals() method. The course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses the binary file I/O to save and retrieve Students and Lists of...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. The student has a String name, a double GPA, and a reasonable equals() method. The course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses the binary file I/O to save and retrieve Students and Lists of...
In C Program #include Create a C program that calculates the gross and net pay given...
In C Program #include Create a C program that calculates the gross and net pay given a user-specified number of hours worked, at minimum wage. The program should compile without any errors or warnings (e.g., syntax errors) The program should not contain logical errors such as subtracting values when you meant to add (e.g., logical errors) The program should not crash when running (e.g., runtime errors) When you run the program, the output should look like this: Hours per Week:...
1) Create a "Can I be President?" program. The program determines if the user meets the...
1) Create a "Can I be President?" program. The program determines if the user meets the minimum requirements for becoming the President of the United States. Use user input. The rules for being president of the U.S. are: Older than 35 Resident of US for 14 Years Natural born citizen Print True if the person could be president and False if they can't be president. 2) Alter one line of that program to be a "I can't be President?" game....
Problem Statement: Design a combinational logic circuit that meets the following specifications: • Input: 3-bit binary...
Problem Statement: Design a combinational logic circuit that meets the following specifications: • Input: 3-bit binary integer (A), 2-bit binary integer (B). • Output: 5-bit binary integer (X) = (AxB). For example, A=6, B=2, X=6x2=12. Notation: • A=(A2,A1,A0) • B=(B1,B0) • X=(X4,X3,X2,X1,X0) Required Output: Show the truth table and a minimal logic expression for each of the outputs. Also, draw a logic diagram using discrete gates. Extra Credit: Redesign by using multiplexers and minimal discrete logic as appropriate. 1 |...
Create shell in C which meets requirements below: 1. The shell must support the following internal commands:
Create shell in C which meets the requirements below:1. The shell must support the following internal commands:i.cd - Change the current default directory toIf the argument is not present, report the current directory. If the directory does not exist an appropriate errorshould be reported. This command should also change thePWD environment variable.ii. clr - Clear the screen.iii. dir - List the contents of directory. iv.environ - List all the environment strings.v. echo - Display on the display followed by a newline...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT