Home > Blockchain >  C if, else if, else statement is not printing cout result
C if, else if, else statement is not printing cout result

Time:08-18

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 result from option #1 despite typing in any other option.

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

Thank you

CodePudding user response:

It's because of char mExercise; and it compiles because char can be compared to int.

Change that line to int mExercise; and it will work.

CodePudding user response:

mExcercise is a char, so in conditional statments, if you want to use char, you have to put the '' outside.

  •  Tags:  
  • c
  • Related