Thread: How to break out an infinite loop and then enter another infinite loop?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    How to break out an infinite loop and then enter another infinite loop?

    I'm trying to create a status menu system with ncurses where the user is presented with three choices along a top row:

    H S T

    Each option selection prints various status over and over continuously. For ex. If the user chooses H, High status info is constantly being displayed.

    If the user then pushes S, the H loop is broken and the S loop enters outputting system status info. And vice versa.

    How can I go about this? I was thinking of using GOTO's but I know that's a no-no.

    For ex. If I'm in H's while loop, the user pushes S, then that would break H's loop, the user would have to push S again, to enter S's loop.

    Ahhhh so confused. (Actually there are more than 3 selections, but for simplicity, I only show 3. This is quickly going to become messy.)

    Some pseudo-code below:

    Thanks.


    Code:
    //e or E to exit program
    while ((ch = getch()) != 'e' || ch != 'E' )
        {
            if (ch == 'H' || ch == 'h' ) {
                
                //infinite loop to constantly display H's status
                while (1){
                    
                    getHighStatus function
                    print high status
    
                    if S is pressed, break, somehow enter S loop
                    if T is pressed, break, somehow enter T loop
    
                }
            }
            else if (ch == 'S' || ch == 's' ) {
                while (1){
                    getSystemStatus function
                    print system status
                    if H pressed, break, somehow enter H loop
                    if T prssed, break, somehow enter T loop
                }
            }
            else if (ch == 'T' || ch == 't' ) {
                  while (1){
                    getTempStatus function
                    print temp status
                    if H pressed, break, somehow enter H loop
                    if S pressed, break, somehow enter S loop
                }
            }
            else
            {    
                print default message
            }
    
        }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    break is indeed what you want. It will break you out of your current loop (or case statement for that matter).


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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A bit of manual page
    getch(3NCURSES) getch(3NCURSES)

    NAME
    getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or push back) characters from curses terminal keyboard

    SYNOPSIS
    #include <curses.h>

    int getch(void);
    int wgetch(WINDOW *win);
    int mvgetch(int y, int x);
    int mvwgetch(WINDOW *win, int y, int x);
    int ungetch(int ch);
    int has_key(int ch);

    DESCRIPTION
    The getch, wgetch, mvgetch and mvwgetch, routines read a character from the window. In no-delay mode, if no input is waiting, the value ERR is re‐
    turned.
    In delay mode, the program waits until the system passes text through to the program. Depending on the setting of cbreak, this is after one
    character (cbreak mode), or after the first newline (nocbreak mode). In half-delay mode, the program waits until a character is typed or the speci‐
    fied timeout has been reached.
    So you put ncurses into no-delay mode, and do this.
    Code:
    while ( 1 ) {
      ch = getch();
    
      if ( ch == ERR ) ch = lastch; // no key, do same as last time
      ch = tolower(ch);
      lastch = ch;
    
      if ( ch == 'e' ) break;
      switch ( ch ) {
        case 'h':
          getHighStatus function
          print high status
          break;
        case 's':
          getSystemStatus function
          print system status
          break;
        case 't'
          getTempStatus function
          print temp status
          break;
        // and so on
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Couldn't you just do this:

    Code:
       //e or E to exit program
       while ((ch = getch()) != 'e' || ch != 'E' )
        {
            if (ch == 'H' || ch == 'h' ) {
                    getHighStatus function
                    print high status
                }
    
            else if (ch == 'S' || ch == 's' ) {
                    getSystemStatus function
                    print system status
                 }
    
            else if (ch == 'T' || ch == 't' ) {
                    getTempStatus function
                    print temp status
                }
    
            else
                print default message
        }
    edited: oh, yeah, you have to look for change in ch
    Last edited by megafiddle; 10-14-2011 at 11:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is for(;;) an infinite loop
    By cyberfish in forum C++ Programming
    Replies: 21
    Last Post: 04-26-2010, 04:20 PM
  2. infinite loop
    By hlam in forum C Programming
    Replies: 4
    Last Post: 10-29-2008, 01:16 AM
  3. switch-case,Infinite while-loop ,break
    By babu198649 in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2008, 08:12 AM
  4. 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
  5. Infinite Loop HELP!!
    By victorti83plus in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2002, 08:10 PM