Please provide C++ code for these guidelines: -Your main function should: read i
ID: 3671416 • Letter: P
Question
Please provide C++ code for these guidelines:
-Your main function should: read in a ‘C’, ‘S’, ‘R’, ‘T’ or an ‘X’. If the user enters a ‘C’, they want to calculate the Area of a Circle. (S is for Square, R for Rectangle, T for Triangle and X is to Exit).
-The user should be able to enter an uppercase or lowercase letter.
-Continue asking the user for another letter until they enter ‘X’ to exit.
-Once the User enter a letter, prompt the user for the data needed to calculate the figure that they want.
(Example if they want to calculate the area of a Circle:
1. prompt the User for to enter a radius.
2. Read in the radius,
3. calculate the area by using the formula a=pi*Radius squared.
4. Then print out the area of the circle.
5. Then go back to the original prompt for C S R T or X.
Explanation / Answer
This below C++ code is written based on the given problem statement requirements.
1.Created switch case statement for select the area for S, R, T or C..
2. Based on the selection input will read and calculate the area and display the area..
See the below code for getting better understand..
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char ch;
float radius = 0.0, side = 0.0, width =0.0, length = 0.0, base =0.0, height = 0.0;
float pi = 3.14159;
double area = 0.0;
while(ch !='X' || ch !='x')
{
cout<<"Select C for circle S for Square R for Rectangle T for Triangle ad X for exit"<<endl;
cin>>ch;
switch(ch)
{
case 'C':
cout<<"Enter Radius: "<<endl;
cin>>radius;
area = pi * radius * radius;
cout<<"Circle Area is : "<<area<<endl;
area = 0.0;
break;
case 'c':
cout<<"Enter Radius: "<<endl;
cin>>radius;
area = pi * radius * radius;
cout<<"Circle Area is : "<<area<<endl;
area = 0.0;
break;
case 'S':
cout<<"Enter side: "<<endl;
cin>>side;
area = side * side ;
cout<<"Square Area is : "<<area<<endl;
area = 0.0;
break;
case 's':
cout<<"Enter side: "<<endl;
cin>>side;
area = side * side;
cout<<"Square Area is : "<<area<<endl;
area = 0.0;
break;
case 'R':
cout<<"Enter Width : "<<endl;
cin>>width;
cout<<"Enter length : "<<endl;
cin>>length;
area = width * length;
cout<<"Square Area is : "<<area<<endl;
area = 0.0;
break;
case 'r':
cout<<"Enter Width : "<<endl;
cin>>width;
cout<<"Enter length : "<<endl;
cin>>length;
area = width * length;
cout<<"Square Area is : "<<area<<endl;
area = 0.0;
break;
case 'T':
cout<<"Enter Base : "<<endl;
cin>>base;
cout<<"Enter Height : "<<endl;
cin>>height;
area = (base * height)/2;
cout<<"Square Area is : "<<area<<endl;
area = 0.0;
break;
case 't':
cout<<"Enter Base : "<<endl;
cin>>base;
cout<<"Enter Height : "<<endl;
cin>>height;
area = (base * height)/2;
cout<<"Square Area is : "<<area<<endl;
area = 0.0;
break;
case 'X':
exit(0);
case 'x':
exit(0);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.