我的部分代码作为 if 语句的一部分被跳过

问题描述

所以我试图制作一个计算器,我添加一个部分以便我可以计算面积,首先我要求整数或几何数学,当我选择几何时,它跳过了我想计算体积的问题。但是没有编译器错误。它在“ else if (choice == "geometry") {”之后的所有内容,直到最后一行。任何人都知道如何修复。

    #include <iostream>
    #include <string>
    #include <cmath>
    using namespace std;

    int main()
    {
      string choice;
      cout << "choose integer or geometry\n";
      cin >> choice;
      if (choice == "integer") {
     double num1{ 0 };
     double num2{ 0 };
     cout << "pick a number\n";
     cin >> num1;
     cout << "pick another number\n";
     cin >> num2;
     string integerChoice;
     cout << "choose addition,subtraction,multipliction,or division\n";
     cin >> integerChoice;
     if (integerChoice == "addition") {
        cout << num1 << " + " << num2 << " is " << num1 + num2
            << '\n';
     }
     else if (integerChoice == "subtraction") {
        cout << num1 << " - " << num2 << " is " << num1 - num2
            << '\n';
     }
     else if (integerChoice == "multiplication") {
        cout << num1 << " * " << num2 << " is " << num1 * num2
            << '\n';
     }
     else if (integerChoice == "division") {
        cout << num1 << " / " << num2 << " is " << num1 / num2
            << '\n';
     }//integer is done
     }
      else if (choice == "geometry") {
    string geoChoice1;
    cout << "do you want to calculate volume,enter yes or no\n";
    cin >> geoChoice1;
    if (geoChoice1 == "yes") {
        cout << "choose retangular prism(incudes cubes),cone,or cylinder\n";

        string volumeChoice;
        cin >> volumeChoice;
        if (volumeChoice == "rectangular prism") {
            double recPrismLength{ 0 };
            double recPrismWidth{ 0 };
            double recPrismHeight{ 0 };
            cout << "Enter the length\n";
            cin >> recPrismLength;
            cout << "Enter the width\n";
            cin >> recPrismWidth;
            cout << "Enter the height\n";
            cin >> recPrismHeight;
            cout << recPrismLength << " * " << recPrismWidth << " * " << recPrismHeight << " is " <<
                recPrismLength * recPrismWidth * recPrismHeight << '\n';
        }
        else if (volumeChoice == "cylinder") {
            float cHeight;
            float cRadius;
            const double pi{ 3.14159265358979323846 };
            float cFormula{ pi * pow(2.0,cRadius) * cHeight };
            cout << "Enter the height of the cylinder\n";
            cin >> cHeight;
            cout << "Enter the radius of the cylinder\n";
            cin >> cRadius;
            cout << cFormula;


        }
        else if (geoChoice1 == "no") {

解决方法

固定代码:

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

std::string ToLower(std::string str) {
    std::transform(str.begin(),str.end(),str.begin(),[](unsigned char c) { return std::tolower(c); });
}

int main()
{
    string choice;
    cout << "choose integer or geometry\n";
    cin >> choice;
    if (choice == "integer") {
        double num1{ 0 };
        double num2{ 0 };
        cout << "pick a number\n";
        cin >> num1;
        cout << "pick another number\n";
        cin >> num2;
        string integerChoice;
        cout << "choose addition,subtraction,multipliction,or division\n";
        cin >> integerChoice;
        if (integerChoice == "addition") {
            cout << num1 << " + " << num2 << " is " << num1 + num2
                << '\n';
        }
        else if (integerChoice == "subtraction") {
            cout << num1 << " - " << num2 << " is " << num1 - num2
                << '\n';
        }
        else if (integerChoice == "multiplication") {
            cout << num1 << " * " << num2 << " is " << num1 * num2
                << '\n';
        }
        else if (integerChoice == "division") {
            cout << num1 << " / " << num2 << " is " << num1 / num2
                << '\n';
        }//integer is done
    }
    else if (choice == "geometry") {
        string geoChoice1;
        while (!(geoChoice1 == "yes" || geoChoice1 == "no"))
        cout << "do you want to calculate volume,enter yes or no\n";
        cin >> geoChoice1;
        ToLower(geoChoice1);
        if (geoChoice1 == "yes") {
            cout << "choose retangular prism(incudes cubes),cone,or cylinder\n";

            string volumeChoice;
            cin >> volumeChoice;
            ToLower(volumeChoice);
            if (volumeChoice == "rectangular prism") { //this will never be executed,as the >> operator skips whitespace,and therefore will only read "rectangular"
                //I will leave this for you to fix
                double recPrismLength{ 0 };
                double recPrismWidth{ 0 };
                double recPrismHeight{ 0 };
                cout << "Enter the length\n";
                cin >> recPrismLength;
                cout << "Enter the width\n";
                cin >> recPrismWidth;
                cout << "Enter the height\n";
                cin >> recPrismHeight;
                cout << recPrismLength << " * " << recPrismWidth << " * " << recPrismHeight << " is " <<
                    recPrismLength * recPrismWidth * recPrismHeight << '\n';
            }
            else if (volumeChoice == "cylinder") {
                float cHeight;
                float cRadius;
                const double pi{ 3.14159265358979323846 };
                double cFormula{ pi * pow(2.0,cRadius) * cHeight }; //this will always be the same,please note,because cHeight and cRadius are only filled after this function
                cout << "Enter the height of the cylinder\n";
                cin >> cHeight;
                cout << "Enter the radius of the cylinder\n";
                cin >> cRadius;
                cout << cFormula;
                break;
            }
        }
        else if (geoChoice1 == "no") break;
        else cout << "Please enter something I understand,either 'yes' or 'no'.\n";
    } //this was missing
} //this was missing


您的代码没有考虑用户输入带有大写 Y 的“是”。代码只会得出“是”不等于“是”的结论。我制作了一个函数,将您的输入转换为小写形式,然后对其进行评估,如果不是“是”或“否”,则要求用户再次输入。

还有一些其他错误,我已经发表了评论,您必须修复。