【问题标题】:While loop keeps printing out and not reading the next lineWhile 循环不断打印出来,而不是读取下一行
【发布时间】:2015-06-03 18:46:20
【问题描述】:

这是我的第一个简单程序。它不停地打印出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 循环仅假定 while (conti) 之后的第一行应该重复,因为您没有告诉它任何不同。
  • 这些反对票有点不公平。 OP说这是他的第一个程序。做个好人……记住你是怎么开始的……

标签: c++ printing while-loop


【解决方案1】:

你需要使用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";

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      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.";
         }
      }
      

      【讨论】:

        【解决方案4】:

        你有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;
        }
        

        【讨论】:

          【解决方案5】:

          您缺少两个 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;
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-01-04
            • 2012-02-14
            • 2014-02-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多