#### Archived

This topic is now archived and is closed to further replies.

# (C++)How to execute other statements while waiting for input?

This topic is 5310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everyone, Out of all the stuff I have learned in C++, most of which pretains to programming mainly in console applications, I have never been taught how make a program that can do more then one task at a given time. I'm well aware that in reality the computer is actually switching between task and doing them really quickly thus giving us the illusion that it's doing many things at once. But one of the "problem" that have always stuck in my mind was how can I get my program to do stuff while waiting for input. Note I've only experience in programming console applications in the dos environment and not in windows so what I'm asking here probably won't be an issue when making windows programs. Here's a simple example, say the program displays a menu and waits for the user to pick a choice. At the same time the program maintains a counter and displays it on the screen. After this counter reaches 0 and if the user hasn't made a selection yet then the default selection is made. So the problem in programming this? If I use the cin statement to get input from user the program stops at that line indefinitely till some kind of input is gotten and nothing else is executed while it's waiting. Same thing if I use scanf, or getch() or whatever other input function. So how can this problem be solved? Thanks [edited by - Greatwolf on June 2, 2003 9:54:52 PM] [edited by - Greatwolf on June 2, 2003 9:55:25 PM] [edited by - Greatwolf on June 2, 2003 9:56:16 PM]

##### Share on other sites
In Windows, it''s indeed easy (there''s a function called CreateThread for this purpose). In DOS, it''s a lot more difficult. Do do true multitasking, you''d have to write your own task switcher (which you probably don''t want to do). You can fake it, though, by making use of the timer interrupt. This interrupt is executed 18.2 times per second by default, but you can change this frequency. Whenever your interrupt handler is called, you can execute other code (although it shouldn''t use interrupts unless you really know what you''re doing -- this can be more difficult than it seems). The task needs to be broken down into small chunks, though, which take only a fraction of a second to complete and can be continued at the next interrupt tick.

##### Share on other sites
quote:

In Windows, it''s indeed easy (there''s a function called CreateThread for this purpose).

!? you wouldnt have to use threads to accomplish that! all he/she wants to do is display a counter while the user is supposed to input something. in windows you get a message when a key is pressed and you dont call a function. i dont think that is possible using cin, sscanf, or getch. all of those functions only return when they get input so you wouldnt be able to do anything until they return.

doh, nuts. Mmmm... donuts
My website

##### Share on other sites
Indeed... what you would do would be to have a main loop, that would check for a nonzero value from kbhit() (conio.h i believe).. if that is true, then check getch() to get the key value that was pressed. For windows, i think all that would be needed would be Getasnyckeystate() (i''m pretty sure that''s the function).. either that or catch the WM_CHAR or WM_KEYDOWN message passed to the window

##### Share on other sites
quote:
Original post by falkone
Indeed... what you would do would be to have a main loop, that would check for a nonzero value from kbhit() (conio.h i believe).. if that is true, then check getch() to get the key value that was pressed. For windows, i think all that would be needed would be Getasnyckeystate() (i''m pretty sure that''s the function).. either that or catch the WM_CHAR or WM_KEYDOWN message passed to the window

what does kbhit() do exactly and how does it work in conjunction with getch()? Can you post a simple code snippet to show what you mean?

Thanks

##### Share on other sites
Sure, kbhit takes a look in the keyboard buffer, and returns a nonzero value if there is something there.. getc() will return the value that kbhit saw..

#include <conio.h>#include <iostream.h>#include <stdlib.h>char keypress;bool done;void main(){done = false;cout << "Press ''Y'' to exit the loop\n";while (!done){	if (kbhit())	{		keypress = getch();		cout << keypress << endl;	}	if (toupper(keypress) == ''Y'')		done = true;} }

##### Share on other sites
quote:

!? you wouldnt have to use threads to accomplish that! all he/she wants to do is display a counter while the user is supposed to input something.

No, as per the original question, that's not all he wants to do, that's an example. The actual problem he posed was how to "do more then (sic) one task at a given time".

For displaying a simple counter, threads are overkill, obviously and it's not the way to deal with blocking input, but it is the correct answer to the original problem.

[edited by - Kippesoep on June 3, 2003 1:56:46 AM]

• ### Forum Statistics

• Total Topics
628714
• Total Posts
2984350

• 23
• 11
• 10
• 13
• 14