Asking a series of question - one at a time.

This program calculates the user's body mass index and tells the user whether they are overweight or healthy. I would like to modify the code so the program asks the user for some information (i.e their name), the user enters their name, and then the program asks "what is your weight". Like a human conversion! I only want "what is your weight" displayed after the user has entered in their name.

I'm really happy with this program. It's my first real program that does something useful


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;

main()
{

    
    
    string first_name; 			//input
    float your_weight_kg;		//input
    float your_height_metre;	//input
    float result;				//BMI result
    
    cout << "Please enter your First Name\n";
    cout << "Please enter your weight in KG (decimal)\n";
    cout << "Please enter your height in metres (decimal)\n";
    
    cin >> first_name;
    cin >> your_weight_kg;
    cin >> your_height_metre;
    
    
    // BMI formula
    
    result = (your_weight_kg / (your_height_metre * your_height_metre));
  
   //BMI result
    if (result > 25){
    	cout <<"\n" "Hello " << first_name << ", you may be overweight. A BMI score of " << result << " indicates you are overweight for your height. The BMI is a guide only and you should consult your doctor for a accurate accessment. ";  
    }
    else if ( result < 18 ) {            /* I use else just to show an example */ 
        cout <<"\n" "Hello " << first_name << ", you may be underweight.  A BMI score of " << result << " indicates you are underweight for your height. The BMI is a guide only and you should consult your doctor for a accurate accessment. ";    
    }
    else {
        cout <<"\n" "Hello " << first_name << ", Congratulations! You are a healthy weight.  A BMI score of " << result << " indicates you are a healthy weight. Try to maintain this weight.";
    }
    
    
    
}
    

Last edited on
After printing a question (using cout) then get the input (using cin).


Try this.
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
/* BMI */

#include <iostream>
using namespace std;

main()
{
    string first_name; 			//input
    float your_weight_kg;		//input
    float your_height_metre;	//input
    float result;				//BMI result
    
    cout << "Please enter your First Name\n";
    cin >> first_name;
    cout << "Please enter your weight in KG (decimal)\n";
    cin >> your_weight_kg;
    cout << "Please enter your height in metres (decimal)\n";
    cin >> your_height_metre;

    // BMI formula
    
    result = (your_weight_kg / (your_height_metre * your_height_metre));
  
   //BMI result
    if (result > 25){
    	cout <<"\n" "Hello " << first_name << ", you may be overweight. A BMI score of " << result << " indicates you are overweight for your height. The BMI is a guide only and you should consult your doctor for a accurate accessment. ";  
    }
    else if ( result < 18 ) {            /* I use else just to show an example */ 
        cout <<"\n" "Hello " << first_name << ", you may be underweight.  A BMI score of " << result << " indicates you are underweight for your height. The BMI is a guide only and you should consult your doctor for a accurate accessment. ";    
    }
    else {
        cout <<"\n" "Hello " << first_name << ", Congratulations! You are a healthy weight.  A BMI score of " << result << " indicates you are a healthy weight. Try to maintain this weight.";
    }   
}


Also, the user can input whole numbers not necessarily a decimal. It doesn't matter.
Stormboy wrote:
Also, the user can input whole numbers not necessarily a decimal.


What do you mean by this? Of course the user can enter decimals, @redback is inputting into a floating point! If I was inputting, all the following inputs for the "your_weight_kg" variable are valid:

-61.5
6141.36183956134
4.614e21
-3.6e-4


EDIT:
Also, please declare main() as type int, return something at the end (probably 0), don't forget to #include <string> at the top of your code and no need to concantenate the "\n" with your
Last edited on
Thanks Stormboy! That does the trick.
@ NT3:
I meant that the user can also input numbers like 50, 20 15. It doesn't necessarily have to be a decimal. I know the user can input decimals. I told that because the instruction which redback wrote tells the user to input a decimal number. So the user may be confused.
Last edited on
Topic archived. No new replies allowed.