I am trying to get my program to restart if the user enters a y, and terminiate if the user enters n.

What am I leaving off here, or what am I typing wrong?


#include <iostream>

using namespace std;

int BMICalc(int weight, int height);

int main()
    int weight ; // weight in pounds
    int height ; // height in inches
    float BMI ; // Body Mass Index
    bool DoItAgain ;
    char Repeat ;

    cout << " " << endl ;
    cout << "Body Mass Index (BMI) Calculator" << endl ;

    cout << " " << endl ;

    while (DoItAgain = true) ;

    cout << "Enter your weight in pounds. (please round to nearest whole number): " ;
    cin >> weight ;

    cout << "Enter your height in inches. (please round to nearest whole number): " ;
    cin >> height ;

    BMI = BMICalc(weight, height) ;

    cout << "Your BMI is" << BMI << endl ;

    if (BMI < 18.5)
        cout << "You are underweight." << endl ;

    if (BMI <= 24.9)
        cout << "Congratulations, you are a healthy weight." <<endl ;

    if (BMI <= 29.9)
        cout << "You are over weight." << endl ;

    if (BMI <= 39.9)
        cout << "You are obese." << endl ;

    if (BMI >= 40.0)
        cout << "You are severely obese." << endl ;

    cout << " " << endl ;
    cout << "Would you like to enter a new height and weight? (y or n): " ;
    cin >> Repeat ;

    return main () ;

    else (Repeat = 'n')
        DoItAgain = false ;
        return 0 ;
int BMICalc(int weight, int height)
    int wgt_kg ; // weight in kilograms
    int hgt_m ; // height in meters

    wgt_kg = (weight * 0.454) ; // kilograms = pounds * 0.454
    hgt_m = (height * 0.0254) ; // meters = inches * 0.0254)

    return (wgt_kg/(hgt_m*hgt_m)) ; // BMI = kg/m^2

Edited 3 Years Ago by Nick Evan: Fixed formatting

Firstly, please use code tags the next time: //code here
It makes it much easier to read and maintains the indentation properly.

So first thing is get rid of the return main() statement. It doesn't do anything. When you complete this while loop you're back in main anyway. There's also no need for the ; after the while. Get rid of the else etc as there's no if to that else (except leave the return 0; where it is at the end of main() )

Now at the end of the while loop you are getting input from the user in the variable Repeat. What you should do is eliminate your intermediate variable and use this Repeat value directly as the test for the while loop. So, change the while to while (Repeat =='y') (single quotes because it's a character). Now, so when your program gets to the while loop for the first time, what is Repeat equal to? Not 'y', so your loop won't run. But go back up to your declaration for Repeat and set Repeat ='y';
Now when you press n (or any other character except lowercase y) the loop test at the top will fail and you will be out of the while loop. If you want to go back and add 'Y' to your yes answers later it's not difficult.

There are a couple of other issues in your function. Make the variables in the body of the function floats because you're going to get some truncation with your int values. You can probably leave your inputs as ints, but make both Wgt_kg and hgt_m to be floats (or doubles) and change the return of the function to float (or double) since you have declared BMI in main to be of type float, the integer would be "promoted" to float, but you get more accuracy if you're returning a float (or double) into a float (or double) variable.

Okay, so take the changes one step at a time and post back with questions.

Edited 4 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.