while循环不断打印出来而不是读取下一行

While loop keeps printing out and not reading the next line

这是我的第一个简单程序。它不停地打印 Guess what it is.,甚至不要求用户输入。 (下一行代码。) 我的错误是什么?

 #include <iostream>
 #include <string>
 using namespace std;

int main()
{
    string userName;
    cout << "Hello there.\n";
    cout << "My name is TARS. \n";
    cout << "What is your name? \n";
    getline(std::cin, userName);
    cout << userName << ", let's play a game.\n";
    int secretNum;
    secretNum = rand() % 20 + 1;
    cout << "I'm thinking of a number between 1-20.\n";
    int Guess;
    bool conti = true;

    while (conti)

        cout << "Guess what it is. \n";
        cin >> Guess;   

        if (Guess == secretNum)
        {
            cout << "You read my mind!";
            conti = false;
        }
        if (Guess < secretNum)
        {
            cout << "That is too low.";
        }
        if (Guess > secretNum)
        {
            cout << "That is too high.";
        }

    return 0;
}

while 循环需要大括号,否则它将永远只执行一条语句。

您需要使用brackets

#include <iostream>
#include <string>
using namespace std;

int main()
{

    string userName;
    cout << "Hello there.\n";
    cout << "My name is TARS. \n";
    cout << "What is your name? \n";
    getline(std::cin, userName);
    cout << userName << ", let's play a game.\n";
    int secretNum;
    secretNum = rand() % 20 + 1;
    cout << "I'm thinking of a number between 1-20.\n";
    int Guess;
    bool conti = true;

    while (conti)
    {

         cout << "Guess what it is. \n";
         cin >> Guess;

         if (Guess == secretNum)
         {
             cout << "You read my mind!";
             conti = false;
         }

         if (Guess < secretNum)
         {
             cout << "That is too low.";
         }

         if (Guess > secretNum)
         {
             cout << "That is too high.";
         }

    }

    return 0;

}

默认情况下,如果您不使用它们,则只有下一行将被视为 while 循环的一部分 在你的情况下:

while (conti)
    cout << "Guess what it is. \n";
while (conti)
cout << "Guess what it is. \n";

相当于:

while (conti)
{
   cout << "Guess what it is. \n";
}

即循环到此结束。您需要的是在正确的位置为循环提供左括号和右括号。

while (conti)
{
   cout << "Guess what it is. \n";
   cin >> Guess;
   if (Guess == secretNum)
   {
       cout << "You read my mind!";
       conti = false;
   }
   if (Guess < secretNum)
   {
       cout << "That is too low.";
   }
   if (Guess > secretNum)
   {
       cout << "That is too high.";
   }
}

您有 missed the braces 个 for while 循环。你可以试试这个:

#include <iostream>
#include <string>
using namespace std;

int main()
{

    string userName;

    cout << "Hello there.\n";
    cout << "My name is TARS. \n";
    cout << "What is your name? \n";
    getline(std::cin, userName);
    cout << userName << ", let's play a game.\n";

    int secretNum;
    secretNum = rand() % 20 + 1;

    cout << "I'm thinking of a number between 1-20.\n";

    int Guess;
    bool conti = true;

    while (conti){

        cout << "Guess what it is. \n";
        cin >> Guess;

        if (Guess == secretNum)
        {
            cout << "You read my mind!";
            conti = false;
        }

        if (Guess < secretNum)
        {
            cout << "That is too low.";
        }

        if (Guess > secretNum)
        {
            cout << "That is too high.";
        }

    }
    return 0;
}

您缺少两个 curly braces 来扩大 while 循环的范围。请注意,如果没有花括号,C++ 中任何循环的范围都将在第一个分号处停止。这是一个可行的解决方案:

#include <iostream>
#include <string>
using namespace std;

int main()
{

      string userName;

      cout << "Hello there.\n";
      cout << "My name is TARS. \n";
      cout << "What is your name? \n";
      getline(std::cin, userName);

      cout << userName << ", let's play a game.\n";

      int secretNum;
      secretNum = rand() % 20 + 1;

      cout << "I'm thinking of a number between 1-20.\n";

      int Guess;
      bool conti = true;

      while (conti) 
      {  // <-- This curly brace was missing

          cout << "Guess what it is. \n";
          cin >> Guess;

          if (Guess == secretNum)
          {
              cout << "You read my mind!";
              conti = false;
          }

          if (Guess < secretNum)
          {
              cout << "That is too low.";
          }

          if (Guess > secretNum)
          {
            cout << "That is too high.";
          }


      } // <-- This curly brace was also missing

      return 0;

}