While循环保持打印输出而不读取下一行

While loop keeps printing out and not reading the next line

本文关键字:一行 读取 环保 循环 打印 输出 While      更新时间:2023-10-16

这是我的第一个简单程序。它不停地打印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;
}

您需要使用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循环需要大括号,否则它将永远只执行一条语句。

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

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

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