Thread: Infinite Loop Issue

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    16

    Infinite Loop Issue

    Code:
    while(sscanf(testSection,"%s",currentWord)==1){              
                 if(strcmp(currentWord,argv[1])==0){
                    fputc('1',output);
                } else {
                    fputc('0',output);
                      }
            }
    I'm trying to read in word by word from an array that contains a sentence, and I'm comparing each word to the word passed as an argument. For some reason however, this goes into an infinite loop because it keeps reading the first word over and over. Any one know why this is happening? Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Otto45 View Post
    it keeps reading the first word over and over. Any one know why this is happening? Thanks.
    Because you're passing the same string to sscanf over and over
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    16
    testSection contains a sentence with spaces in between the words. Should this bit of code read in each word individually and the next time sscanf gets called it would move onto the next word?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    sscanf() alwais starts from the beginning of the string, it doesn't save any state between calls.
    Kurt

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Use strtok().
    Kurt

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    16
    Alright, I was thinking about it wrong then, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. infinite loop
    By liukinhei in forum C Programming
    Replies: 3
    Last Post: 03-24-2008, 11:01 PM
  3. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  4. almost infinite loop
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 06:01 PM
  5. Infinite loop?
    By ER in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2001, 05:47 PM