Sign in to follow this  

c++: win32 console program that supports dual input

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I am trying to write my first program that accepts both input from the user and the computer at the same time. I am trying to write a program that is a side scrolling console based program that has the user trying to dodge moving objects. To do this the user must be able to move whenever they want (thus eliminating sleep(); or wait(); functions). I planned to use getch(); for this. I also want the computer update the board (since it is side scrolling) every X milliseconds. I am able to accomplish this by using the clock(); function. The problem is is that getch(); waits for input. If the user stays still I still want the board to update. So I installed a if (kbhit == true) statement but it still waits for input. Could someone help me out writing a statement that allows the user to move whenever but also allows the computer to update the board every x milliseconds? Thanks! This is my current code: #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <time.h> int main() { int keyhit(0), escape_key(0), time1(0), time2(0), timef(0), hs(0); while (escape_key != 1) { if (hs == 0) { time1 = clock(); hs = 1; } time2 = clock(); timef = time2 - time1; if (timef > 650) { cout << "TIME \n"; hs = 0; } if( kbhit() == true ) { keyhit = getch(); cout << keyhit << "\n"; if (keyhit == 27) { escape_key = 1; } keyhit = 0; } } return(0); }

Share this post


Link to post
Share on other sites
Your code works for me.. I compiled it using the free VC++ 2003 toolkit. It does what you want it to do (i.e. keeps printing TIME and at the same time polls for a keypress).

cheers
sam.

Share this post


Link to post
Share on other sites
Thats what it does for me.

Its not suppose to do that though.

Its suppose to print the ascii value of the keyboard when pressed and every 650 milliseconds print "TIME". Instead, it prints all the "TIME"s after a key is pressed. If no key is pressed, no time is shown.

Share this post


Link to post
Share on other sites
Still didn't work for me.

Here is my new code, which still does the same thing except prints it all to the screen before escape key is pressed.



#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

int main() {

int keyhit(0), escape_key(0), time1(0), time2(0), timef(0), hs(0);

while (escape_key != 1) {

if (hs == 0) {
time1 = clock();

hs = 1;
}

time2 = clock();

timef = time2 - time1;


if (timef > 650) {
cout << "TIME \n";
hs = 0;
}

if (kbhit() != 0) {
keyhit = getch();
}

if (keyhit != 0) {
cout << keyhit << endl;
if (keyhit == 27)
escape_key = 1;
keyhit = 0;
}
}
return(0);
}

Share this post


Link to post
Share on other sites
It's an efficiency thing. It is more efficient to buffer up a lot of data and write it to screen in one go than it is to write out things little by little. So cout only writes to screen when it has received a certain amount of data (most likely this amount is implementation, and OS, dependant).

To get cout to output straight away, you need to flush it. This can be done in a number of ways:

1. pass 'endl' to cout. endl is called an IO manipulator and has the effect of flushing cout and writing an end line.
2. call cout.flush() which flushes cout without outputing an end line.
3. ask for user input. endl will flush when you ask for user input, as you have seen.

Another option is to use cerr instead of cout. cerr is unbuffered so output will be displayed directly.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this