[英]C++ cin.ignore and getline in while loop
I'm very new to programming, and I'm having trouble using getline in a while loop. 我对编程很新,而且我在while循环中使用getline时遇到了麻烦。 When I cout the course variable the first letter is missing. 当我输出课程变量时,第一个字母丢失了。 Getting rid of cin.ignore sends it into an infinite loop. 摆脱cin.ignore将其发送到无限循环中。
Here's what I have so far: 这是我到目前为止所拥有的:
#include <iostream>
#include <string>
using namespace std;
int main (){
string answer = "Yes";
string course;
while (answer == "Yes"){
cin.ignore();
cout<< "Enter a course name: ";
getline (cin, course);
cout<< course << endl;
cout<< "Continue ('Yes' or 'No')? ";
cin>> answer;
cout<< answer << endl;
}
return 0;
}
Move the ignore
to the bottom of your loop. 将ignore
移动到循环的底部。 It's there to remove the newline character that the cin >>
operator leaves in the buffer, so you only need it after you've used cin >>
. 它可以删除cin >>
运算符在缓冲区中留下的换行符,因此在使用cin >>
之后只需要它。
You should also pass arguments to ignore
to ignore everything until you hit a newline in case they've entered more than just "Yes" or "No" on the line. 您还应该将ignore
参数传递给忽略所有内容,直到您输入换行符,以防它们输入的内容超过“是”或“否”。 You can do that with: 你可以这样做:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
The problem that I see is when you remove cin.ignore()
you get interaction as follows: 我看到的问题是,当您删除cin.ignore()
时,您将获得如下交互:
Enter a course name: Math 输入课程名称:数学
Math 数学
Continue ('Yes' or 'No')? 继续('是'还是'否')? Yes 是
Yes 是
Enter a course name: 输入课程名称:
Continue ('Yes' or 'No')? 继续('是'还是'否')? Yes // etc, etc... 是的//等等......
The second time it does not prompt you for class input. 第二次它没有提示你输入课程。 This is because the Enter/Return you use to submit the info is getting extracted by getline()
, which stops at the first '\\n' character it sees. 这是因为用于提交信息的Enter / Return是由getline()
提取的, getline()
在它看到的第一个'\\ n'字符处停止。
One way to fix it is use cin.ignore(), after your custom input. 修复它的一种方法是在自定义输入后使用cin.ignore()。 Mind that if reading from file, you should end the line after class input to get the same result as here. 请注意,如果从文件中读取,则应该在课程输入后结束行以获得与此处相同的结果。
while (answer == "Yes"){
// REMOVE cin.ignore() FROM HERE
cout<< "Enter a course name: ";
getline (cin, course);
cout<< course << endl;
cout<< "Continue ('Yes' or 'No')? ";
cin>> answer;
cout<< answer << endl;
{ // THIS IS MORE EFFICIENT
cin.ignore(); // ADD cin.ignore() HERE TO DISCARD '\n'
}
{ // THIS WORKS BETTER FOR HUMAN INPUT
string dummy;
getline (cin, dummy)
}
}
If you use getline
, stick to getline
. 如果你使用getline
,坚持使用getline
。 Mixing it with stream operations will easily mess things up. 将它与流操作混合将很容易搞砸。
#include <iostream>
#include <string>
int main() {
std::string answer = "Yes";
std::string course;
while (answer == "Yes") {
do {
std::cout << "Enter a course name: ";
std::getline(std::cin, course);
} while (course == "");
std::cout << course << '\n';
do {
std::cout << "Continue ('Yes' or 'No')? ";
std::getline(std::cin, answer);
} while (answer != "Yes" && answer != "No");
std::cout << answer << '\n';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.