while循环保持打印而不读取下一行

时间:2015-06-03 18:46:21

标签: c++ printing while-loop

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

5 个答案:

答案 0 :(得分:2)

你需要为while循环使用大括号,否则它将永远执行该单个语句。

答案 1 :(得分:2)

您需要使用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 :(得分:1)

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

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

}