Thread: User input while loop is running

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    26

    User input while loop is running

    Lets say I had a loop running, but anytime during the loop I wanted the user to be able to press a key and make something else happen.

    Any suggestions on how to do this? I am completely lost.

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Plenty of ways to do this.....

    you can do this (pseudocode), while the A key isnt pressed the loop runs, otherwise it quits (you can use the getch() function to do this)

    Code:
    while(key 'a' is not pressed)
    {
    blahblah loopbody
    }
    orrrr, this way the loop will still continue, but do something different

    Code:
    bool a;
    
    while(some other while statement)
    {
    if a=true
    {
    blah loop body
    }
    else
    {
    blah other loop body
    }
    (as many ifs and elses as you need)
    }
    you can detect keystrokes (characters and numbers) with the getch() function from conio.h.. you should have conio already in your library, so just #include <conio.h>

    you can detect all keys through windows, (assuming you are using windows) with windows.h.... there are key definitions that look something like this "VK_ENTER" or "VK_BACKSPACE"

    you could do something like this
    Code:
    				if (keys[VK_ESCAPE])				// Was ESC Pressed?
    				{
    					Quit = true;
    
    				}
    Last edited by Shamino; 11-04-2005 at 02:25 AM.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    getch() will halt execution of the loop if no key has been hit, you might want to look at kbhit() function to use in conjuction with getch(), depending on your os/compiler.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    Thanks for your help, everyone. I'll try out these strategies soon.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    There is a very simple way to use this:
    Code:
    char c='1';
    while (c!='q') // or anything else u want to use to exit loop
    {
        if (kbhit()) c=getch();  // this will require the conio.h library
    }
    // this loop will run non stop and enter every key u enter to c, note you can also use integers for this. also to make your program run better make sure you don't write stuff that doesn't suppose to change in the main while and only write them in the "if" of the kbhit, it makes a hugh difference when using graphics.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    can anyone give an example like the full code coz i dont know how to implement this in my code! PLZ.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    Code:
    while(!kbhit)
    {
        loop();
    }

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    here is a very simple program that writes a charter on the screen non stop untill u enter 'q'
    Code:
    #include <iostream.h>
    #include <conio.h>
    void main()
    {
    	char c='1';
    	while (c!='q')
    	{
    		if (kbhit()) c=getch();
    		cout << c;
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  5. Format User Input When using strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 03:59 AM