Thread: Stopping an Infinite Loop

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Stopping an Infinite Loop

    I have a program that will be serving as an interface for a test instrument. This program will run from the command line in Windows, and after taking information about the user (just the user's initials and the revision number), will get data from the instrument, display it, and, at the command of the user, save it and print it (to a printer).

    When the information is displayed, it must be updated (every second) until the user chooses to save and print. So, here is the structure of this part of my program (all the variables have been declared):

    Code:
    while (1)
    {
      i = 1;
      while(i == 1)
      {
        /* Get data from instrument. */
        /* Print data, along with message to hit enter to break the loop and save to file. */
        sleep(1);
        /* If the user hits Enter during this time, exit the loop by setting i to 0. */
      }
    
      /* Save to file, send to printer. */
    
      printf ("Press Enter to test again, or q to exit program: ");
      scanf ("%c", answer);
      if (answer == q) return 0;
    }
    Basically, I need a way to exit that nested while loop when the user presses Enter. I'm not sure how go about this, though, as scanf will pause the loop for a prompt. I need the loop to complete each second.

    Thanks for any insight.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    This is very platform dependant. You'll either need to use ncurses for a *nix environment or use conio for DOS environment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Look at the OP's name. linuxpyro. But Windows it is . . .
    This program will run from the command line in Windows
    Try kbhit() for Dev-C++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    A suggestion is make a switch case where one of the options is a break;. Play around with ANSI values for the enter key and you should be able to add it in there with no problems. There could be a better way of doing this but if it gets the job done who cares. According to my references the enter key is
    Code:
    code character Hex binary 
    13     ^m          0d   00001101
    You would have to format your input keep that in mind.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    You can change the ISR for the keyboard and know at all times which keys have been altered, there. You can find a tutorial to do that at www.inversereality.org, after doing everything correctly you'll have an array that tells you exactly which keys are pressed and which are not, at anygiven time.

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. Stopping a Loop
    By Catman188 in forum C Programming
    Replies: 2
    Last Post: 02-14-2008, 10:14 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