在 cin [duplicate] 之后使用 getline(cin, s)

Using getline(cin, s) after cin [duplicate]

提问人:pauliwago 提问时间:4/21/2011 最后编辑:Michael Curriepauliwago 更新时间:7/28/2020 访问量:98208

问:

我需要以下程序来获取用户输入的整行并将其放入字符串名称中:

cout << "Enter the number: ";
int number;
cin >> number;

cout << "Enter names: ";
string names;

getline(cin, names);

但是,在命令之前使用命令(我猜这是问题所在),它不允许我输入名称。为什么?cin >> numbergetline()

我听说过一些关于命令的事情,但我不知道这是如何工作的,也不知道为什么这是必要的。cin.clear()

C++ GetLine CIN

评论

23赞 Martin York 4/21/2011
假设您键入:.然后读作 JUST 5。在流上保留换行符 (Enter)。因此,当您尝试阅读带有它的名称时,它会读到行尾。但请注意,那里有一个换行符可供阅读(因此名称将为空(因为您没有读出 5 之后的换行符)。如果你想在 >> 和 getline() 之间切换,你需要注意输入上行的尾部。5<enter>John<enter>cin >> numbergetline(cin,name)
0赞 Lightness Races in Orbit 5/30/2015
@LokiAstari:这比下面发布的任何一个答案都要好。你能这样发布吗?

答:

11赞 Tony Delroy 4/21/2011 #1
cout << "Enter the number: ";
int number;
if (cin >> number)
{
    // throw away the rest of the line 
    char c;
    while (cin.get(c) && c != '\n')
        if (!std::isspace(c))
        {
            std::cerr << "ERROR unexpected character '" << c << "' found\n";
            exit(EXIT_FAILURE);
        }
    cout << "Enter names: ";
    string name;
    // keep getting lines until EOF (or "bad" e.g. error reading redirected file)...
    while (getline(cin, name))
        ...use name...
}
else
{
    std::cerr << "ERROR reading number\n";
    exit(EXIT_FAILURE);
}

在上面的代码中,这个位...

    char c;
    while (cin.get(c) && c != '\n')
        if (!std::isspace(c))
        {
            std::cerr << "ERROR unexpected character '" << c << "' found\n";
            exit(EXIT_FAILURE);
        }

...在数字仅包含空格后检查输入行的其余部分。

为什么不直接使用忽略?

这非常冗长,因此在流之后使用是一种经常推荐的替代方法,可以丢弃内容到下一个换行符,但它可能会丢弃非空格内容,并且这样做会忽略文件中的损坏数据。您可能关心也可能不在乎,具体取决于文件的内容是否受信任,避免处理损坏数据的重要性等。ignore>> x

那么什么时候会使用 clear 和 ignore?

因此,(和)不是必需的,但对于删除错误状态很有用。例如,如果要为用户提供许多输入有效号码的机会。std::cin.clear()std::cin.ignore()

int x;
while (std::cout << "Enter a number: " &&
       !(std::cin >> x))
{
    if (std::cin.eof())
    {
        std::cerr << "ERROR unexpected EOF\n";
        exit(EXIT_FAILURE);
    }

    std::cin.clear();  // clear bad/fail/eof flags

    // have to ignore non-numeric character that caused cin >> x to
    // fail or there's no chance of it working next time; for "cin" it's
    // common to remove the entire suspect line and re-prompt the user for
    // input.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max());
}

使用 skipws 或类似产品难道不能更简单吗?

另一个简单但半生不熟的替代方法是在阅读行之前跳过任意数量的空格......ignorestd::skipws

if (std::cin >> number >> std::skipws)
{
    while (getline(std::cin, name))
        ...

...但是,如果它得到像“1E6”这样的输入(例如,一些科学家试图输入 1,000,000,但 C++ 只支持浮点数的表示法)不会接受这一点,你最终会设置为 ,并读取为 的第一个值。另外,如果有一个有效数字,后跟一个或多个空行,则这些行将被静默忽略。number1E6name

评论

3赞 Lightness Races in Orbit 9/11/2015
呃,这并不能回答问题或解决问题。-1
0赞 Tony Delroy 9/13/2015
@LightnessRacesinOrbit:它如何“不解决问题”?第一次调用使用数字后面的换行符并循环,直到找到非空行...对我来说似乎是固定的。OP的问题没有问“为什么?”问题发生了,但确实评论说他不确定为什么可能是必要的 - 其他人在2天前编辑了5年前的问题以添加它,大大改变了要点。getlineclear
0赞 Lightness Races in Orbit 9/13/2015
现在你已经编辑了[别人的]答案,那就更好了
0赞 Tony Delroy 9/14/2015
@LightnessRacesinOrbit:我从2011年开始编辑了我对jonsca回答的评论要点 - 没什么新鲜事。
20赞 jonsca 4/21/2011 #2

另一种方法是将一个

cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); 

之后完全刷新输入缓冲区(拒绝所有多余的字符,直到找到换行符)。你需要得到方法。cin>>number;#include <limits>max()

评论

0赞 Tony Delroy 4/21/2011
@jonsca:“拒绝所有多余的字符”在大多数生产系统中都是可疑的......吃空格很好,但丢弃未知数据很容易无声无息地导致错误的结果。
0赞 jonsca 4/21/2011
@cnicutar因实施而异
0赞 jonsca 4/21/2011
@Tony 如果你有一个循环来接收位于 cin 语句后面的字符,而不是 getline,怎么样?当然,额外的角色会给它带来麻烦。你说的是安全漏洞吗?
1赞 Tony Delroy 4/21/2011
“循环吸收字符”......如果你的意思是......对我来说看起来不错。综上所述,我想更多的是用户误解了输入格式要求,或者某些脚本生成了输入中断,但损坏的输入似乎被成功处理,因为它被忽略了 - 他们最终可能会信任损坏的结果。如果输入不符合规范,最好让程序生成错误。while (isspace(cin.peek())) cin.ignore()
0赞 jonsca 4/21/2011
@Tony 啊,好吧,我误解了你想说的话。关于验证的好点。
10赞 Martin York 4/21/2011 #3

尝试:

int number;

cin >> number;

char firstCharacterOfNames;
cin >> firstCharacterOfNames;  // This will discard all leading white space.
                               // including new-line if there happen to be any.

cin.unget();                   // Put back the first character of the name.

std::string  names;
std::getline(cin, names);      // Read the names;

或者。如果您知道该数字和名称将始终位于不同的行上。

cin >> number;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
std::getline(cin, names);
26赞 Cristiano Miranda 7/9/2012 #4
cout << "Enter the number: ";
int number;
cin >> number;

cin.ignore(256, '\n'); // remaining input characters up to the next newline character
                       // are ignored

cout << "Enter names: ";
string names;
getline(cin, names);
-1赞 max 2/3/2015 #5
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Enter the number: ";
    int number;
    cin >> number;
    cout << "Enter names: ";
    string names;

    // USE peek() TO SOLVE IT! ;)
    if (cin.peek() == '\n') {
        cin.ignore(1 /*numeric_limits<streamsize>::max()*/, '\n');
    }

    getline(cin, names);

    return 0;
}

