So I just can't figure out what's wrong with my code. When I process a purchase, after I enter the amount of yogurt and am told to go enjoy it, I immediately after get the error message I made for if people don't input P or S, then right afterwards it goes back to normal like it is when you first enter the program, could anybody tell me why this is? Thank you!
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string userCommand;
int creditCount = 0;
int userInput;
while (creditCount >= 0)
{
cout << "--------Menu--------\n" << "P (Process Purchase)\n" << "S (Shutdown System)\n"
<< "--------Menu--------\n" << "Your Choice: ";
getline(cin, userCommand);
if (userCommand[0] == 'P' || userCommand[0] == 'p')
{
if (creditCount >= 10)
{
cout << "You qualify for a free yogurt, would you like to use ten of your credits (Yes or No)?";
getline(cin, userCommand);
if (userCommand[0] == 'Y' || userCommand[0] == 'y')
{
creditCount = creditCount - 10;
cout << "You just used 10 credits and now have " << creditCount << " left.\n"
<< "Enjoy your free yogurt!";
}
else if (userCommand[0] == 'N' || userCommand[0] == 'n')
{
cout << "How many yogurts would you like to buy? ";
cin >> userInput;
if (userInput > 0)
{
creditCount = creditCount + userInput;
cout << "You just earned " << userInput << " stamps. You now have " << creditCount
<< " credits! Enjoy your yogurt!";
}
else
{
cout << "Invalid input, please try processing your purchase again and enter a positive "
<< "integer.";
}
}
else
{
cout << "*Error, please try processing your purchase again and enter either Yes or No*\n\n";
}
}
else if (creditCount < 10)
{
cout << "How many yogurts would you like to buy? ";
cin >> userInput;
if (userInput > 0)
{
creditCount = creditCount + userInput;
cout << "You just earned " << userInput << " stamps. You now have " << creditCount
<< " credits! Enjoy your yogurt!\n\n";
}
else
{
cout << "Invalid input, please try processing your purchase again and enter a positive "
<< "integer.";
}
}
}
else if (userCommand[0] == 'S' || userCommand[0] == 's')
{
cout << "*Shutting down system*\n";
return 0;
}
else
{
cout << "*Error, please enter either P (for Process Purchase) or S (for Shutdown System)*\n\n";
}
}
}