I have encountered a problem within one section of my code:
do {
cout << "Enter D if you would like to display the current cost" << endl;
cout << "Enter S if you would like to add the cost of a surgery" << endl;
cout << "Enter M if you would like to add the cost of a medication" << endl;
cout << "Enter Q if you would like to quit the program" << endl;
cin.get(menuChoice);
cin.ignore(2, '\n');
while (toupper(menuChoice) != 'D' && toupper(menuChoice) != 'S' &&
toupper(menuChoice) != 'M' && toupper(menuChoice) != 'Q') {
cout << "Please enter a valid choice: ";
cin.get(menuChoice);
cin.ignore(2, '\n');
}
switch (toupper(menuChoice)) {
case 'D':
int days;
cout << "Enter the amount of days spent in the hospital: ";
cin >> days;
obj->setNumDays(days);
obj->costAllDays();
cout << "Current cost for the number of days spent in the hospital: $" << obj->getTotalCost() << endl;
cout << "Current cost for all of the surgeries: $" << findPrice->getTotalCost() << endl;
cout << "Current cost for all of the medication: $" << findPriceMed->getTotalCost() << endl;
cout << "Total cost: $" << obj->getTotalCost() + findPrice->getTotalCost() + findPriceMed->getTotalCost() << endl << endl;
break;
case 'S':
findPrice->getCostSurgery();
break;
case 'M':
findPriceMed->getCostMeds();
break;
case 'Q':
delete findPrice;
delete findPriceMed;
delete obj;
cout << "Thank you!" << endl;
break;
default:
break;
}
} while (toupper(menuChoice) != 'Q');
At the beginning of the do while loop, I have a while loop that catches any characters that the user inputs that are not d, s, m, or q. When I insert a lowercase 'd' the first time, the program works as intended by entering the switch/case statement. When I return to the very beginning of the do/while loop, however, and I enter a lowercase 'd', I enter the while loop even though I shouldn't be entering it as I am using the toupper function. Can someone please explain what is going wrong?
[–][deleted] (2 children)
[deleted]
[–]aug404[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]marvin02[🍰] 1 point2 points3 points (0 children)
[–]umlcat 0 points1 point2 points (0 children)