如何为我的 getline() 修复这个 cin.ignore()
How do I fix this cin.ignore() for my getline()?
所以我为c ++编写了一个简单的程序,我遇到了这个问题。该程序基本上要求用户输入,并且它将跳过输入后的任何空格。我试图在用户输入后使用cin.ignore()
,但它不起作用。我将如何解决它?
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout << "Your name: ";
getline(cin, s);
cin.ignore();
cout << "You entered: " << s << endl;
return 0;
}
为什么要
使用std::cin.ignore()
?假设您想跳过潜在的前导空格,您可能需要使用 std::ws
std::getline(std::cin >> std::ws, s);
相关文章:
- C++中的cin.ignore()函数不适用于整个流
- C++ cin.ignore() 跳过循环
- 更好的方法,而不是在C ++中使用cin.ignore
- C++ - 如何知道在哪里放置 cin.ignore
- C++:忽略第一个 cin.ignore 之后的输入
- cin.ignore() 正在产生不需要的行为
- 如何将光标移动到 cin.clear() 和 cin.ignore() 之后的文本输出末尾?
- C++ cin.ignore and getline in while loop
- CIN被跳过,Cin.ignore不起作用
- CIN.IGNORE删除第一个字符的第二次函数被调用
- cin.ignore(1)之间的分歧是什么?和cin.ignore(n)
- 使用 cin.ignore 和 cin.clear CPP 时的奇怪行为
- 为什么在"cin"后使用 "getline" 时需要 cin.ignore(),而多次使用 "cin" 时不需要?
- getline删除第一个字符;删除cin.ignore();无法解决
- C++怪异的cin.ignore行为
- Cin 不会暂停输入,尽管同时使用了 cin.clear() 和 cin.ignore()
- 当循环不中断时,使用 cin.ignore 和 cin.clear() 来验证输入
- CIN.IGNORE如何与CIN和Getline一起工作
- 尽管Cin.ignore(),CIN不等待输入
- cin.clear() 和 cin.ignore() 不起作用