c++ - 我的部分代码作为我的 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 输入“是”,您的代码没有考虑到。代码只会得出“是”不等于“是”的结论。我制作了一个函数,将您的输入转换为小写形式,然后对其进行评估,如果不是“是”或“否”,则要求用户再次输入。
还有一些其他错误我已发表评论,您将不得不修复。
推荐阅读
- sql - 如何在 SQL 的 2 列中对具有镜像值的行进行重复数据删除
- github - Github 或 GItLab 网站上是否有提供远程存储库的终端?
- python - Fabric:如何访问 postgres shell
- flutter - 如何创建网格布局?
- c# - 如何在不同的方法中验证在一个方法中创建的用户信息对象
- exoplayer2.x - 如何序列化/存储 Media3/ExoPlayer MediaItem
- java - Android numberPicker to countdownTimer
- python - SQLAlchemy ORM & 依赖倒置
- ionic-framework - 在 ionic5 ion-slide 中转到您想要的幻灯片页面
- visual-studio - 如何更改 Visual Studio 中选项卡布局的字体和背景颜色?