Thread: Looping error

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Looping error

    My gigantic while loop works great except that it repeats the very last line in the output. Any ideas on where the error might be would be appreciated.

    Here is a piece of the loop:

    Code:
    while (!fin.eof()) {
        fin >> ch1;
          if ((ch1 == 'M') || (ch1 == 'F')) {
      fin >> ch2;
      if (ch1 == 'M') {
       males++;
      } else {
       females++;
      if (ch2 == 'A') {
       fin >> age;
       ageAccum = age + ageAccum;
       count++;
       fin >> dummych;
       fin >> weight;
       weightAccum = weight + weightAccum;
       count2++;
       newlog(ch1, age, weight, fout);
      } else {
       fin >> weight;
       weightAccum = weight + weightAccum;
       count2++;
       fin >> dummych;
       fin >> age;
                   ageAccum = age + ageAccum;
       count++;
       newlog(ch1, age, weight, fout);
      }
      }

    If you need the whole thing, let me know.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it repeats the very last line in the output
    >while (!fin.eof()) {
    eof doesn't return true until after you've tried and failed to read from the stream. This means that the last record will be processed twice because the input failed and the last record is supposedly still in memory.

    This is also a FAQ entry, if you want to look it up. The entry is concerning feof, but the problem is the same.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    I've tried putting the first input before the while loop, but that didn't work. Any actual coding ideas?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I guess it is to hard to find what you where told to read. Let's do it, in a bold font link
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Last edited by prog-bman; 04-03-2005 at 10:10 PM.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM