C++ 逻辑运算符

C++ Logicial Operators

所以我编写了一个小程序来计算简单形状的周长(对编码很陌生,所以保持简单)。我现在被卡住了 代码太复杂了,我一辈子都想不出如何让最后一个逻辑运算符工作!!!?一如既往地非常感谢您的时间和建议。

此致,杰克。

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

您没有正确编写 else-if 语句。它应该是这样的形式:

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

因此你的 else if 条件应该是:

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.";
}

此外,PI 不是 3.14。包括 <math.h> 并使用 M_PI.

您需要处理代码格式...

使用 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;
 }

你好,你要写else if 如下。

 #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;

}

希望对您有所帮助