【发布时间】: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