### having trouble with an if else statement

In my computer science I class I am having trouble making my program make decisions, in this project, the program has to decide whether the user is male or female and run a specific formula depending on whether the user entered male or female. These is the exact requirements for the project. A typical chocolate bar will contain around 230 calories. Write a program that allows the user to input his or her weight in pounds, height in inches, age in years, and the the character m for male and f for female. the program should then output the number of chocolate bars that should be consumed to maintain one's weight for the appropriate sex of the specified weight, height, and age. I have spent quite some time trying to figure this out and came to the conclusion that I need some help! Not asking for the answer to be given to me but just for some assistance. Below is what code I have put in, I am using a g++ compiler

#include <iostream>
using namespace std;
int main ( )
{
int weightinpounds, heightininches, ageinyears, gender, sum1, totalchoc;
cout << "Press return after entering a number\n";
cout << "Enter weightinpounds\n";
cin >> weightinpounds;
cout << "Enter heightininches\n";
cin >> heightininches;
cout << "Enter ageinyears\n";
cin >> ageinyears;
cout << "Enter gender";
cin >> gender;
if (gender = male);
{
sum1 = 66 + (6.3 * weightinpounds) + (12.9 * heightininches) - \$
totalchoc = (sum1 / 230);
cout << totalchoc;
}
else (female);
{
sum1 = 655 + (4.3 * weightinpounds) + (4.7 * heightininches) - \$
totalchoc = (sum1 / 230);
cout << totalchoc;
}

cout << "\n";
return 0;
}
You declared gender as an integer, but what integer should I possibly put in to show I am male or female? This would be better as a char (so I can put in 'm' or 'f') or as a string (so I can put in "male" or "female").

if(gender = male) - Using a single equals sign is assignment. To do comparison, a double equals sign is used.
`if(gender = 1)` This would assign the number 1 to the variable gender.

`if(gender == 1)` This would compare if gender is equal to 1.

Also note that if you use char for gender, chars must be compared in single quotes.
 ``12`` ``````char gender = 'm'; if(gender == 'm')``````

And if you use a string, they must be put in double quotes. ""
 ``12`` ``````string gender = "female"; if(gender == "female")``````

Do not include anything else in the brackets after else, or any brackets at all. Else is just every case which fails the if statement(s). Also, check your sum for female case. I suspect a maths error.
Last edited on
Topic archived. No new replies allowed.