-3

I am getting caught in an infinite loop, weirdly, while trying to take in my input. I've been able to use a loop like this in the past appropriately, but for some reason I'm getting caught here:

vector<string> boolean_forms;
    while (!cin.eof()) {
        cout << "HELP IM STUCK";
        string line;
        getline(cin,line);
        if (!cin.fail()) {
            boolean_forms.push_back(line);
        }
    }

I don't know, is there a better way to do this? I can't for the life of me figure out why this is causing issues. Is it possible that there's code elsewhere in my program that's messing things up? I can't imagine what it would be, as this is just reading from a file which I don't write to at all.

Update: After changing to doing input directly instead of first checking end of file first, I get a segmentation fault. The segfault isn't caused by anything outside this loop, as if I try pushing back the line directly instead of from the input, the program runs as it should and I get expected output.

This is... frustrating. I'm getting downvoted to hell while I've tried all of the solutions proposed and none of them have worked. If I read in before checking end-of-file, I get a segmentation fault. If I check eof first and then getline, I get an infinite loop. If I hard-code insert the line I'm trying to check into the vector, my code runs fine so I know it must have to do with reading from cin. I read in with ./a.out < input.txt which has worked for me numerous times until now.

theupandup
  • 305
  • 1
  • 10
  • How are you opening the file ? – Shravan40 Mar 26 '17 at 16:04
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Galik Mar 26 '17 at 16:05
  • My problem is different, Galik. They are doing "while (cin >> x)" and actually the solution proposed is essentially what I have... except that I'm reading in by line so the insertion operator can't be used in my case. – theupandup Mar 26 '17 at 16:10
  • I'm not opening the file, I'm directing the file as input. ./a.out < input.txt – theupandup Mar 26 '17 at 16:14
  • Please re-read Galik's reference post. You misread it. . – 2785528 Mar 26 '17 at 16:36
  • You're right, I apologize. I have tried getting the input right away with while(getline(cin, line)), but this causes a segfault. – theupandup Mar 26 '17 at 16:53
  • 1
    Possible duplicate of [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Mike Kinghan Mar 26 '17 at 17:24
  • It's not a duplicate: I've already explained that none of the solutions posed there work for me. If I check eof from the loop condition I get an infinite loop and if I read first I get a segfault. Hardcoding the input into the vector gets me through the program without segmentation fault – theupandup Mar 26 '17 at 17:27
  • @skiesareforflying When you follow the right approach you get a segfault - meaning you've done something else wrong. When you follow the wrong approach, you're on the wrong track - fighting to get that working is counter-productive. You need to go back to X and stop struggling with Y. _Post your right-approach segfaulting code, and this should get you on the right track._. – Disillusioned Mar 26 '17 at 23:14

2 Answers2

0

Checking EOF, read data and repeat is the wrong way to do. You should first read data, check if the read failed and then repeat.

vector<string> boolean_forms;
std::string line;
while(getline(cin, line))
    boolean_forms.push_back(line);
The Techel
  • 814
  • 8
  • 13
  • this causes a segfault. If I manually write out boolean_forms.push_back("my input would be here") without taking from input, the program runs properly without segmentation fault. – theupandup Mar 26 '17 at 16:49
  • That shouldn't happen, there's something going on in other parts of your program. – The Techel Mar 26 '17 at 17:18
  • Yeah I wonder that too... though I don't get a seg fault if I hard-code the input in, so I guess it must be something subtle that is messing with input? I'm compiling with c++11 if that makes any difference. – theupandup Mar 26 '17 at 17:24
  • 1
    The above code looks fine. You could provide a minimal example which crashes. – The Techel Mar 26 '17 at 17:25
  • I would have expected: `while(!getline(cin, line).eof()) ...` (the peculiarities that exist in programming) :) – Disillusioned Mar 27 '17 at 03:25
0
ifstream iFile("input.txt"); // You need to change the file location based on your setup.

Try this:

while (true) {
    std::string line;
    iFile >> line;
    if( iFile.eof() ) 
        break;
    // Do Something
}

For detailed description of isotream::eof please see this

Shravan40
  • 6,849
  • 5
  • 21
  • 43
  • the input is redirected, I'm not opening any file. Also I'm reading in a line including white space, so ">>" does not work. – theupandup Mar 26 '17 at 16:50