Home > Software design >  C How would I make something like this? For every x numbers above 50, increase y value
C How would I make something like this? For every x numbers above 50, increase y value

Time:08-22

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


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;

int metricResult = (mWeight * 9)   (mHeight * 9) - (mAge * 6.5);

/*if (mAge >= 50)
{
    int metricResult = (mWeight * 9)   (mHeight * 9) - (mAge * 8.5);
}*/




switch (mExercise)
{
case 1:
    cout << "The amount of calories you should consume on average per day:\n\n" << metricResult * 1.0 << "\n\n";
    break;
case 2:
    cout << "The amount of calories you should consume on average per day:\n\n" << metricResult * 1.1 << "\n\n";
    break;
case 3:
    cout << "The amount of calories you should consume on average per day:\n\n" << metricResult * 1.15 << "\n\n";
    break;
case 4:
    cout << "The amount of calories you should consume on average per day:\n\n" << metricResult * 1.25 << "\n\n";
    break;
case 5:
    cout << "The amount of calories you should consume on average per day:\n\n" << metricResult * 1.35 << "\n\n";
    break;
case 6:
    cout << "The amount of calories you should consume on average per day:\n\n" << metricResult * 1.5 << "\n\n";
    break;
default:
    cout << "Invalid input. Please try again.\n\n";
}

So in the place of that 'if' statement that is commented out, I'd like something along the lines of

"on mAge, for every number above 50, increase mAge in the formula

int metricResult = (mWeight * 9) (mHeight * 9) - (mAge * 8.5);

by 0.2" So for if the user types 51, mAge would become (mAge * 8.7) and 52 would make it 8.9, 53 would make it 9.1 etc

Does that make sense? It seems pretty complex.

Initially I tried loops and if statements but either way it seems like the formula needs to be more flexible than my skills allow. I simply don't have the logic or the skill to make it so. Sorry for being unable to explain it simpler.

Thank you

CodePudding user response:

If you break it down into pieces, it becomes easier to see the solution:

float overAge = (mAge > 50) ? (mAge - 50) : 0;
float ageMultiplier = 8.5   0.2 * overAge;
int metricResult = (mWeight * 9)   (mHeight * 9) - (mAge * ageMultiplier);

In case you are fairly new to programming in C , the '?' is called a 'ternary operator' and you can look that up for more information.

CodePudding user response:

Add a double factor; to your program. Then calculate factor according to the mAge like, e.g.

if(mAge<=50) factor = 8.5; // or whatever value you need
if(mAge>50) factor = 8.5   0.2*(mAge-50); // or whatever value you need

and

int metricResult = (mWeight * 9)   (mHeight * 9) - (mAge * factor);

Note: There should not be a big change in factor from mAge=50 to mAge=51 (but which values to apply to factor is up to you).

  •  Tags:  
  • c
  • Related