I need help with my if statements!!!

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.

Need help quickly please ;D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//declare variables and named constants
	char gender = ' ';
	char activity = ' ';
	int weight = 0;
	
	const char genderM = 'M';
	const char genderF = 'F';
	const char activityA = 'A';
	const char activityI = 'I';

	int calorieIntake = 0;


	//enter gender (this will be the outer loop)
	cout << "Enter your your weight (in lbs.): ";
	cin >> weight;

	cout << "Enter your gender (M or F): ";
	cin >> gender;

	//uppercase the gender
	gender = toupper(gender);

	

	//set up if (boy == true, girl == false)
	if (genderM == gender)
	{
		//ask for activity level
		cout << "Enter your your activity level ('A' for active or 'I' for inactive): ";
		cin >> activity;

		//uppercase the activity level character
		activity = toupper(activity);

		//set up nested statement for males
		if (activity == activityA)
		{
			//calculate ideal calorie intake for active males
			calorieIntake = weight*15;
			
			//display calculated calorie intake for active males
			cout << "Your ideal number of calorie intake: " << calorieIntake << endl;
			
		}

		else if (activity == activityI)
		{
			//calculate ideal calorie intake for inactive males
			calorieIntake = weight*13;
			
			//display calculated calorie intake for inactive males
			cout << "Your ideal number of calorie intake: " << calorieIntake << endl;
		} //end nested if

	}

	else if (genderF == gender)

		//ask for activity level
		cout << "Enter your your activity level ('A' for active or 'I' for inactive): ";
		cin >> activity;

		//uppercase the activity level character
		activity = toupper(activity);

		//set up nested statement for females
		if (activity == activityA)
		{
			//calculate ideal calorie intake for active females
			calorieIntake = weight*12;

			//display calculated calorie intake for active females
			cout << "Your ideal number of calorie intake: " << calorieIntake << endl;
			
		}

		else if (activity == activityI)
		{
			//calculate ideal calorie intake for inactive females
			calorieIntake = weight*10;

			//display calculated calorie intake for inactive females
			cout << "Your ideal number of calorie intake: " << calorieIntake << endl;

		} //end nested if



	system("pause");
	return 0;
}	//end of main function 


here a picture of it in visual basic 2010
Last edited on
http://i.imgur.com/5yub3js.jpg?1

^^ THE PICTURE LINK
Please use the code tags <> instead of just posting the code or posting a picture of the code. It becomes 100% easier to understand!


Hope that worked.. Sorry first time posting!
Last edited on
I looked over those however i still don't see where I'm messing up.
Thank you by the way.
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.
Last edited on
Thanks for the reply. Okay that was a misprint, however I fixed it and I'm still getting the same result. It's weird.
Last edited on
Change line 64 to just say else and try again with the input you gave in the first post.
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:
else if (genderF == gender)
Notice that I removed the semicolon and added the word if.
closed account (3qX21hU5)
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 else if (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 else if or delete the conditions. Also make sure you delete all those semi colons after the else's

GOT IT! Thanks guys. It was the semicolons that were messing it up.

One more question though. Where would be a proper place to put an error message when the users enters invalid data and what exactly should I write??

By the way I updated the code
Use an else block:
1
2
3
4
5
6
7
8
9
10
11
12
if (/*valid condition*/)
{
    //some code...
}
else if (/*some other valid condition*/)
{
    //some more code...
}
else
{
    //handle any other condition that wasn't specified
}
closed account (3qX21hU5)
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.
Last edited on
Alright thank you all very much. You all were very helpful;D
Topic archived. No new replies allowed.