BMI program

Hi guys I am obviously new to this all and so I decided to make my first usable program implementing my knowledge on classes/objects and constructors together. I don't see any problems but this is the result I get from the program. Any help would be grateful!
http://s27.postimg.org/45jcx2vib/12345.png
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
//BMI
#include <iostream>
using namespace std;

class BMI{
public:
	BMI(){
		cout << "Enter your weight in kilograms(KG)!" << endl;
		cin >> Weight;
		cout << "Now enter your height in metres(M)!" << endl;
		cin >> Height;
	}
	float GetWeight(){
		cout << "Your weight is " << endl;
		return Weight;
	}
	float GetHeight(){
		cout << "Your height is " << endl;
		return Height;
	}
	void CalculateFirstBMI(){
		First_BMI = Weight/Height;
	}
	void CalculateSecondBMI(){
		Second_BMI = First_BMI/Height;
	}
	void SetFinalBMI(){
		Final_BMI = Second_BMI;
	}
	float TotalBMI(){
		return Final_BMI;
	}
private:
	float Weight;
	float Height;
	float First_BMI;
	float Second_BMI;
	float Final_BMI;
};

int main(){
	BMI BMI;
	cout << "Your BMI is " << BMI.TotalBMI() << "." << endl;
}
//Divide weight in kg by height in metres.
//Then divide the answer by height again to get BMI. 
You don't need First_BMI or Second_BMI. Just write

Final_BMI = Weight/(Height * Height);

immediately after line 11

[EDIT]
If you want to keep it like it is you need to call CalculateFirstBMI();, CalculateSecondBMI();, and SetFinalBMI(); after line 11
Last edited on
Yes that makes it simpler, oh and the order of code, I should remember that for next time. Thanks for your help.
There is just one more thing if anyone could help me, the result of the calculation just doesn't add up!
-1.07374E+008. What is this lol?

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

class BMI{
public:
	BMI(){
		cout << "Enter your weight in kilograms(KG)!" << endl;
		cin >> Weight;
		cout << "Now enter your height in metres(M)!" << endl;
		cin >> Height;
	}
	float SetFinalBMI(){
		Final_BMI = Weight/(Height*Height);
	}
	float GetWeight(){
		cout << "Your weight is " << endl;
		return Weight;
	}
	float GetHeight(){
		cout << "Your height is " << endl;
		return Height;
	}
	float TotalBMI(){
		return Final_BMI;
	}
private:
	float Weight;
	float Height;
	float Final_BMI;
};

int main(){
	BMI BMI;
	cout << "Your BMI is " << BMI.TotalBMI() << "." << endl;
}
//Divide weight in kg by height in metres.
//Then divide the answer by height again to get BMI. 
-1.07374E+008. What is this lol?
It's the value of the uninitialized variable Final_BMI, I'd guess.

SetFinalBMI() is not called (maybe after the ominous line 11?).
SetFinalBMI() should return void
Hm I still have the same problem, driving me mad now because it's a pretty simple program that doesn't work properly :/
closed account (Dy7SLyTq)
please post your 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
//BMI
#include <iostream>
using namespace std;

class BMI{
public:
	BMI(){
		cout << "Enter your weight in kilograms(KG)!" << endl;
		cin >> Weight;
		cout << "Now enter your height in metres(M)!" << endl;
		cin >> Height;
	}
	void SetFinalBMI(){
		Final_BMI = Weight/(Height*Height);
	}
	float GetWeight(){
		cout << "Your weight is " << endl;
		return Weight;
	}
	float GetHeight(){
		cout << "Your height is " << endl;
		return Height;
	}
	float TotalBMI(){
		return Final_BMI;
	}
private:
	float Weight;
	float Height;
	float Final_BMI;
};

int main(){
	BMI BMI;
	cout << "Your BMI is " << BMI.TotalBMI() << "." << endl;
}
//Divide weight in kg by height in metres.
//Then divide the answer by height again to get BMI.  
See this:
1
2
3
4
5
6
7
8
9
10
11
...
class BMI{
public:
	BMI(){
		cout << "Enter your weight in kilograms(KG)!" << endl;
		cin >> Weight;
		cout << "Now enter your height in metres(M)!" << endl;
		cin >> Height;
		SetFinalBMI(); // Note!
	}
...
I see it now, appreciate it coder!! So if you're using a constructor, any function you want to use in that same class has to be *called* within the constructor.
Last edited on
closed account (NyqLy60M)
@ ALEXCX2PLUS:

You don't initialize 'functions', which are called methods or member functions when outside of your main source - "SetFinalBMI();" inside of your constructor is just calling the method. If you wanted to, you could call it from within your main() function.
Yea! I just wanted to make it look tidy ;)
closed account (Dy7SLyTq)
making it tidy would be calling in main. you can either call a class function within a class using *this or outside using an instance of the class
any function you want to use in that same class has to be *called* within the constructor.
No. Normally you should initialize your member variable within the constructor. More complex stuff should be done in a separate function. E.g.:
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
//BMI
#include <iostream>
using namespace std;

class BMI{
public:
	BMI() :  // Note
	  Weight(),
	  Height(),
	  Final_BMI() { }

	void SetFinalBMI(){  // Note
		cout << "Enter your weight in kilograms(KG)!" << endl;
		cin >> Weight;
		cout << "Now enter your height in metres(M)!" << endl;
		cin >> Height;
		Final_BMI = Weight/(Height*Height);
	}
	float GetWeight(){
		cout << "Your weight is " << endl;
		return Weight;
	}
	float GetHeight(){
		cout << "Your height is " << endl;
		return Height;
	}
	float TotalBMI(){
		return Final_BMI;
	}
private:
	float Weight;
	float Height;
	float Final_BMI;
};

int main(){
	BMI BMI;
	BMI.SetFinalBMI(); // Note
	cout << "Your BMI is " << BMI.TotalBMI() << "." << endl;
}
//Divide weight in kg by height in metres.
//Then divide the answer by height again to get BMI.   
Last edited on
Topic archived. No new replies allowed.