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