Thread: Can't get my do while loop to stop hanging

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Angry Can't get my do while loop to stop hanging

    Hello

    I am having trouble getting my ‘do while’ loop to work. In my program I need to scan the first 4digits of a line, capture and compare those digits with the number a user entered. The program then counts how many times those 4digits appeared in the document.

    My problem is I can’t seem to create a loop so that the program will scan and compare the numbers until the EOF, and I end up going into a loop that goes on forever. (I can get the code to scan and compare the first number. And if I copy that piece of code 30odd times it ‘will’ work, just not efficiently.

    I have used that particular loop when displaying the entire contents of the file, but can’t seem to get it to work properly in this program.

    I think the problem may be that "while ((c =getc(fp))!= 10);" keeps looping looking for line return and on the last line of file it might not be there. It might go directly to EOF which would hang the loop but im not too sure on how to fix it, or what the code is for fixing it, (im stil a beginner :-) )


    Can anyone help me?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > } while (fp !=EOF)

    This is your problem.
    This is your solution:

    while ( !feof( fp ) )

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    Thankyou for the code Quzah, but its still hanging, I haven't placed it in the wrong place have i?

    Edited portion of original file

    if (fp == NULL) printf("Unable to open file \n");
    else
    {
    do
    {
    fscanf(fp,"%d", &fourdigitno);
    if (inputno == fourdigitno)
    {
    totaln = totaln +1;
    }
    while(( c =getc(fp))!= 10);
    } while ( !feof( fp ) ) ;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if (fp == NULL)
        printf("Unable to open file \n"); 
    else 
    { 
        do 
        { 
            fscanf(fp,"%d", &fourdigitno); 
            if (inputno == fourdigitno) 
            { 
                totaln = totaln +1; 
            } 
    
            while(( c =getc(fp))!= 10); 
    
        } while ( !feof( fp ) ) ; 
    }
    One way to find out what loop is the problem, is to remove the inner loop. You shouldn't need this since you're using fscanf. One additional potential problem with your loop is that if no newline is in your file, you'll just merrily start reading off past the end of your file, since your read never checks for it. Furthermore, you could replace that loop with the following line:

    fscanf( fp, "%*[^\n]\n" );

    That should scan and ignore everything up through the newline character.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Talking

    To all, with help from a friend, The problem is fixed, an extra piece of code had to be added, so the the loop will also stop if EOF is reached.

    while (c !=EOF)
    {
    fscanf(fp,"%d", &fourdigitno);
    printf("Scanning File: %d \n", fourdigitno);
    if (inputno == fourdigitno)
    {
    totaln = totaln +1;
    }
    while ((c =getc(fp))!= 10 && c!= EOF);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. need help, fgets won't stop while loop
    By Enkid in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 07:15 AM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 05:16 AM