Search code examples
c++if-statementcharcin

C++ if, else if, else statement is not printing cout result


I'm struggling with this code. Been working at these if, else if, else statements for a few hours now.

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";
    }
}

They aren't successfully printing the cout results. I had it somewhat working earlier when the math formulas inside the statements used to be different. I've tried to have all of them as if statements which I'm pretty sure isn't how it's supposed to be. I also had an issue where it would only print the result from option #1 despite typing in any other option.

TLDR, with the current code, simply won't print no matter which option I pick from 1 to 6.


Solution

  • You forgot to add single quotation marks (i.e. '') to your values. For instance, this '1' is a character literal but this 1 is not a character literal (it's an integer literal). You need to compare a value of char type with values of char type.

    Also, you better use a switch statement instead of the if else if structure.

    Like this:

    #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( );
    }
    

    It's probably also worth mentioning that due to the changes I made, the assembly output of the program decreased from 207 lines down to 136 lines which means it's more efficient now.