Thread: Escaping an infinite loop

  1. #1
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127

    Escaping an infinite loop

    I am trying to work on a note program that I have been working on for a long time and wanted to add a new feature. Up until now the user has only been able to enter a 1 line note. I wanted to make it so that they can use as many lines as they want. This is how I have it so far.
    Code:
    while(1){
             fgets(text,1000,stdin);
             fprintf(fp,text);
        }
    That works but then the user needs to entirely exit the program to stop it. I wanted to add an if statement so that they could enter a character to terminate the note. Like this...
    Code:
    while(1){
                fgets(text,1000,stdin); 
                if(text=='~'){
                              break;
               }
               fprintf(fp,text);
        }
    This compiles with no warnings but then when I run it just keeps going and does not exit the infinite loop when they enter a ~. Why is this?
    ~Sven

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Did you mean this?
    Code:
    if(*text=='~'){
    [edit]Maybe this?
    Code:
       while ( fgets(text, sizeof text, stdin) )
       {
          if ( *text == '~' )
          {
             break;
          }
          /* ... */
       }
    [edit=2]Oy, but a pokey edit. Sly:
    Last edited by Dave_Sinkula; 03-03-2006 at 05:19 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I'd imagine that text is some sort of character array. In which case you'd have to dereference the name in order to get the value (assuming it's the only thing in the array) or you'd have to just check (text[strlen(text)-1] == '~').

    EDIT: *petting his cat* "I'll get you NEXT time, Sinkula. NEXT TIME!"
    Sent from my iPadŽ

  4. #4
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Thanks guys I didn't know that I needed to dereference in this case. Thanks again.
    ~Sven

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I also think '~' is kind of a weak way to exit too. There is no confimation that someone wouldn't actually want that at the beginning of a note for some reason. You should make it more solid.
    Code:
    while(fgets(text,1000,stdin) && strcmp(text, "END NOTE\n") != 0){
         fprintf(fp,text);
    }
    Sent from my iPadŽ

  6. #6
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    I was coming up with a symbol that no one ever uses. I couldn't think of anything better. As I start my next line with a ~.
    ~Sven

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's used in Diku derived MUDs to denote the end of a string in area / player / etc files.


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

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    How about:
    Code:
    while ( fgets(text, 1000, stdin) )
    {
        if (text[0] == '\n' )
        {
            break;
        }
          /* ... */
    }
    A blank line indicates no more note.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by WaltP
    How about:
    Code:
    while ( fgets(text, 1000, stdin) )
    {
        if (text[0] == '\n' )
        {
            break;
        }
          /* ... */
    }
    A blank line indicates no more note.
    "...oops I hit enter twice... woe is me."

    Not to mention the fact that I (like some others) like a blank line between paragraphs.

    *On a side note: I've never tried any DikuMUD based MUDs, if I recall, I've only played CircleMUD based.
    Sent from my iPadŽ

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by SlyMaelstrom
    "...oops I hit enter twice... woe is me."

    Not to mention the fact that I (like some others) like a blank line between paragraphs.

    *On a side note: I've never tried any DikuMUD based MUDs, if I recall, I've only played CircleMUD based.
    "Oops I entered ~100... woe is me."

    Sometimes a ~ is meant to mean approximately.

    You gotta define the limitations of your input. I just gave an alternative, Didn't mean to start a "war between the delimiters"!

    Does this count as MUD slinging?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by WaltP
    "Oops I entered ~100... woe is me."
    Yeah, I guess you're right, that would be a problem wouldn't it. I'm sorry for suggesting your idea had flaws. I'm starting to agree with the other guy who made your point earlier.
    Quote Originally Posted by SlyMaelstrom
    I also think '~' is kind of a weak way to exit too.
    Oh wait... that was me.
    Sent from my iPadŽ

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SlyMaelstrom
    *On a side note: I've never tried any DikuMUD based MUDs, if I recall, I've only played CircleMUD based.
    Click 'derived' above.


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

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Oh wow... did... ANYTHING not derive from DikuMUD? In any event, it's been a while and don't recall the usage of ~.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. 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