I can not for the life of me get this to run right...

If the user enters A or B, the user enters the height and weight, it then prints the condition of low, target, or high. The program should be in a loop that until the user chooses C, the program will again ask for more input. I can not for the life of me figure this out. What am I doing wrong?

I apologize for any standard programming rules being broken. This is for school and I am very new at this. Here is my code:

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
#include <iostream>
using namespace std;

int main()
	
{
	char sex;

	cout << "A = female B = male C = quit" << endl;

	cin >> sex;

	while (sex != 'C')
		{
			double height;
			int weight;
		if (sex == 'A')
		{

			cout << "Enter Height" << endl;
			cin >> height;
			cout << "Enter Weight" << endl;
			cin >> weight;
			if ((height = 58) || (height <= 64))
			{
				if (weight <= 114)
				{ cout << "weight is low!" << endl; }
				
				else if ((weight >= 115) || (weight = 133))
				{ cout << "weight is on target" << endl; }

				else
				{ cout << "weight is high!" << endl; }
				
			}

			else if ((height = 65) || (height >= 71))
			{
				if (weight <= 117)
				{ cout << "weight is low!" << endl; }

				else if ((weight >= 136) || (weight = 156))
				{ cout << "weight is on target!" << endl; }

				else
				{ cout << "weight is high!" << endl; }

			}
		}
		else if (sex == 'B')
		{
			if ((height = 61) || (height <= 67))
			{
				if (weight <= 114)
				{ cout << "weight is low!" << endl; }

				else if ((weight >= 123) || (weight = 133))
				{ cout << "weight is on target!" << endl; }

				else
				{ cout << "weight is high!" << endl; }
			}
			if ((height = 68) || (height <= 75))
			{
				if (weight <= 155)
				{ cout << "weight is low!" << endl; }

				else if ((weight >= 156) || (weight = 175))
				{ cout << "weight is on target!" << endl; }

				else
				{ cout << "weight is high!" << endl; }
			}
		}
		else
			cout << "invalid choice";
	}
}
don't use while(sex != 'C')! That would not work. You have to declare a bool variable and set the value to true. Also, if I were you, I'd use a do-while-loop.
1
2
3
do{
    //whatever
}while(/*something*/);

Because there, you always have at least one execution of the loop. It doesn't make sense if you start the program that you stop it as the first step.

So your while-loop should look a bit like this:
1
2
3
4
5
6
7
bool b=true;

do{
    if(sex=='C') b=false;

    //whatever
}while(b/*==true*/);
Actually the while loop will work just fine as it's written, but you need to get input inside the loop. If you keep the logic the way it is now, just get input at the bottom of the loop for the sex/quit info. The code prompting for height and weight should go before the first if statement in the loop (or leave it where it is and duplicate it inside the else if ( sex == 'B' ) block.)


Also, on lines 24, 29, 37, 42, 52, 57, 63 and 68 you use = when you should be using == for comparison.

I still can't get it to work. I still do not know what I am doing wrong. It seems to be just repeating the A option over and over, no matter what letter you pick initially. Did I do it correctly? If not, what did I do 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
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
#include <iostream>
using namespace std;

int main()
	
{
	char sex;
	bool b=true;
	


	cout << "A = female B = male C = quit" << endl;

	cin >> sex;

	do
		{	double height;
			int weight;

			if (sex =='C')
			{	b= false; }

			if (sex == 'A')
			{	double height;
				int weight;
				cout << "Enter Height" << endl;
				cin >> height;
				cout << "Enter Weight" << endl;
				cin >> weight;
					if ((height == 58) || (height <= 64))
					{	if (weight <= 114)
						{ cout << "weight is low!" << endl; }				
						else if ((weight >= 115) || (weight == 133))
						{ cout << "weight is on target" << endl; }
						else
						{ cout << "weight is high!" << endl; }
				
					}
					else if ((height == 65) || (height >= 71))
					{	if (weight <= 117)
						{ cout << "weight is low!" << endl; }

					else if ((weight >= 136) || (weight == 156))
						{ cout << "weight is on target!" << endl; }

					else
						{ cout << "weight is high!" << endl; }
					}
			}
			else if (sex == 'B')
			{	double height;
				int weight;
				cout << "Enter Height" << endl;
				cin >> height;
				cout << "Enter Weight" << endl;
				cin >> weight;
					if ((height == 61) || (height <= 67))
					{	if (weight <= 114)
						{ cout << "weight is low!" << endl; }
						else if ((weight >= 123) || (weight == 133))
						{ cout << "weight is on target!" << endl; }
						else
						{ cout << "weight is high!" << endl; }
					}
					else if ((height == 68) || (height <= 75))
					{	if (weight <= 155)
						{ cout << "weight is low!" << endl; }
						else if ((weight >= 156) || (weight == 175))
						{ cout << "weight is on target!" << endl; }
						else
						{ cout << "weight is high!" << endl; }
					}
			}
			
		}while(b=true);
		
	
}
Last edited on
Put lines 12 and 14 after line 16. That way, it will ask input every time.

