problems with "else"

problem is that i cant get my "else" to work. i tryed it with only the if and got it kinda working. after you put your lenght in, it jumps to "what is your name" and the tells if bmi is goo or bad and then to "press any key to continue...."

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
 #include <iostream>
#include <string>

using namespace std;

int name, meter, kg;
long bmi;

int main()
{
	

	cout << "vad är din vikt i KG?:    "; //whats your weight
	cin >> kg;

	cout << "Hur lång är du i meter:    "; //whats your height in meters
	cin >> meter; 

	cout << "Och slutligen vad är dit namn:    "; //and finally whats your name
	cin >> name;
	

	bmi = (meter*meter) / kg;

	while(true);
	{
		if (bmi < 35);
		{
			cout << "not so good";
		}
		else  //"else" getting redlined
		{
			cout << "great";
		}
	}


	system("pause");
	return 0;
}
The problem is on line 27, you put a semicolon after the if statement which is the cause of the error you are experiencing with your else statement. Remove the semicolon at the end of that line and your else statement should work.

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
 #include <iostream>
#include <string>

using namespace std;

int name, meter, kg;
long bmi;

int main()
{
	

	cout << "vad är din vikt i KG?:    "; //whats your weight
	cin >> kg;

	cout << "Hur lång är du i meter:    "; //whats your height in meters
	cin >> meter; 

	cout << "Och slutligen vad är dit namn:    "; //and finally whats your name
	cin >> name;
	

	bmi = (meter*meter) / kg;

	while(true);
	{
		if (bmi < 35) //there was a semicolon here causing the error
		{
			cout << "not so good";
		}
		else  //"else" getting redlined
		{
			cout << "great";
		}
	}


	system("pause");
	return 0;
}
Note: You have the same issue on line 25 with your while statement.
it fixed it thanx..
now it goes on a endles loop saying "not so goodnot so goodnot so good"
The while loop condition of true is never changed, so there's an infinite loop. Do you need a loop? Are they supposed to be able to input details for multiple people?

Why declare global variables at lines 6 and 7 instead of declaring them locally in the main function?
remove your while and it will work fine.
Last edited on
Registered users can post here. Sign in or register to post.