#include <iostream>
usingnamespace std;
int main() {
// declare constant variable PI
constfloat PI = 3.14159;
//variables required for the area calculation
int selection=0;
float radius=0.0;
float side=0;
float area=0.0;
float height=0.0;
float base=0.0;
cout<<"Program to calculate areas of objects:\n" ;
// run until 4 is selected.
Do
{
cout<<"\n1.Square \n2.Circle \n3.Rigth Triangle \n4.Quit";
cout<<"\nEnter your choice:";
cin>>selection;
if(selection==1)
{
cout<<"\nLength of side:";
cin>>side;
area=side*side;
cout<<"\nArea of Square:"<<area;
}
elseif(selection==2)
{
cout<<"\nRadius of circle:";
cin>>radius;
area=PI*radius*radius;
cout<<"\nArea of circle:"<<area;
}
elseif(selection==3)
{
cout<<"\nHeight of Triangle:";
cin>>height;
cout<<"\nBase of Triangle:";
cin>>base;
area=0.5*height*base;
cout<<"\nArea of Triangle:"<<area;
}
First I would start with making your code easier to read. It would make it easier to see that you are missing two closing "}"s. It would also allow you to see that you are missing the while condition of the do/while loop. Compiling the program would have alerted you to this unless it was a copy and paste problem.
#include <iostream>
usingnamespace std;
int main()
{
// declare constant variable PI
constfloat PI = 3.14159;
//variables required for the area calculation
int selection = 0;
float radius = 0.0;
float side = 0;
float area = 0.0;
float height = 0.0;
float base = 0.0;
cout << "Program to calculate areas of objects:\n";
// run until 4 is selected.
Do
{
cout << "\n1.Square \n2.Circle \n3.Rigth Triangle \n4.Quit";
cout << "\nEnter your choice:";
cin >> selection;
if (selection == 1)
{
cout << "\nLength of side:";
cin >> side;
area = side*side;
cout << "\nArea of Square:" << area;
}
elseif (selection == 2)
{
cout << "\nRadius of circle:";
cin >> radius;
area = PI*radius*radius;
cout << "\nArea of circle:" << area;
}
elseif (selection == 3)
{
cout << "\nHeight of Triangle:";
cin >> height;
cout << "\nBase of Triangle:";
cin >> base;
area = 0.5*height*base;
cout << "\nArea of Triangle:" << area;
}
} // <--- missing closing of do.
} // <--- Missing closing of main
Looking at the code this way it helps to what could be in a function. I would also consider using a switch/case in place of all the if/else statements.
#include <iostream>
usingnamespace std;
double SelOne();
void SelFour();
int main()
{
// declare constant variable PI
constdouble PI = 3.14159;
//variables required for the area calculation
int selection = 0;
double area = 0.0;
double radius = 0.0;
double height = 0.0;
double base = 0.0;
cout << "Program to calculate areas of objects:\n";
// run until 4 is selected.
while (true)
{
cout << "\n1.Square \n2.Circle \n3.Rigth Triangle \n4.Quit";
cout << "\nEnter your choice:";
cin >> selection;
if (selection == 1)
{
area = SelOne();
cout << "\nArea of Square:" << area << '\n';
}
elseif (selection == 2)
{
cout << "\nRadius of circle:";
cin >> radius;
area = PI * radius*radius;
cout << "\nArea of circle:" << area;
}
elseif (selection == 3)
{
cout << "\nHeight of Triangle:";
cin >> height;
cout << "\nBase of Triangle:";
cin >> base;
area = 0.5*height*base;
cout << "\nArea of Triangle:" << area;
}
elseif (selection == 4)
{
SelFour();
break;
}
else
{
std::cout << "Invalid selection!\n";
}
}
}
double SelOne()
{
double side = 0.0;
double area = 0.0;
std::cout << "\nLength of side:";
cin >> side;
area = side * side;
return area;
}
void SelFour()
{
std::cout << "\nGood bye!\n\n";
}
Program to calculate areas of objects:
1.Square
2.Circle
3.Rigth Triangle
4.Quit
Enter your choice:1
Length of side:12.5
Area of Square:156.25
1.Square
2.Circle
3.Rigth Triangle
4.Quit
Enter your choice:5
Invalid selection!
1.Square
2.Circle
3.Rigth Triangle
4.Quit
Enter your choice:4
Good bye!
You should be able to figure out how to write the other functions you need.