BMI calculator

The calculator works perfectly fine but sometimes (mostly when the result is overweight) it displays 2 outputs i.e normal AND overweight whereas it should only display overweight.

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
 #include <iostream>
using namespace std;
int main()

{
	float a,b,c,d,e,f,g
	;cout<<"Enter your weight in kilograms\n";
	cin>>a;
	
	cout<<"Enter your height in feet\n";
	cin>>b;
	cout<<"Enter your height in inches\n";
	cin>>c;
	
	d= b*12;
	
	e= (d+c)*2.54;
	f=e/100
	
	;g= a / (f*f)
		
	;cout<<"Your BMI is=\n";
	cout<<g;

	if (g<=18.5);
     cout<<"\tYou are Underweight\n";
 
	if (g>18.5 && g<25)
	;cout<<"\tYou are normal\n";
	
	
	 if (g>=26)
	;cout<<"\tYou are overweight\n";
	
	return 0;
}
On lines 28 & 32 you should put an else in front of the if.

Also you need to remove the semi-colon ; at the end of line 25 and at beginning of lines 29 & 33.

More info on control structures here:

http://www.cplusplus.com/doc/tutorial/control/
Still no avail. The error is still there.
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
#include <iostream>
using namespace std;
int main()

{
	float a,b,c,d,e,f,g
	;cout<<"Enter your weight in kilograms\n";
	cin>>a;
	
	cout<<"Enter your height in feet\n";
	cin>>b;
	cout<<"Enter your height in inches\n";
	cin>>c;
	
	d= b*12;
	
	e= (d+c)*2.54;
	f=e/100
	
	;g= a / (f*f)
		
	;cout<<"Your BMI is=\n";
	cout<<g;

	if (g<=18.5)
     cout<<"\tYou are Underweight\n";
 
	else if (g>18.5 && g<25)
	cout<<"\tYou are normal\n";
	
	
	else if (g>=26)
	cout<<"\tYou are overweight\n";
	
	return 0;

Your updated code seems to work fine when I try it, though you might want to change line 32 to else if (g>=25)
Enter the values
Weight = 100
Height in feet = 5
Height in inches = 10

(thats obviously not my data :p but it gives 2 outputs on these values)
I get the following when I click the Edit & Run button on your 2nd post:

Enter your weight in kilograms
100
Enter your height in feet
5
Enter your height in inches
10
Your BMI is=
31.6327	You are overweight


...?
Then what is wrong with mine :/
I get like this

Your BMI is=
31.6327 You are normal
You are overweight


I use Devc++
The only thing I could suggest is to check that you are definitely compiling & running the code that you posted. The following code (that you posted) should work perfectly no-matter what compiler you use:

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
#include <iostream>
using namespace std;
int main()

{
	float a,b,c,d,e,f,g
	;cout<<"Enter your weight in kilograms\n";
	cin>>a;
	
	cout<<"Enter your height in feet\n";
	cin>>b;
	cout<<"Enter your height in inches\n";
	cin>>c;
	
	d= b*12;
	
	e= (d+c)*2.54;
	f=e/100
	
	;g= a / (f*f)
		
	;cout<<"Your BMI is=\n";
	cout<<g;

	if (g<=18.5)
     cout<<"\tYou are Underweight\n";
 
	else if (g>18.5 && g<25)
	cout<<"\tYou are normal\n";
	
	
	else if (g>=25)
	cout<<"\tYou are overweight\n";
	
	return 0;
}


I have many painful memories of pulling my hair out when my code won't work, only to find that I am editing the wrong file, or the compilation was failing & I was running an old executable. Maybe it's something like that?
Topic archived. No new replies allowed.