I have a program that calculates the calorie intake for a person who desires to keep a constant weight. The inputs are weight, female or male, and whether they're inactive or active.
I can get it to run but it seems to bypass the ending if statements.
for example,
(My inputs)
gender= male
weight= 180
activity level= inactive
it will ask me what the activity level is TWICE and give me the girl and boys calculation.
Line 32... I'm sure you did not mean to say gender==gender.
And line 64 is what is causing your second activity level prompt. Look at other examples of code provided by moorecm and check very carefully how the samples use the word else.
Okay just tried that and no change.
Here is what command prompt is up to during debug
Enter your gender (M or F): m
Enter your your weight (in lbs.): 180
Enter your your activity level ('A' for active or 'I' for inactive): i
Your ideal number of calorie intake: 2340
Enter your your activity level ('A' for active or 'I' for inactive): i
Your ideal number of calorie intake: 1800
Press any key to continue . . .
If I change line 64 to just say else, then it does not ask for activity level twice.
The problem in your code is that you say this: else (genderF == gender);
when you probably mean this: elseif (genderF == gender)
Notice that I removed the semicolon and added the word if.
Like booradley60 said on line 64 you have a condition for a else statement. That is a error you can not have a condition for a else statement. If you want to have a condition for it change it to elseif (you condition). Otherwise delete the condition. Also the semicolon is not suppose to be there either.
Else's are meant to be used for everything else that you don't have a condition for.
Also this line else (activity == activityI);, remember what I said about else statements. Also that semi colon is not suppose to be there.
So basically go through your code and change all the else's into elseif or delete the conditions. Also make sure you delete all those semi colons after the else's
if (/*valid condition*/)
{
//some code...
}
elseif (/*some other valid condition*/)
{
//some more code...
}
else
{
//handle any other condition that wasn't specified
}
Depends on the error type and other factors. Since you are still learning you could just use cout << "Some error message here" << endl; to display error messages instead of getting into exceptions.
For example usually when there is a error you do usually two things.
1) Notify the user of the error. cout << "You have encountered a error!" << endl;
or cerr << "You have encountered a error!" << endl;
2) Try and do something to fix it. If the error can not be fixed it is best to exit the program.
Here is a example of a very beginner error handling, which handles a error when the user enters a number less then 0.
1 2 3 4 5 6 7 8 9
cout << "Enter a number: ";
cin >> number;
while (number < 0)
{
cout << "You entered a invalid number! The number can't be less then 0!" << endl;
cout << "Please enter another number: ";
cin >> number;
}
This is a very simple error handler which if the user enters a number that is less then 0 will keep running a loop that asks them for a different number until they enter one more then 0. Of course this still doesn't even come close to handling all the errors possible for that situation (Like what if a user enters something other then a number), but it shows you a simple example. Error handling is a very big subject and would be hard to learn just from a post on the forums so if you are interested in it I would check out one of the many tutorials on the net about it or grab a good book on it.
P.S the else statement might not be the best thing to use for error handling but it works. If anything I would look into switch statements and the default case.