【问题标题】:C++ if, else if, else statement is not printing cout resultC++ if、else if、else 语句不打印 cout 结果
【发布时间】:2022-08-18 15:32:01
【问题描述】:

我正在为这段代码苦苦挣扎。已经在这些 if、else if、else 语句上工作了几个小时。

void metric()

double mWeight;
double mHeight;
double mAge;
char mExercise;
bool mCorrectExercise = true;
int metricResult;

cout << \"Please type in your age: \";
cin >> mAge;

cout << \"Please type in your weight: \";
cin >> mWeight;

cout << \"Please type in your height: \";
cin >> mHeight;

cout << \"Finally, please select an exercise program that most closely matches yours.\\n\\n1) No exercise.\\n\\n2) 1-2 hours a week.\\n\\n3) 3-5hours a week.\\n\\n4) 6-10 hours a week\\n\\n5) 11-20 hours a week.\\n\\n6) 20+ hours a week.\\n\\n\";
cin >> mExercise;



if (mExercise == 1)
{
    metricResult = (mWeight * 11) + (mHeight * 8) - (mAge * 6.5) + 66;
    cout << metricResult << \"\\n\\n\";
}
else if (mExercise == 2)
{
    metricResult = (mWeight * 11) + (mHeight * 8) - (mAge * 6.5) + 66;
    cout << metricResult * 1.1 << \"\\n\\n\";
}
else if (mExercise == 3)
{
    metricResult = (mWeight * 11) + (mHeight * 8) - (mAge * 6.5) + 66;
    cout << metricResult * 1.25 << \"\\n\\n\";
}
else if (mExercise == 4)
{
    metricResult = (mWeight * 11) + (mHeight * 8) - (mAge * 6.5) + 66;
    cout << metricResult * 1.35 << \"\\n\\n\";
}
else if (mExercise == 5)
{
    metricResult = (mWeight * 11) + (mHeight * 8) - (mAge * 6.5) + 66;
    cout << metricResult * 1.5 << \"\\n\\n\";
}
else if (mExercise == 6)
{
    metricResult = (mWeight * 11) + (mHeight * 8) - (mAge * 6.5) + 66;
    cout << metricResult * 1.7 << \"\\n\\n\";
}
else
{
    cout << \"Invalid input. Please try again.\\n\\n\";
}

}

他们没有成功打印 cout 结果。当语句中的数学公式曾经不同时,我早些时候让它有点工作。我试图让所有这些都像我很确定的陈述不是它应该是的那样。我还有一个问题,尽管输入了任何其他选项,但它只会打印选项 #1 的结果。

TLDR,使用当前代码,无论我从 1 到 6 选择哪个选项,它都不会打印。

谢谢

  • char mExercise 是结果不等于 1-6 的原因。字符 \'1\' 的 ASCII 值是 49,例如,不是 (int) 1。也许您想改用 int mExercise
  • mExercise 的类型为 char。当输入为1 时,从std::cin 读取的mExercise 的值为\'1\'(注意单引号),但值为\'1\'char 没有数值1 .要修复,(1)将mExercise的类型更改为int(因此读取1的输入将进行转换,并给出1的数值而不是char的值@ 987654338@) 或 (2) 将 if 语句中的比较更改为 mExercise == \'1\'(对于其他值也是如此)。

标签: c++


【解决方案1】:

这是因为char mExercise; 并且它编译是因为 char 可以与 int 进行比较。

将该行更改为int mExercise;,它将起作用。

【讨论】:

    【解决方案2】:

    练习是一个字符,所以在条件语句中,如果你想使用字符,你必须把''放在外面。

    【讨论】:

      【解决方案3】:

      您可以在值(1,2 等)周围使用 '' 或将 mExercise 更改为 int。

      【讨论】:

        【解决方案4】:

        您忘记在值中添加单引号(即'')。例如,这个 '1' 是一个字符文字,但这个 1 不是一个字符文字(它是一个整数文字)。您需要将char 类型的值与char 类型的值进行比较。

        此外,您最好使用 switch 语句而不是 if else if 结构。

        像这样:

        #include <iostream>
        
        
        void metric( )
        {
            // bool mCorrectExercise = true;
        
            std::cout << "Please type in your age: ";
            double mAge;
            std::cin >> mAge;
        
            std::cout << "Please type in your weight: ";
            double mWeight;
            std::cin >> mWeight;
        
            std::cout << "Please type in your height: ";
            double mHeight;
            std::cin >> mHeight;
        
            std::cout << "Finally, please select an exercise program that "
            "most closely matches yours.\n\n1) No exercise.\n\n"
            "2) 1-2 hours a week.\n\n3) 3-5hours a week.\n\n"
            "4) 6-10 hours a week\n\n5) 11-20 hours a week.\n\n"
            "6) 20+ hours a week.\n\n";
        
            char mExercise;
            std::cin >> mExercise;
        
            const double metricResult { (mWeight * 11) + (mHeight * 8) - (mAge * 6.5) + 66 };
        
        
            switch ( mExercise )
            {
                case '1':
                    std::cout << metricResult * 1.0 << "\n\n";
                    break;
                case '2':
                    std::cout << metricResult * 1.1 << "\n\n";
                    break;
                case '3':
                    std::cout << metricResult * 1.25 << "\n\n";
                    break;
                case '4':
                    std::cout << metricResult * 1.35 << "\n\n";
                    break;
                case '5':
                    std::cout << metricResult * 1.5 << "\n\n";
                    break;
                case '6':
                    std::cout << metricResult * 1.7 << "\n\n";
                    break;
                default:
                    std::cout << "Invalid input. Please try again.\n\n";
            }
        }
        
        int main( )
        {
            metric( );
        }
        

        可能还值得一提的是,由于我所做的更改,程序的汇编输出从 207 行减少到 136 行,这意味着它现在更有效率。

        【讨论】:

        • 当然,您可以使用由mExercise 索引的度量结果系数容器走得更远。几乎是单线。并检查std::cin 的返回值。