0

I'm reading lines from std::cin with a while loop that ends when EOF is introduced. After exiting the loop, I call std::cin.clear() to put the stream back to normal and read again, but it's not entering the second loop.

I've checked the eofbit and it's set to false. The failbit is set to false as well and the goodbit is set to true. The gcout after clearing is 0. Here's the code I'm running:

    while (getline(std::cin, person)) {
        // Do stuff until I enter ^D
    }
    std::cin.clear();

    // Adding std::cin >> person here doesn't work either

    while (getline(std::cin, person)) {
        // It never enters this loop
    }

What am I doing wrong?

Groctel
  • 130
  • 11
  • What platform? There is more than goes into user input than just the C++-layer `std::cin` - maybe your actual pipe closed too. We can't know without knowing what technologies you're using – Lightness Races in Orbit Dec 09 '19 at 11:49
  • I'm working on ArchLinux with all packages updated to date. I'm compiling with g++ and I don't know if that's the info you're referring to specifically :( – Groctel Dec 09 '19 at 11:53
  • How do you provide input to this program? Using what technologies? Using what commands? In what software? Tell us _everything_ that you're doing, from start to finish. – Lightness Races in Orbit Dec 09 '19 at 12:07
  • What do you mean by "read again"? Read the same data again? Read more input provided _after_ "entering ^D"? Be specific. – Lightness Races in Orbit Dec 09 '19 at 12:08
  • AFAIK, `^D` marks "end of transmission" and is literally the end of the transmission; no postscripts. – molbdnilo Dec 09 '19 at 12:11
  • I'm opening the program with my shell (zsh), running it with `./bin/test`, no args. The input is provided from the shell as text text entered by a human from the keyboard. When I say "read again" I mean read another input. I input the text for the first loop correctly and then I cannot input anything anymore. The program just ignores every `std::cin` sentence after that. – Groctel Dec 09 '19 at 12:13
  • 1
    EOF means end of file: There is no more data to read. The only way to read anything more is to change reading position `seekg` if input supports it. Human console doesn't have such functionality (should user type everything again?). For me this question suffers from [XY problem](http://xyproblem.info/). You have some simple issue and you trying to solve it in weird way and you ask to fix this weird way. – Marek R Dec 09 '19 at 12:26
  • I undersand the XY problem here, but I have to say this code was given to me written that way by my professor, so I (wrongly) figured that I was doing something wrong, not that it was a weird way to do this. I'm sorry :( – Groctel Dec 09 '19 at 12:30
  • *^D* is a signal to the linux terminal that you do not intend to enter more data, and I do not believe you can go around this without making your own terminal emulator. Nevertheless, you may find this question helpful: https://stackoverflow.com/q/51194821 – Al.G. Dec 09 '19 at 22:58

1 Answers1

1

The std::cin.clear() command clean the error flags in cin, so that further I/O operations will work correctly. The clear method don't reset the cin to initial value. The std::cin is a stream, so you can read something only one time.

My suggestion in this case for your purpose is to store the readed lines in a string or in an array of strings, or in the way you prefer and then reads it from the stored data.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35