Need help with getline() [duplicate]

冷暖自知 提交于 2019-11-25 23:37:55

问题


Is there a reason why if in my program I am asking the user for input, and I do:

int number;
string str;
int accountNumber;

cout << \"Enter number:\";
cin >> number;
cout << \"Enter name:\";
getline(cin, str);
cout << \"Enter account number:\";
cin >> accountNumber;

Why after inputting the first number, it outputs \"Enter Name\", followed immediately by \"Enter Account Number\" before I even get to input my \"str\" for the getline(cin, str) line? Thanks!


回答1:


The getline(cin, str); reads the newline that comes after the number read previously, and immediately returns with this "line". To avoid this you can skip whitespace with std::ws before reading the name:

cout << "Enter number:";
cin >> number;
cout << "Enter name:";
ws(cin);
getline(cin, str);
...

Note that this also skips leading whitespace after the newline, so str will not start with spaces, even if the user did input them. But in this case that's probably a feature, not a bug...




回答2:


Try

cout << "Enter name:";
cin.ignore();
getline(cin, str);



回答3:


It looks like you want line based reading. For this you probably want to use getline consistently and then parse each line if you need to parse a number from then read line. It makes the input reading more consistent.

This way you don't have to manually scan for the end of each line to guarantee that the next read operation starts on a fresh line.

It also makes adding error handling for repeating input requests simpler.

e.g.

#include <string>
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>

int parse_integer(const std::string& input)
{
    std::istringstream iss(input);
    int result;
    if (!(iss >> result))
    {
        // error - throw something?
    }
    return result;
}

int main()
{
    int number;
    std::string str;
    int accountNumber;

    std::string inputline;

    std::cout << "Enter number: ";

    if (!std::getline(std::cin, inputline))
    {
        // error - throw something?
    }

    number = parse_integer(inputline);

    std::cout << "Enter name:";

    if (!std::getline(std::cin, inputline))
    {
        // error - throw something?
    }

    str = inputline;

    std::cout << "Enter account number:";

    if (!std::getline(std::cin, inputline))
    {
        // error - throw something?
    }

    accountNumber = parse_integer(inputline);

    return 0;
}



回答4:


cin >> number // eat the numeric characters
getline(cin, str) // eat the remaining newline



回答5:


I think the problem is that cin >> passes on the newline character (\n). The getline() assumes the newline character is whitespace and passes it on. Someone posted a solution you can use.

You can use a dummy getline(cin, dummy); or the real thing cin.ignore(100,'\n');




回答6:


cin >> number

only grabs the numbers from the buffer, it leaves the "enter" in the buffer, which is then immediately grabbed up by the getline and interpreted as an empty string (or string with just the new line, i forget).




回答7:


Don't use getline(): it's a bad thing for memory allocation. Use fgets(). See fgets reference.



来源:https://stackoverflow.com/questions/1744665/need-help-with-getline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!