簡體   English   中英

我不知道為什么此循環發生故障

[英]I Can't Figure Out Why This Loop Is Malfunctioning

所以我只是無法弄清楚我的代碼出了什么問題。 當我進行購買時,輸入酸奶量並被告知去享用酸奶后,我會立即收到錯誤消息,提示我如果人們不輸入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";
      }

   }
}

@ acid1789已正確指出錯誤是由於getline之后的空行。 根據您的代碼,我可以判斷出您在這里使用無限循環

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

由於creditCount永遠不會小於0,因此您可以使用

while (true){...}

它只是更好的編程實踐。

要更正您的程序,您可以使用

cin >> userCommand;

代替

getline(cin, userCommand);

希望能幫助到你。

編輯:-對不起Python習慣。

這里的問題是getline返回空行。

因此,在您第一次遍歷循環后,它將回到頂部以讀取另一行。 獲取一個空行,這將觸發錯誤情況。

您可以通過在getline之后測試一個空行來解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM