C++ 而loop一直在打印,不读取下一行

C++ 而loop一直在打印,不读取下一行,c++,printing,while-loop,C++,Printing,While Loop,这是我的第一个简单程序。它不断打印出猜猜它是什么。不间断,甚至不要求用户输入。(下一行代码。) 我犯了什么错 #include <iostream> #include <string> using namespace std; int main() { string userName; cout << "Hello there.\n"; cout << "My name is TARS. \n"; cout

这是我的第一个简单程序。它不断打印出
猜猜它是什么。
不间断,甚至不要求用户输入。(下一行代码。) 我犯了什么错

 #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;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串用户名;

不能您需要为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;

}
#包括
#包括
使用名称空间std;
int main()
{
字符串用户名;
cout
while(续)

cout您错过了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;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串用户名;

CUT

你缺少了两个卷轴> <代码>来扩展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;

}
#包括
#包括
使用名称空间std;
int main()
{
字符串用户名;

不能把应该重复的代码放在大括号里。你的while循环只假设
while(conti)
之后的第一行应该重复,因为你没有告诉它任何不同。这些否决票有点不公平。OP说这是他的第一个程序。做个好人…记住你是怎么开始的。。
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.";
   }
}
#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;
}
#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;

}