只需向前看一下,看看 a 是否仍然留在 的内部缓冲区中。如果是这样:忽略它(基本上跳过它)cin.peek()'\n'cin

评论

0赞 Lightness Races in Orbit 9/11/2015
如果是呢?这是一个非常脆弱的解决方案,我会在代码审查中拒绝它。'\r'
8赞 Rajeev Atmakuri 4/17/2015 #6

在使用 getline 之前,可以使用 std::ws 提取输入缓冲区中的任何空格字符。std::ws 的标头是 sstream。

cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
cin>>ws;
getline(cin, names);
0赞 reeeeeeeeeeeeeeeeeeeeeeeeeegie 8/20/2016 #7

您可以在 cpppreference 中找到您想要的答案。

当在空格分隔的输入之后立即使用时,例如,getline 使用 operator>> 在输入流上留下的结束行字符,并立即返回。一种常见的解决方案是在切换到面向行的输入之前,忽略输入行上的所有剩余字符。int n; std::cin >> n;cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

1赞 pz64_ 9/12/2016 #8

或者,您可以刷新输入缓冲区以读取字符串

法拉盛(stdin)

它在标头 stdio.h 中定义。

此代码有效。.

cout << "Enter the number: ";
int number;
cin >> number;

cout << "Enter names: ";
string names;
fflush(stdin);  //FLUSHING STDIN
getline(cin, names);
0赞 lopezdp 9/17/2016 #9

您希望在 CIN 语句后使用 cin.ignore(),因为您想在使用 CIN 获取 int 变量后忽略缓冲区中剩余的“\n”。

我有一个类似的程序,我用过类似的问题:

#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

int main() {
    int i = 4;
    double d = 4.0;
    string s = "HackerRank ";

    // Declare second integer, double, and String variables.
    int n;
    double d2;
    string str;

    // Read and save an integer, double, and String to your variables.
    cin >> n;
    cin >> d2;

    cin.ignore();

    getline(cin, str);

    // Print the sum of both integer variables on a new line.
    cout << i + n << endl;


    // Print the sum of the double variables on a new line.
    cout << d + d2 << endl;

    // Concatenate and print the String variables on a new line
    cout << s << str << endl;

    // The 's' variable above should be printed first.

    return 0;
}
1赞 1911 Soldier 10/28/2016 #10

我刚刚用过

getline(cin >> ws,lard.i_npute);

与标准

#include <iostream>

标头,在我遇到回车问题并且 ws 操纵器工作的情况下。我可能会开始将循环函数嵌入为类,并至少使用构造函数和析构函数调用。

0赞 minhle_r7 11/9/2017 #11

从概念上讲,我认为你希望每个答案都是整齐的一行。那你为什么不试试这个呢?

cout << "Enter the number: ";
string line;
getline(cin, line);
int number = std::stoi(line);

cout << "Enter names: ";
string names;
getline(cin, names);

代码正确使用第一个换行符,如果该行正确,则给出数字,如果行不正确,则引发异常。全部免费!

1赞 Aditya Gullapalli 3/20/2018 #12
cout << "Enter the number: ";
int number;
cin >> number;

cout << "Enter names: ";
string names;
getline(cin, names);//works on the \n left behind
getline(cin, names);//continues and rewrites names

它非常不言自明,在使用的流中留下了一个\n,它在第一次使用时被分配给名称。现在,重用 getline 会写入正确的值。cin >> number

6赞 Vishal101022 12/10/2018 #13

在 getline() 函数之前使用 cin 时尝试 cin.ignore()

void inputstu(){
    cout << "Enter roll Number:";
    cin >> roll_no;
    cin.ignore(); //ignore the withspace and enter key
    cout << "Enter name:";
    getline(cin, stu_name);
}