C++ While If Condition y/n User Choice

Viewed 454

I'm learning c++ and am having trouble with my loop conditions. the code executes without error but the user is able to type in conditions other than 'y',' Y' ,'n','N' Why aren't the conditions working as specified without bugs? This is a BMI calendar that calculates BMI, exits upon user agency, and quits after three invalid cin >>> char choice

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
    //declare variables
    int height = 0;
    int weight = 0;
    int BMI = 0;
    int counter = 1;
    char choice;

    //Introduction
    cout << "Welcome!: ";
    cout << "This is a c++ program that calculates your Body Mass Indicator ";
    cout << "Would you like to continue?(Y/N) \n";
    while (cin >> choice)
    {

        //If the user selects yes then continue the program
        if (choice == 'y', 'Y')
        {
            //Get height/weight from user
            cout << "Please enter your height in inches: ";
            cin >> height;
            cout << "Please enter your weight in pounds: ";
            cin >> weight;

            //Calculate the user's BMI from input
            BMI = (weight * 703) / (height * height);
            if (BMI < 18.5)
                cout << "Your Body Mass Index is " << BMI << " and according to cdc.gov, you are underweight" << endl;

            else if ((BMI > 18.5) && (BMI < 24.9))
                cout << "Your Body Mass Index is " << BMI << " and according to cdc.gov, this is a normal weight." << endl;

            else if ((BMI > 25) && (BMI < 29.9))
                cout << "Your Body Mass Index is " << BMI << " and according to cdc.gov, this is considered overweight. Please see a health counselor or fitness trainer to discuss your options." << endl;

            else
                cout << "Your Body Mass Index is " << BMI << " and according to cdc.gov, this is considered Obese. Please see a health counselor or fitness trainer to discuss your options." << endl;
        }
        //Display goodbye if the user exists
        else if (choice == 'n', 'N')
        {
            cout << "NO Calculation Requested ";
            break;
        }
        else if (choice != 'n', 'N', 'y', 'Y') {
            counter++;
            cout << "Invalid input... please re-enter your choice ";
        }
        else if (counter == 3) {
            cout << "Number of Invalid choices Exceeded – Try Again Later ";
        }

        return 0;

    }
}
1 Answers

if (choice =! 'y', 'Y') should be if (choice == 'y' || choice == 'Y').

Let's unpack why that line is bad.

Inequality in C++ is expressed with !=. What you wrote, =! doesn't exist in the language. And you want to enter the block if the user input indicated "yes". So choice needs to be equivalent to either one of 'y' OR 'Y'.

Next, the comma operator has special meaning in C++, and it's not what you were hoping for. What it really does is evaluate the expression on the left, discard the result, and then evaulate the expression on the right. Your right-side expression is just 'Y', which isn't a zero, so the if statement would always evaluate to true, if the code compiled.

You need to make similar changes to these lines:
else if (choice == 'n', 'N')
else if (choice != 'n', 'N', 'y', 'Y') {

What's interesting/fun is the inconsistency. You make the comparisons correctly with regards to BMI.

Related