How to exit the program and and continue

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

int main()
{
float weight, height, bmi;
cout<<"Enter weight in kg and height in metres";
cin>>weight>>height;

bmi = weight/(height*height);

if(bmi < 18.5)
cout<<"Underweight";
else
if(bmi>18.5 && bmi<24.99)	
cout<<"Normal weight";
else
if(bmi>25 && bmi<29.99)
cout<<"Overweight";
else 
if(bmi>30 && bmi <34.99)
cout<<"Obesity (Class 1)"
else
if(bmi>35 && bmi<39.99)
cout<<"Obesity (Class 2)";
else
cout<<"Morbid Obesity"	
}


I think the above code is self explanatory. It displays BMI category according to weight and height entered by person. Till this I have done. The last line of question says :Program should be able to check after each calculation if the person wants to exit the program or continue by providing response as "yes" or "no". How to do this part?
Last edited on
You'd need a loop.
something like this perhaps.
1
2
3
4
5
6
7
8
9
10
    char ok = 'y';
    while (ok == 'y')
    {
        cout << "something..\n";
        cout << "something else..\n";
        
        
        cout << "Do you wish to continue? y or n \n";
        cin >> ok; 
    }


However, there is a more important problem to be fixed. Because of the way you have worded the if-else statements, there are a lot of gaps, in-between values which are not caught until the very last else statement.

Any of these values for bmi will output "Morbid Obesity".
    18.5, 24.99, 24.995, 25.0, 29.99, 29.995, 30.0,
    34.99, 34.995, 35.0, 39.99, 39.995, 40.00, 41.0 
Last edited on
Another way to it:
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
while(true)
{
  cout<<"Enter weight in kg and height in metres";
  cin>>weight>>height;

  bmi = weight/(height*height);

  if(bmi < 18.5)
    cout<<"Underweight";
  else if(bmi>18.5 && bmi<24.99)	
    cout<<"Normal weight";
  else if(bmi>25 && bmi<29.99)
    cout<<"Overweight";
  else if(bmi>30 && bmi <34.99)
    cout<<"Obesity (Class 1)";
  else if(bmi>35 && bmi<39.99)
    cout<<"Obesity (Class 2)";
  else
    cout<<"Morbid Obesity";

  cout << "\nContinue (y/n) ? ";
  char answer;
  if (answer != 'y')
    break;
}

Thanks. Understood how to do it.I will take care of that gaps. Actually the question asked was with gaps
Topic archived. No new replies allowed.