Body Mass Index Program with void function

I'm studying functions, functions prototypes, declarations. First I tried to create a function with a string return. Then I figured out it is complicated. Here is my last attempt to calculate BMI. I want this BMI() function to stand separate from main() function. Unfortunately I get errors. Here is the 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
#include <iostream>
void BMI();

int main ()
{
    std::cout << "HELLO !!! " << std::endl;
    std::cout << BMI() << std::endl;
    return 0;
} 

void BMI()
{
    double height, weight, score;
    std::cout << "How much do you weigh? ";
    std::cin >> weight;
    std::cout << "How tall are you? ";
    std::cin >> height;
    score = weight / (height * height );
    if (score < 18.5 ) 
    {
        std::cout << "THIN\n";
    }
    if (score > 18.5 && score < 25)
    {
        std::cout << "NORMAL\n";
    }
    if (score > 25 && score < 30)
    {
        std::cout << "FAT\n";
    }
    else
    {
        std::cout << "OBESE\n";
    }
}
Last edited on
std::cout does not understand void value. So instead of :

std::cout << BMI() << std::endl;

You need to write this :

BMI(); // Just call the function
Thank you very much. I read "Sams Teach Yourselff C++ in 21 Days" and at the same time I compile and run my own imagination to exercise. Now I understand. My BMI is a void. So I can't std::cout it. It must be something (int, float, char etc.) except void. Am I right?
Last edited on
It must be something (int, float, char etc.) except void.

Yes exactly. But you need to return a value in your function if you use something other than void.
Last edited on
closed account (48T7M4Gy)
Another way:
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
#include <string>
#include <iostream>

std::string BMI(double, double);

int main ()
{
    std::cout << "HELLO !!! " << std::endl;
    
    double height = 0.0, weight = 0.0;
    
    std::cout << "How much do you weigh? ";
    std::cin >> weight;
    
    std::cout << "How tall are you? ";
    std::cin >> height;
    
    std::cout << "Your BMI is " << BMI(weight, height) << std::endl;
    
    return 0;
}

std::string BMI(double aWeight, double aHeight)
{
    double score = 0.0;
    
    score = aWeight / (aHeight * aHeight );
    
    if (score >= 30)
        return "OBESE";
    else if(score > 25)
        return "FAT";
    else if (score >= 18.5)
        return "NORMAL";
    else
        return "THIN";
}
Thank you very much. I just wanted my main() to be empty as possible. Because I can add another functions, classes later. So it will seem clear.
closed account (48T7M4Gy)
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
#include <string>
#include <iostream>

void BMI();

int main ()
{
    std::cout << "HELLO !!! " << std::endl;
    BMI();
    
    return 0;
}

void BMI()
{
    double height = 0.0, weight = 0.0;
    
    std::cout << "How much do you weigh? ";
    std::cin >> weight;
    
    std::cout << "How tall are you? ";
    std::cin >> height;
    
    double score = 0.0;
    score = weight / (height * height );
    
    std::cout << "You are ";
    
    if (score >= 30)
        std::cout << "OBESE\n";
    else if(score > 25)
        std::cout << "FAT\n";
    else if (score >= 18.5)
        std::cout << "NORMAL\n";
    else
        std::cout << "THIN\n";
    
    return;
}
Topic archived. No new replies allowed.