我无法弄清楚为什么这个循环发生故障

时间:2016-01-27 21:39:21

标签: c++

所以我无法弄清楚我的代码有什么问题。当我处理购买时,在我输入酸奶的数量后,我被告知要去享受它,我立即收到错误消息后,如果人们没有输入P或S,那么之后它会恢复正常状态当你第一次进入该计划时,有人可以告诉我为什么会这样吗?谢谢!

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

   }
}

2 个答案:

答案 0 :(得分:2)

@ acid1789已正确指出错误是由于getline之后的空行造成的。从你的代码我可以得出的是你在这里使用无限循环

$conn

因为creditCount永远不会小于0而不是你可以使用

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

它更好的编程实践。

要更正您的程序,您只需使用

即可
while (true){...}

而不是

cin >> userCommand;

希望它有所帮助。

编辑: - 抱歉Python习惯..

答案 1 :(得分:1)

这里的问题是getline返回一个空行。

因此,在您第一次完成循环后,它会返回到顶部以读取另一行。获取一个空行,触发错误情况。

你可以通过在getline之后测试一个空行来解决这个问题。