While循环保持打印输出而不读取下一行
While loop keeps printing out and not reading the next line
这是我的第一个简单程序。它不停地打印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;
}
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 如何从一行中读取特定数量的字符?(C++)
- 读取最后一行代码算法 - c++ 时出现问题
- 而(!inputfile.eof())只读取第一行?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 需要从存档中读取一行文本.txt直到找到"hhh",然后转到下一行
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 从文件中的一行读取特定数据-C++
- 使用std::getline()读取一行
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- 从文件读取并在一行中写入 cout
- 将 ifstream 文件和每一行读取到数组中
- 从文件 c++ 中的下一行读取
- 在同一行读取具有多个类型的文件
- 写入终端并从同一行读取
- 如何在c++中从最后一行读取文本文件
- 逐行读取数组,每一行读取3个值
- 在同一行读取字符串和整数
- 从一行读取hh:mm格式的时间