So I have written a small program to work out the perimeter of simple shapes (very new to coding so keeping it simple). I am now stuck though the code works up too triangle and I can't for the life of me work out how to get the last logical operator to work!!!? As always greatly appreciate your time and advice.
Best regards, Jake.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int length;
int Diameter;
float Pi = 3.14;
string Shape;
string Square = "Square";
string Triangle = "Triangle";
string Circle = "Circle";
cout <<"=======================" << endl;
cout <<"=Welcome to Perimeters=" << endl;
cout <<"#######################" << endl;
cout <<"###|Select A Shape|####" << endl;
cout <<"=======================" << endl;
cout <<"= | Circle | =" << endl;
cout <<"= | Triangle | =" << endl;
cout <<"= | Square | =" << endl;
cout <<"=======================" << endl;
cout <<"Enter Shape >; ";
cin >> Shape;
if (Shape == "Square") {
cout << "Enter Length of Side >: ";
cin >> length;
cout << "Perimeter = " ;
cout << length * 4 <<endl;
} else {
(Shape == "Triangle"){
cout << "Enter Length of Side >: ";
cin >> length;
cout << "Perimeter = " ;
cout << length * 3 <<endl;
}
}
else {
(Shape == "Circle") {
cout << "Enter Diameter >: ";
cin >> Diameter;
cout << "Perimeter = " ;
cout << Diameter * Pi <<endl;
}
}
return 0;
}
You are not writing the else-if statement correctly. It should be in the form:
Therefore your else if conditionals should be:
Additionally, PI isn't 3.14. Include
<math.h>
and useM_PI
.