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?

code:

#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
}

Dani AI

Generated

A few distinct problems are stopping this from working. The while in the original code uses an assignment and a stray semicolon, which either creates an empty/infinite loop or prevents the following block from being the loop body. There’s also a recursive return main() (don’t use recursion to repeat the program), an uninitialized boolean, and the BMI checks use separate if statements so one value can trigger multiple messages. ’s tip about using floating types is correct — use double for the calculation.

A cleaner, more robust control flow is a repeat loop that runs once and asks the user whether to continue. Compare the response case‑insensitively and avoid reentering main. Example pattern:

char reply = 'y';
do {
    // read inputs, compute and print BMI
    std::cout << "Repeat? (y/n): ";
    std::cin >> reply;
} while (std::tolower(reply) == 'y');

Use a proper floating-point BMI calculation and avoid truncation. Use a precise conversion (1 lb = 0.45359237 kg, 1 in = 0.0254 m) and return a double. Then use an if/else if chain so each BMI falls into exactly one category:

double bmi_calc(int pounds, int inches) {
    double kg = pounds * 0.45359237;
    double m  = inches * 0.0254;
    return kg / (m * m);
}

Other practical tips: check std::cin for input failure and clear the stream if needed, format the output with std::fixed and std::setprecision, accept both Y and y (use <cctype>), and compile with warnings enabled (e.g. -Wall -Wextra) to catch assignment-in-condition or unused-variable mistakes. Remove return main() entirely — let the loop control repetition. These changes address the logic, precision, and robustness issues raised in the thread and build on ’s suggestions.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.