I am working on a project(again) and am running into an issue with this code. I am trying to choose a "plan" and my cin works to type in a plan..but then repeats the "Please enter the number of prescriptions" part several times and the other cin won't allow me to enter anything.
userChoice is of type int. Hence cin >> userChoice will only accept an integer value. A B C are not integer values hence the >> fails, puts the input stream into fail mode so that subsequent cin >> statements also fail. Hence multiple output displays before the program terminates. Make userChoice type char.
L9
char userChoice {};
Also
L5 main has a return type of int
L6 - 10 - variables should be initialised when defined.
if ((userChoice == 'A')||(userChoice == 'a')) {
}
{
cout << "Please enter the estimated number of prescriptions (0 to 96): ";
cin >> userMeds;
if ((userChoice == 'B')||(userChoice == 'b')) {
}
{
cout << "Please enter the estimated number of prescriptions (0 to 96): ";
cin >> userMeds;
if ((userChoice == 'C')||(userChoice == 'c')) {
}
{
cout<< "Please enter the estimated number of presciptions (0 to 96): ";
cin >> userMeds;
cout << userMeds;
}
}
}
That does look suspicious. Now, without the ; / {}:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
if ((userChoice == 'A')||(userChoice == 'a')) {
cout << "Please enter the estimated number of prescriptions (0 to 96): ";
cin >> userMeds;
if ((userChoice == 'B')||(userChoice == 'b')) {
cout << "Please enter the estimated number of prescriptions (0 to 96): ";
cin >> userMeds;
if ((userChoice == 'C')||(userChoice == 'c')) {
cout<< "Please enter the estimated number of presciptions (0 to 96): ";
cin >> userMeds;
cout << userMeds;
}
}
}
If userChoice is neither 'A' nor 'a', then the inner IFs are never evaluated.
If userChoice is 'A' or 'a', then the conditions in inner IFs are always false.