BMI calculator

Sep 29, 2020 at 9:56pm
Write your question here.

Hi,
I have to write a program who calculates the BMI for a male or female, but i got a arror: uninitialized local variable 'geslacht'(gender). Maybe someone knows how to fix?


using namespace std;

int main()
{

float geslacht, bmi, g, l; //g=gewicht, l=lengte
cout << "Voer uw geslacht in (man = 1, vrouw = 2): "<< geslacht << endl;
cout << "Voer uw gewicht in kg aub : " << endl;
cin >> g;
cout << "Voer uw lengte in cm aub : " << endl;
cin >> l;
bmi = (g / ((l / 100) * (l / 100)));
cout << "Uw BMI : " << bmi << endl;
if (geslacht = 1)
if (bmi > 25)
cout << "overgewicht";
else if (bmi < 25 && bmi>20)
cout << "dat is prima";
else
cout << "ondergewicht";

{if (geslacht = 2)
if (bmi > 24)
cout << "overgewicht";
else if (bmi < 19 && bmi>24)
cout << "dat is prima";
else
cout << "ondergewicht";
}
return 0;
}
Sep 29, 2020 at 10:09pm
You try to cout it before you’ve given it a value.

Later on you have incorrectly used = (assignment) when you need ==(compare for equality).

And geslacht is probably better as an int.
Sep 29, 2020 at 10:53pm
Hello MauriceF,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



float geslacht, bmi, g, l; //g=gewicht, l=lengte . Why not do
double geslacht{}, bmi{}, gewicht{}, lengte{}. Always a good idea to initialize your variables.

Andy
Last edited on Sep 29, 2020 at 10:53pm
Sep 30, 2020 at 3:15am
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
#include <iostream>

using namespace std;

int main()
{
    double geslacht{0}, bmi{0}, gewicht{0}, lengte{0}; // <--
    
    cout << "Voer uw geslacht in (man = 1, vrouw = 2): "; // <--
    cin >> geslacht; // <--
    
    cout << "Voer uw gewicht in kg aub : " << endl;
    cin >> gewicht;
    
    cout << "Voer uw lengte in cm aub : " << endl;
    cin >> lengte;
    
    bmi = (gewicht / ((lengte / 100) * (lengte / 100)));
    cout << "Uw BMI : " << bmi << endl;
    
    if (geslacht == 1) // <--
    {
        if (bmi > 25)
            cout << "overgewicht";
        
        else if (bmi > 20) // <--
            cout << "dat is prima";
        else
            cout << "ondergewicht";
    }
    
    if (geslacht == 2) // <--
    {
        if (bmi > 24)
            cout << "overgewicht";
        else if (bmi > 19) // <--
            cout << "dat is prima";
        else
            cout << "ondergewicht";
    }
    
    return 0;
}
Sep 30, 2020 at 3:20am
Also, if not already mentioned, the type of geslacht should be int, not double/float
Topic archived. No new replies allowed.