Thread: quit the loop?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    9

    quit the loop?

    How can I stop the loop when user enter ESC? When user enters ESC, the program prompts user for entering Y/N, if Y, stops the loop, otherwise, the loop continue.

    Code:
    int i = 0;
    do
    {
       i++;
    // many sub-programs follow, e.g.
       fun1();
       fun2(); 
       fun3();
       .........
       fun10();
    }
    while(i < 2000); // for example 2000
    Because these many sub-programs involves a lot of computations and cannot stop or wait for user's input.
    Useless the user press ESC, the loop should be continued to run.
    I have no ideas of it.
    Can anyone answer me?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here is some info that will help you.

    gg

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    it would also work doing this

    Code:
    #include <stdio.h>
    #include <conio.h>
    #define ESC 0x1b
    
    int main ( )
    {
     int i=5;
     do{
          printf("Press a key.\n");
          if(getch()==ESC){
             printf("You pressed Escape.\n");
             }
          else      
              printf("You didnt press Escape.\n");
             
           i--;       
           }
           while(i>0);
    return 0;
    }
    Pretty much same way

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    9
    I've try getch() that the program wait for user's input and cannot jump to
    puts("After get ESC"); if no one hit keyboard.
    How can I write an program that do not wait for user input?
    The program only respond when user hit ESC, otherwise, the program still running.

    Code:
    int i = 0;
    do
    {
       i++;
       puts("Before get ESC");
       if(getch()==ESC){
          printf("You pressed Escape.\n");
       }
       else      
          printf("You didnt press Escape.\n");
       puts("After get ESC");
    
    // many sub-programs follow, e.g.
       fun1();
       fun2(); 
       fun3();
       .........
       fun10();
    }
    while(i < 2000); // for example 2000

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    if i understand you then u can do just fine with kbhit...

    do{
    blabla();
    }
    while(!kbhit());

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yeah, what Fozzy said: use kbhit().

    gg

  7. #7
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    You could use _kbhit() to check for a key press first. [edit on] As stated above [edit off]
    Code:
    #include <stdio.h>
    #include <conio.h>
    #define ESC 0x1b
     
    int main()
    {
        int i = 0, stop=0;
        while( i < 200000 && !stop )
        {
            if(_kbhit() && getch()==ESC)
            {
                stop=1;
                continue;
            }
    
            printf("PRESS ESC TO STOP %d\n", i);
    //      many sub-programs follow, e.g.
    //      fun1();
    //      fun2(); 
    //      fun3();
    //      .........
    //      fun10();
          i++;
        }
        return 0;
    }
    Last edited by Scarlet7; 03-29-2003 at 10:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  4. Loop won't quit!
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 07-13-2002, 09:22 PM
  5. 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