Not the output desired

Ok when I compile this program it asks me for 'Gender' and if I put something other than 'F' or 'M' it should go straight to an 'Invalid Selection!' message that I have incorporated. What am I doing wrong?
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
#include <iostream>
 #include <iomanip>
 
using namespace std;
 
int main()
 {
 //Declare Variables
 
int weight = 0;
 int dailyCal = 0;
 char gender = ' ';
 char level = ' ';
 
//enter gender
 gender = toupper(gender);
 cout << "Enter Gender ('F') for Femaile and ('M') for Male: ";
 cin >> gender;
 
level = toupper(level);
 cout << "Enter Your Activity Level (A/I): ";
 cin >> level;
 
cout << "Enter Current Weight: ";
 cin >> weight;
 
//beginning if
 if (gender == 'M')
 {
 if (level == 'A')
 dailyCal = weight * 15;
 else if (level == 'I')
 dailyCal = weight * 13;
 else 
cout << "Invalid Selection!" << endl;
 }
 else if (gender == 'F')
 {
 if (level == 'A')
 dailyCal = weight * 12;
 else if (level == 'I')
 dailyCal = weight * 10;
 else
 cout << "Invalid Selection!" << endl;
 }
 
//Display Calories needed
 
cout << "Daily calories needed: " << dailyCal << endl;
 
system("pause");
 return 0;
 }
Last edited on
If by "jump" you mean "gender" then there's nothing between the reading of the gender and the asking of the activity level and weight that would make the execution go to the invalid selection case.

You would have to do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Enter Gender ('F') for Female and ('M') for Male: ";
cin >> gender;
gender = toupper(gender);

if( NotValidGender(gender) )
{
   cout << "Invalid Selection!" << endl;
}

//maybe do the above in a loop until the user inputted a valid gender

cout << "Enter Your Activity Level (A/I): ";
...
Ok, I reworded it but it is still not giving me the right output. Please someone help me with this.

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

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	//Declare Variables

	int weight = 0;
	int dailyCal = 0;
	char gender = ' ';
	char level = ' ';

	//enter gender
	gender = toupper(gender);
	cout << "Enter Gender ('F') for Femaile and ('M') for Male: ";
	cin >> gender;

	level = toupper(level);
	cout << "Enter Your Activity Level (A/I): ";
	cin >> level;

	cout << "Enter Current Weight: ";
	cin >> weight;

	//beginning if
	if (gender == 'M')
	{
		if (level == 'A')
			dailyCal = weight * 15;
		else if (level == 'I')
			dailyCal = weight * 13;
		else 
			cout << "Invalid Selection!" << endl;
	}
	else if (gender == 'F')
	{
		if (level == 'A')
			dailyCal = weight * 12;
		else if (level == 'I')
			dailyCal = weight * 10;
		else
			cout << "Invalid Selection!" << endl;
	}

	//Display Calories needed

	cout << "Daily calories needed: " << dailyCal << endl;

	system("pause");
	return 0;
}
No, the problem is obivous. You have to look at the program.
Take a look at this:
1
2
3
4
5
6
7
8
9
cout << "Enter Gender ('F') for Femaile and ('M') for Male: ";
	cin >> gender;

	level = toupper(level);
	cout << "Enter Your Activity Level (A/I): ";
	cin >> level;

	cout << "Enter Current Weight: ";
	cin >> weight;

Here you're reading the gender, the level activity and the weight. The program will read the level activity and the weight even if the gender is other the M or F.
So, if you want you program to read the gender, to see if it is M or F and if it's so, to make that operations and in the other case to stop the program and to print "Invalid Selection", you'll have to do like this:
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	//Declare Variables

	int weight = 0;
	int dailyCal = 0;
	char gender = ' ';
	char level = ' ';

	//enter gender
	gender = toupper(gender);
	cout << "Enter Gender ('F') for Femaile and ('M') for Male: ";
	cin >> gender;
	level = toupper(level);
    if((gender == 'M') || (gender == 'F'))
	{


	cout << "Enter Your Activity Level (A/I): ";
	cin >> level;

	cout << "Enter Current Weight: ";
	cin >> weight;

	//beginning if
	if (gender == 'M')
	{
		if (level == 'A')
			dailyCal = weight * 15;
		else if (level == 'I')
			dailyCal = weight * 13;
		else
			cout << "Invalid Selection!" << endl;
	}
	else if (gender == 'F')
	{
		if (level == 'A')
			dailyCal = weight * 12;
		else if (level == 'I')
			dailyCal = weight * 10;
		else
			cout << "Invalid Selection!" << endl;
	}


	//Display Calories needed

	cout << "Daily calories needed: " << dailyCal << endl;
	}
	else
    {
        cout<<"Invalid Selection!";
        return 0;
    }


	return 0;
}

I hope it's what you want.
Last edited on
I will point out the error in your logic. This is your code verbatim without the code inside your ifs. Do you see the problem?


1
2
3
4
5
6
7
8
9
10
	
if (gender == 'M')
{
  //...
}
else if (gender == 'F')
{
  //...
}
//Ultra hint, you are missing something here... 
Last edited on
Ok, I think like this.

if the gender is not 'M' then it would go to the gender being 'F', right?
The if-clause is just checking something. If the gender is not 'M' it will check the next if-clause. If it isn't 'F' either, it will print "Invalid Selection" in the next case:
1
2
3
4
5
6
7
8
9
if (gender == 'M')
{
  //...
}
else if (gender == 'F')
{
  //...
}
else cout<<"Invalid Selection!";
Topic archived. No new replies allowed.