I'm trying to use cin.ignore after I use the extractor operator (>>) to read and discard the end of line character before I use a getline function to collect more data from an input file. Somehow cin.ignore() is pausing my code after it executes and I'm not sure why because I'm getting no error messages in the console. Why is this happening and how can I properly use cin.ignore() to discard the end of line character so that I can continue collecting data properly?
Sorry. test is just a struct. my actual code is a lot longer I just shortened it here to show only relevant parts. I edited my post to show that test is a struct. Does it make more sense now? The main trouble I'm having is finding out how to use cin.ignore to discard the end of line character after I use the extractor operator. I'm not getting any errors but it looks like my code just stops after cin.ignore is executed. To test this I put cout << "something" before and after the cin.ignore function is executed and only the cout message before cin.ignore is showing.
I was particularly confused by this line, but apparently you've removed it:
dataInput >> test
And your input file is not very helpful, being gibberish.
For your input statements, you have 3 uses of >>, which read space-separated "words", so you would read in "han", "ds", and "mr", then you would skip the rest of that line (the " ds gt" part), then you read two whole lines, then two more space-separated words. It just seems strange.
The problem is that std::cin.ignore(...) will clear the input buffer of what was entered by the keyboard, but you are reading from a file.
What you need is dataInput.ignore(...); to clear the file buffer.
Of course clearing the file buffer of the "\n" will not totally solve your problem. It appears that what you are trying to read does not match the input file.
Sorry I just edited my input file I think it will make more sense now. I tried your suggestion but that just breaks my code by collecting the data in an incorrect order, adding some weird values in, and cin.ignore is still pausing my code. This is pretty much all of my code by the way, it's just a struct and an input file. How might I not be checking for eof correctly? Is the way I'm using cin.ignore somehow messing up the eof?
An alternative take is to overload the stream extraction (operator >>) to obtain a record from the file. Also, you don't need .ignore() in this case. Just use std::ws on the stream extraction to remove white space. Consider: