Function call error

Hey all,

I am writing a BMI calculation program, but i keep running into some problems.
I cant get the program to call another function further in the code. no matter what value i give it.

Could someone help this noob out?

Thanks in advance, im sure the answer is quite easy

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

using namespace std;

void geslacht(char M, char V);
void BMIM();
void BMIV();

int main()
{
    cout << "Welkom, dit programma berekent uw BMI\n";
    cout << "Wat is uw geslacht? \n Vul M in voor man en V voor vrouw \n"; 
    geslacht();

    return 0;
}

void BMIM()
{
    double lengte = 0.0, gewicht = 0.0;

    cout << "Wat is uw gewicht (in KG) ";
    cin >> gewicht;
    
    cout << "Hoe lang bent u (in cm) \n ";
    cin >> lengte;

    double score = 0.0;
    score = gewicht / (lengte * lengte);

    cout << "U";

    if (score >= 30)
        cout << " heeft obesitas\n";
    else if (score > 25)
        cout << " heeft overgewicht\n";
    else if (score >= 20)
        cout << "w BMI is normaal\n";
    else
        cout << " heeft ondergewicht \n";

    return;
}

void BMIV()
{
    double lengte = 0.0, gewicht = 0.0;

    cout << "Wat is uw gewicht (in KG) ";
    cin >> gewicht;
    
    cout << "Hoe lang bent u (in cm) \n ";
    cin >> lengte;

    double score = 0.0;
    score = gewicht / (lengte * lengte);

    cout << "U";

    if (score >= 30)
        cout << " heeft obesitas\n";
    else if (score > 24)
        cout << " heeft overgewicht\n";
    else if (score >= 19)
        cout << "w BMI is normaal\n";
    else
        cout << " heeft ondergewicht \n";

    return;
}

void geslacht(char M, char V)
{
    float geslacht;
    cin >> geslacht;
    if (geslacht = M)
        BMIM();
    if (geslacht = V)
        BMIV();
}
Last edited on
Hello Rlabee,

1
2
3
4
5
6
7
8
9
10
11
12
void geslacht(char M, char V)
{
    float geslacht;  //  <--- Bels to use a "double".

    cin >> geslacht;  //  <--- Expects a number. A floating point number.

    if (geslacht = M)  //  Trying to set a floating point number equal to a "chat". Not possible.
        BMIM();

    if (geslacht = V)
        BMIV();
}

"=" means to set and "==" means to compare. Even as "==" you are comparing a floating point number to a "char".

I think what you are looking for is:
1
2
3
4
5
6
7
8
9
10
11
12
void geslacht(char M, char V)
{
    char geslacht;

    cin >> geslacht;

    if (geslacht == 'M' || geslacht == 'm')
        BMIM();

    if (geslacht = V)
        BMIV();
}

You will need to fix the other if statement, but that should solve the problem and call the right function.

The other problem I see is that line 13 in "main" calls the function with no parameters, but the function is expecting 2 parameters.

The prototype, Line 6 must match the function on line 73 and the function call needs to match the function definition, but in the function call only the names are needed not the type.

Note: Notice the use of blank lines and how it makes the code easier to read. The first benefit is to you and the second benefit is to those who have to read your code. The easier it is to read the quicker the response.

Andy
That makes sense.

just dont know what parameter i should use in line 14. tried a lot but nothing seems to work.

I notice the space between the lines and it , indeed, makes it easier to reas. going to use it in my advantage in the future!
it's your function, ¿why do you ask for parameters if you have no idea what you want? ¿do you even use the parameters?


> Hoe lang bent u (in cm)
meters, the formula that you are using requires the height in meters
Yes you're right. Thanks for noticing.

I completely understand what Handy Andy was saying now and the problem is solved.
As you can say im new to programming and wasnt sure what parameters were and how to use everything.

new to all the terms.

But thanks anyway ne555.
Hello Rlabee,

If you are still having a problem with functions have a look at this:

https://www.learncpp.com/

Chapter 2 Introduction to functions
https://www.learncpp.com/cpp-tutorial/introduction-to-functions/

I have always found that their pages are easy to follow and the example code is good.

I would like to see what you have changed if youdo not mind.

Andy
Hi andy!

Thanks, just had a quick look at the site and it explains a lot in an easy way. thanks for te tip!
the code i came up with is:

Right now im looking to make it a bit more compact.
maybe make another function call, but not sure how or where.
I would like to put a value back to "main" not sure why but just to try somethings.
Last edited on
Hello Rlabee,

The basic idea of the program looks good. You have the prototypes, function definition and function calls correct now.

I have not looked into the functions yet, but there is most likely a way to shorten them.

A change you could make is:
1
2
3
4
#include <cctype>  // <--- std::tolower() and std::toupper + others functions.

if (std::toupper(geslacht) == 'M')
        BMIM();

It may not seem like much, but it helps. "cctype" has other functions that are useful. http://www.cplusplus.com/reference/cctype/

Another shorter way of initializing number variables, since C++11 on, double lengte{}. The empty {}, the uniform initialize, will set the variable to the type of zero based on the variables type. In this case it would be "0.0". An "int" would be zero and a "char" would be "\0".

Andy
Topic archived. No new replies allowed.