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



所以我试图制作一个计算器,我添加了一个部分,这样我就可以计算面积,首先我要求整数或几何数学,当我选择几何时,它跳过了我想要计算体积的问题。但是没有编译器错误。一切都在";否则如果(choice=="geometry"({"并且直到最后一行。任何人都知道如何修复。

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string choice;
cout << "choose integer or geometryn";
cin >> choice;
if (choice == "integer") {
double num1{ 0 };
double num2{ 0 };
cout << "pick a numbern";
cin >> num1;
cout << "pick another numbern";
cin >> num2;
string integerChoice;
cout << "choose addition, subtraction, multipliction, or divisionn";
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 non";
cin >> geoChoice1;
if (geoChoice1 == "yes") {
cout << "choose retangular prism(incudes cubes), cone, or cylindern";
string volumeChoice;
cin >> volumeChoice;
if (volumeChoice == "rectangular prism") {
double recPrismLength{ 0 };
double recPrismWidth{ 0 };
double recPrismHeight{ 0 };
cout << "Enter the lengthn";
cin >> recPrismLength;
cout << "Enter the widthn";
cin >> recPrismWidth;
cout << "Enter the heightn";
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 cylindern";
cin >> cHeight;
cout << "Enter the radius of the cylindern";
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 geometryn";
cin >> choice;
if (choice == "integer") {
double num1{ 0 };
double num2{ 0 };
cout << "pick a numbern";
cin >> num1;
cout << "pick another numbern";
cin >> num2;
string integerChoice;
cout << "choose addition, subtraction, multipliction, or divisionn";
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 non";
cin >> geoChoice1;
ToLower(geoChoice1);
if (geoChoice1 == "yes") {
cout << "choose retangular prism(incudes cubes), cone, or cylindern";
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 lengthn";
cin >> recPrismLength;
cout << "Enter the widthn";
cin >> recPrismWidth;
cout << "Enter the heightn";
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 cylindern";
cin >> cHeight;
cout << "Enter the radius of the cylindern";
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;是";将不等于";是的";。我做了一个函数,将您的输入转换为小写形式,然后对其进行求值,如果不是";是";或";否";要求用户重新放置。

还有一些其他错误,我已经发表了意见,你必须纠正。