1

I'm trying to write a program that takes inputData from two files for a season of some sport (i.e.: football) and writes an output listing rankings each week. In the input file with the scores for each game, every week is separated by a line of '-' characters. I have an if, else loop set up where the program peeks at the first character of each line. If it sees a character other than '-', it reads normally. However, when it reads '-', the program will begin the output cycle.

The thing is, being that this is peek, I need to figure out how to get to the next line without creating new input and not cause a crash. All I can think of is using inStream.find( !'-' ); or inStream.seekg( !'-' );. Are there any other options I can use?

Also, for reference, the code is listed here: https://coderpad.io/475356. Look for line 80 for the problem area. Just don't make any edits please.

Thank you for your time.

P.S.: If anyone can find any other crashes, though, feel free to mention it.

1 Answers1

3

How about just using ignore() to skip the line?

inStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Make sure you have:

#include <limits>

If you prefer not to have std::, just put using std::numeric_limits; at the top of your file, and then drop std:: from the expression above.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • I'm kinda annoyed that you need the std:: redirects all over, but I'll give it a try. – JbstormburstADV Apr 10 '14 at 00:31
  • @JbstormburstADV [`using namespace std` is not considered a good practice for several reasons](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Use instead `using std::numeric_limits;`. – David G Apr 10 '14 at 00:37
  • Wouldn't that also mean I'd have to be adding 'std::' pretty much everywhere? I've got too much code written up to really justify that, not to mention this is near beginner level. – JbstormburstADV Apr 10 '14 at 00:43
  • @JbstormburstADV, If you are just doing some kind of coding exercise, feel free to do `using namespace std`. His comment and my update are meant to discourage future people who see this question from doing this "bad practice". If you are just writing throwaway code and not planning to reuse this code sometime in the future for anything other than an exercise, this is not as big of a deal. – merlin2011 Apr 10 '14 at 00:46
  • Well, this should help, but stuck in a loop now, so we have to figure that out. Thanks for the advice, though. – JbstormburstADV Apr 10 '14 at 00:58