C++ Logicial Operators

66 Views Asked by At

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;
}
3

There are 3 best solutions below

3
On BEST ANSWER

You are not writing the else-if statement correctly. It should be in the form:

if(boolean expression) {}
else if (boolean expression) {} // as many else ifs as you need
else {} // optional

Therefore your else if conditionals should be:

if (Shape == "Square") {
    cout << "Enter Length of Side >: ";
    cin >> length;
    cout <<  "Perimeter = " ; 
    cout << length * 4 <<endl;
} else if (Shape == "Triangle"){
    // and so on...
} else {
    cout << "Invalid shape entered.";
}

Additionally, PI isn't 3.14. Include <math.h> and use M_PI.

0
On

You need to work on your code formatting...

Use else if insead of else.

if (Shape == "Square") {
    cout << "Enter Length of Side >: ";
    cin >> length;
    cout <<  "Perimeter = " ; 
    cout << length * 4 <<endl;
}
 else if (Shape == "Triangle"){ 
    cout << "Enter Length of Side >: ";
    cin >> length;
    cout <<  "Perimeter = " ; 
    cout << length * 3 <<endl;
 }    
 else if (Shape == "Circle") {
    (Shape == "Circle")
    cout << "Enter Diameter >: ";
    cin >> Diameter;
    cout <<  "Perimeter = " ; 
    cout << Diameter * Pi <<endl;
 }
 else{
    cout << "invalid shape name" << endl;
 }
0
On

Hello You have to write else if as below.

 #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";     **strong text**


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 if (Shape == "Triangle"){
         cout << "Enter Length of Side >: ";
         cin >> length;
         cout <<  "Perimeter = " ; 
         cout << length * 3 <<endl;
} else if (Shape == "Circle") { 
         cout << "Enter Diameter >: ";
         cin >> Diameter;
         cout <<  "Perimeter = " ; 
         cout << Diameter * Pi <<endl;
}
return 0;

}

Hope this will help you