How to choose from the menu to calculate each area for each shape? so far it doe
ID: 3822060 • Letter: H
Question
How to choose from the menu to calculate each area for each shape? so far it does not let me choose other thing that 1 because all the numbers that I enter give me the option one to calculate area for square. I need to make the program ask what choice leaving the menu there and and then ask what it needs to calculate each. It is c++ programming and I have to use do while loop.
#include
#include
using namespace std;
//Declare your variables//
double side, base, height, width, length, radius, Areasq, Arearec, Areatri, Areacir;
const double pi = 3.1416;//Use constant for pi so you do not have to keep plugging the number//
int main() //Create a menu to be able to choose what you want to calculate//
{
int choice;
do
{
cout << endl
<< "1. Area for a square. "
<< "2. Area for a rectangle. "
<< "3. Area for a triangle. "
<< "4. Area for a circle. "
<< "5. EXIT. "
<< "Enter your choice and press return:";
cin >> choice;
{
switch (choice)//It should switch from one option to the other//
Case1:
cout << " Enter the side value and press ENTER: " << endl;
cin >> side;
Areasq = side * side;
cout << " Area: " << Areasq << "m^2" << endl;
break;
switch (choice)
Case2:
cout << " Enter width and length and press ENTER: " << endl;
cin >> width, length;
Arearec = width * length;
cout << " Area = " << Arearec << "m^2" << endl;
break;
switch (choice)
Case3:
cout << "Enter the base and height for the triangle and press ENTER:" << endl;
cin >> base, height;
Areatri = (1/2)*(base * height);
cout << " Area = " << Areatri << "m^2" << endl;
break;
switch (choice)
Case4:
cout << " Enter the radius for calculating the circle area: " << endl;
cin >> radius;
Areacir = pi * radius * radius;
cout << " Area = " << Areacir << " m^2 " << endl;
break;
}
}
while (choice != 5);
return (0);
}
Explanation / Answer
Hello, Basically your program is fine except little mistakes.
In switch case statement "case" should be all small letters and there should be a space between case keyword and the value. e.g. case 1
That was it, you are good to go.
Here I am posting full working code with corrected mistakes.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
//Declare your variables//
double side, base, height, width, length, radius, Areasq, Arearec, Areatri, Areacir;
const double pi = 3.1416;//Use constant for pi so you do not have to keep plugging the number//
int main() //Create a menu to be able to choose what you want to calculate//
{
int choice;
do
{
cout << endl
<< "1. Area for a square. "
<< "2. Area for a rectangle. "
<< "3. Area for a triangle. "
<< "4. Area for a circle. "
<< "5. EXIT. "
<< "Enter your choice and press return:";
cin >> choice;
{
switch (choice)//It should switch from one option to the other//
case 1:
cout << " Enter the side value and press ENTER: " << endl;
cin >> side;
Areasq = side * side;
cout << " Area: " << Areasq << "m^2" << endl;
break;
switch (choice)
case 2:
cout << " Enter width and length and press ENTER: " << endl;
cin >> width, length;
Arearec = width * length;
cout << " Area = " << Arearec << "m^2" << endl;
break;
switch (choice)
case 3:
cout << "Enter the base and height for the triangle and press ENTER:" << endl;
cin >> base, height;
Areatri = (1/2)*(base * height);
cout << " Area = " << Areatri << "m^2" << endl;
break;
switch (choice)
case 4:
cout << " Enter the radius for calculating the circle area: " << endl;
cin >> radius;
Areacir = pi * radius * radius;
cout << " Area = " << Areacir << " m^2 " << endl;
break;
}
}
while (choice != 5);
return (0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.