std::getline skipping input from std::cin after last occurrence of delimiter, but not with input from std::istringstream

HansKlokkenspel

I need to read some input that is delimited by a whitespace, the main construction I used for this is:

while(std::getline(std::cin, s, ' ')){
    std::cout << s << std::endl;
}

For the input: "this is some text"

The output for S will be: "this", "is", "some", thus skipping the last piece of input after the last whitespace.

I want to include the last piece of input in my program as well, so I went looking for a solution and found the following:

while (std::getline(std::cin, line)) {
    std::istringstream iss(line);

    while (std::getline(iss, s, ' ')) {
        std::cout << s << std::endl;
    }
}

For the input: "this is some text"

The output for S will be: "this", "is", "some", "text", which is exactly what I want.

My question is: why does reading from std::cin with a delimiter skip the input after the last occurrence of the delimiter, but reading from std::istringstream does not?

erip

My question is: why does reading from std::cin with a delimiter skip the input after the last occurrence of the delimiter, but reading from std::istringstream does not?

It doesn't.

In your first example:

while(std::getline(std::cin, s, ' ')){
    std::cout << s << std::endl;
}

You are specifically reading items from newline that are literally delimited by a single space. Because the line is (ostensibly) ended with a newline, it will never finish extracting from the input string as it is expecting either ' ' or an EOF.

In your second example:

while (std::getline(std::cin, line)) {
    std::istringstream iss(line);

    while (std::getline(iss, s, ' ')) {
        std::cout << s << std::endl;
    }
}

The std::getline in the first while will strip the newline from your example sentence. Then items are extracted according to some basic rules.

Here are the rules (from cppreference):

Extracts characters from input and appends them to str until one of the following occurs (checked in the order listed)
    a) end-of-file condition on input, in which case, getline sets eofbit.
    b) the next available input character is delim, as tested by Traits::eq(c, delim), in which case the delimiter character is extracted from input, but is not appended to str.
    c) str.max_size() characters have been stored, in which case getline sets failbit and returns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

std::cin infinite loop from invalid input

From Dev

Initializing "const std::string" from "std::istringstream"

From Dev

Limiting input size with std::setw in std::cin

From Dev

std::thread and input with std::cin in an opengl application

From Dev

std::cin sometimes hangs, sometimes repeats forever after invalid input

From Dev

Detecting end of input using std::getline

From Dev

std::getline stuck on end of piped input

From Dev

std::cin: empty the input buffer without blocking

From Dev

std::cin infinite loop for wrong input

From Dev

C++ std::cin producing extra input

From Dev

std::cin: empty the input buffer without blocking

From Dev

std::cin infinite loop for wrong input

From Dev

Stopping while-loop with std::cin input

From Dev

Ignoring/skipping tokens using std::cin

From Dev

Ignoring/skipping tokens using std::cin

From Dev

Where does newline come from when using std::cin after std::cout?

From Dev

skipping over my last cin input

From Dev

C++ Using std::getline in place of cin >>

From Dev

How to know if std::cin is from redirect?

From Dev

Get only the first value from std::cin

From Dev

std::cin doesn't throw an exception on bad input

From Dev

Why does std::cin's extractor operator wait for user input?

From Dev

Using istream (std cin): prevent "[input] is not recognized as ..." on Windows

From Dev

std::cin does not accept input, program closes immediately

From Dev

Is it safe for the input iterator and output iterator in std::transform to be from the same container?

From Dev

Is there any std::istream manipulator that consume predefined string from input?

From Dev

How to read from std.in if the length of the input is not define

From Dev

How to read from std.in if the length of the input is not define

From Dev

input validation with std::ifstream

Related Related

  1. 1

    std::cin infinite loop from invalid input

  2. 2

    Initializing "const std::string" from "std::istringstream"

  3. 3

    Limiting input size with std::setw in std::cin

  4. 4

    std::thread and input with std::cin in an opengl application

  5. 5

    std::cin sometimes hangs, sometimes repeats forever after invalid input

  6. 6

    Detecting end of input using std::getline

  7. 7

    std::getline stuck on end of piped input

  8. 8

    std::cin: empty the input buffer without blocking

  9. 9

    std::cin infinite loop for wrong input

  10. 10

    C++ std::cin producing extra input

  11. 11

    std::cin: empty the input buffer without blocking

  12. 12

    std::cin infinite loop for wrong input

  13. 13

    Stopping while-loop with std::cin input

  14. 14

    Ignoring/skipping tokens using std::cin

  15. 15

    Ignoring/skipping tokens using std::cin

  16. 16

    Where does newline come from when using std::cin after std::cout?

  17. 17

    skipping over my last cin input

  18. 18

    C++ Using std::getline in place of cin >>

  19. 19

    How to know if std::cin is from redirect?

  20. 20

    Get only the first value from std::cin

  21. 21

    std::cin doesn't throw an exception on bad input

  22. 22

    Why does std::cin's extractor operator wait for user input?

  23. 23

    Using istream (std cin): prevent "[input] is not recognized as ..." on Windows

  24. 24

    std::cin does not accept input, program closes immediately

  25. 25

    Is it safe for the input iterator and output iterator in std::transform to be from the same container?

  26. 26

    Is there any std::istream manipulator that consume predefined string from input?

  27. 27

    How to read from std.in if the length of the input is not define

  28. 28

    How to read from std.in if the length of the input is not define

  29. 29

    input validation with std::ifstream

HotTag

Archive