How to stop an infinite loop by means of any key? Please help.

This is a discussion on How to stop an infinite loop by means of any key? Please help. within the C Programming forums, part of the General Programming Boards category; Dear CProgramming.com administrator/s and members, A greeting of peace. I hope everyone is in good health upon reading this thread. ...

  1. #1
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Unhappy How to stop an infinite loop by means of any key? Please help.

    Dear CProgramming.com administrator/s and members,

    A greeting of peace.

    I hope everyone is in good health upon reading this thread. Well, I have this code to "implement" an infinite loop:

    Code:
    #include<stdio.h>
    int main(void){
         clrscr();
         do {
             printf(" C programming ");
         } while( getch() != 0 );
         
         return 0;
    
    }


    I want to stop this "possibly" infinite loop by means of pressing any key, but unfortunately, the string C programming will only keep on appearing if I keep on pressing any key, which is not I wanted. I want to stop the displaying of text if I press any key. On a tutorial, getch() function said:

    gets character from console, no echoing.


    I really don't know if getch() is the one right for the job. I hope someone could correct me what is the best thing to do.


    Thank you very much and God bless everyone.

    Warm regards,

    Mark Squall
    "Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You cannot stop an infinite loop on a key press, in standard C, because there is no function to detect keystrokes. C doesn't actually care about keyboards. Keyboards are hardware specific. That means you need hardware (OS/compiler) specific functions if you want to do so.

    You're looking for something like kbdhit, which again isn't a standard function.


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

  3. #3
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Talking Thank you Mr. Quzah.

    Dear Sir,


    Thank you for the information, I am really not totally familiar with majority of standard C functions, I really thought there's a function that can detect keystrokes. I just thought C has a similar function to those of VB6 TextBox procedure KeyPress(KeyAscii as Integer), hehehe.


    Thanks a lot and more power to everyone.



    Warm regards,


    MarkSquall
    "Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to stop an infinite cyle?
    By opsis in forum Linux Programming
    Replies: 4
    Last Post: 01-07-2007, 11:58 AM
  2. Stopping an Infinite Loop
    By linuxpyro in forum C Programming
    Replies: 4
    Last Post: 11-30-2006, 11:21 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 05:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21