我的C 代码永远不会超过其他语句
My C++ code will never be executed past the else statement
刚从这里开始。Xcode告诉我,"代码永远不会在其他语句中的第一行上执行,我不知道该怎么做。
我正在尝试做到这一点,以便如果用户的输入不是开关语句中的四个选项,则默认为第三个选项。请帮助。
int lengthoption;
std::cout << "Please select an option for the length of your loan based on the following options: " << std::endl;
std::cout << "1. 3 years" <<std::endl;
std::cout << "2. 4 years" <<std::endl;
std::cout << "3. 5 years" <<std::endl;
std::cout << "4. 6 years" <<std::endl;
std::cin >> lengthoption;
double NumberofPayments;
if (lengthoption == 1 || 2 || 3 || 4)
{
switch (lengthoption)
{
case 1:
NumberofPayments = 36;
std::cout << "You chose a 3 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
break;
case 2:
NumberofPayments = 48;
std::cout << "You chose a 4 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
break;
case 3:
NumberofPayments = 60;
std::cout << "You chose a 5 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
break;
case 4:
NumberofPayments = 72;
std::cout << "You chose a 6 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
}
}
else
{
***NumberofPayments = 60;***
std::cout << "You chose a 5 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
}
if (lengthoption == 1 || 2 || 3 || 4)
2评估为true,因此条件始终是正确的。
您必须在每个||
之间放置完整的布尔表达:
if (lengthoption == 1 ||
lengthoption == 2 ||
lengthoption == 3 ||
lengthoption == 4)
或
if (lengthoption >= 1 && lengthoption <= 4)
相关文章:
- C++ If 语句,从其他语句重复
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 如果有 constexpr if 语句,为什么没有其他 constexpr 语句呢?
- 在后台循环,同时运行其他语句 C++
- C - 如果语句问题.代码将不会运行其他,否则
- 否则,如果语句意外执行,则表现为其他语句
- 我可以将循环用作如果其他条件语句
- 如何引用无效的输入,然后重新输入和如果其他语句
- C 程序运行不正确的其他语句
- C 搜索数组以获取一个数字,但如果找不到该数组,则不会触发其他语句
- 如何将多个变量与其他if语句进行比较
- 如何在内联回调函数中使用EXPECT_EQ和其他Google Mock语句
- C 使用功能定义中的if/其他语句以返回最小的数字
- 最后一行欧普特两个组合输出问题?如果其他语句
- 我的C 代码永远不会超过其他语句
- 为什么if语句中未定义的参数不会导致segfault或其他错误
- 如何停止 while 循环并转到我的其他语句
- C++Bare布尔表达式(不包含if、while或其他语句)
- C++,其他受scanf语句影响的变量
- C++计算器跳过其他语句