Secondly, line 75 is wrong: it should be "while (b == true)", using comparison (==) instead of assignment (=).
Last edited on
Ok, now I am confused. When I did that it just repeats the menu every time input is put in.

A = female B = male C = quit
A
A = female B = male C = quit
B
A = female B = male C = quit
C
A = female B = male C = quit

This is what I put:

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
#include <iostream>
using namespace std;

int main()
	
{
	char sex;
	bool b=true;
	


	
	do
		{	cout << "A = female B = male C = quit" << endl;

			cin >> sex;
			double height;
			int weight;



			if (sex =='C') b=false;

			if (sex == 'A')
			{	double height;
				int weight;
				cout << "Enter Height" << endl;
				cin >> height;
				cout << "Enter Weight" << endl;
				cin >> weight;
					if ((height == 58) || (height <= 64))
					{	if (weight <= 114)
						{ cout << "weight is low!" << endl; }				
						else if ((weight >= 115) || (weight == 133))
						{ cout << "weight is on target" << endl; }
						else
						{ cout << "weight is high!" << endl; }
				
					}
					else if ((height == 65) || (height >= 71))
					{	if (weight <= 117)
						{ cout << "weight is low!" << endl; }

					else if ((weight >= 136) || (weight == 156))
						{ cout << "weight is on target!" << endl; }

					else
						{ cout << "weight is high!" << endl; }
					}
			}
			else if (sex == 'B')
			{	double height;
				int weight;
				cout << "Enter Height" << endl;
				cin >> height;
				cout << "Enter Weight" << endl;
				cin >> weight;
					if ((height == 61) || (height <= 67))
					{	if (weight <= 114)
						{ cout << "weight is low!" << endl; }
						else if ((weight >= 123) || (weight == 133))
						{ cout << "weight is on target!" << endl; }
						else
						{ cout << "weight is high!" << endl; }
					}
					else if ((height == 68) || (height <= 75))
					{	if (weight <= 155)
						{ cout << "weight is low!" << endl; }
						else if ((weight >= 156) || (weight == 175))
						{ cout << "weight is on target!" << endl; }
						else
						{ cout << "weight is high!" << endl; }
					}
			}
			
		}while(b==true);
		
	
}
You may get the results you're getting if you weren't using capital letters as the input. But even if you did, your checking is being done wrong. Like in if (height == 58 || height <= 64), entering ANY number up to 64, including negative numbers, would register as true. A -10 IS less than or equal to 64. What you need is if (height >= 58 && height <= 64), which is if height is higher or equal to 58 AND height is less than or equal to 64. The same with the other checking in the program. Your program should look more 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
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
99
100
101
102
103
// Weight-Height.cpp : main project file.

#include <iostream>
using namespace std;

int main()
{
	char sex;
	double height;
	int weight;

	bool b=true;

	do
	{	
		cout << "Are you..\n\nA = Female\nB = Male\n\nC = choose to quit" << endl;

		cin >> sex;
		sex = toupper(sex);

		if (sex =='C')
		{
			cout << "Good-bye for now.." << endl;
			b=false;
		}

		if (sex == 'A')
		{	
			cout << "Enter height in inches" << endl;
			cin >> height;
			cout << "Enter weight in pounds" << endl;
			cin >> weight;
			if ((height >= 58) && (height <= 64))
			{	if (weight <= 114)
			{ 
				cout << "Weight is low!" << endl;
			}				
			else if ((weight >= 115) && (weight <= 133))
			{ 
				cout << "Weight is on target" << endl;
			}
			else
			{ 
				cout << "Weight is high!" << endl;
			}
			}

			else if ((height >= 65) && (height >= 71))
			{
				if (weight <= 117)
				{ 
					cout << "Weight is low!" << endl;
				}
				else if ((weight >= 136) && (weight <= 156))
				{ 
					cout << "Weight is on target!" << endl;
				}
				else
				{
					cout << "Weight is high!" << endl; 
				}
			}
		}
		else if (sex == 'B')
		{
			cout << "Enter height in inches" << endl;
			cin >> height;
			cout << "Enter weight in pounds" << endl;
			cin >> weight;
			if ((height >= 61) && (height <= 67))
			{
				if (weight <= 114)
				{
					cout << "Weight is low!" << endl;
				}
				else if ((weight >= 123) && (weight <= 133))
				{ 
					cout << "Weight is on target!" << endl;
				}
				else
				{ 
					cout << "Weight is high!" << endl; 
				}
			}
			else if ((height >= 68) && (height <= 75))
			{
				if (weight <= 155)
				{
					cout << "Weight is low!" << endl; 
				}
				else if ((weight >= 156) && (weight <= 175))
				{ 
					cout << "Weight is on target!" << endl; 
				}
				else
				{
					cout << "Weight is high!" << endl;
				}
			}
		}

	} while(b==true);
}
Last edited on
omg, thanks a lot everyone. You were more help than my professor was. I have sent email after email...no answer. I appreciate all the help and thanks again. ^^
Topic archived. No new replies allowed.