0

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

   }
}
Kyotosomo
  • 11
  • 2
  • 5
    Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – LogicStuff Jan 27 '16 at 21:44
  • 1
    Simply put, after you do `cin >> userInput;`, `std::getline` will read what's left on that same line. Calling `userCommand[0]` on empty `userCommand` is also UB. – LogicStuff Jan 27 '16 at 21:45
  • 1
    Possible duplicate of [cin and getline skipping input](http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input) – Barmar Jan 27 '16 at 21:58

2 Answers2

2

@acid1789 has pointed out correctly the error is due to the empty line after getline. From your code what i can make out is you are using an infinite loop here

while(creditCount >= 0){...}

since creditCount is never going to be less than 0 instead of this you can use

while (true){...}

its just better programming practice.

And to correct your program you can just use

cin >> userCommand;

instead of

getline(cin, userCommand);

Hope it helps.

Edit:- Sorry Python habits..

1

The issue here is that getline is returning an empty line.

So after your first time through the loop, it goes back to the top to read another line. Gets an empty line, which triggers the error case.

You could fix this by testing for an empty line after getline.

acid1789
  • 129
  • 4