Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple pause routine won't work.

Started by ryanc Oct 15, 2000 at 9:35 PM 3 replies 600+ views
Original Post
ryanc
ryanc
I'm a beginner C++ programmer and I am having a problem with some simple code. I use MSVC++ 6.0 Intro Ed. I am trying to create a simple routine function that will pause a DOS program and wait for a keypress. The code is at the bottom. When I run the program, the first pause will work. But when I press a key, it skips the rest of the "pauses". Please tell me what I'm doing wrong or what I can do to perform this task. --------------Source--------------- #include "rc_pause.h" #include stdio.h //there are brackets around these (gamedev thinks its a html tag) #include conio.h main() { printf("Welcome to a pause demo\n"); pause(); printf("you pressed a key\n"); pause(); printf("you pressed another\npress again to exit\n"); pause(); return 0; } -------"rc_pause.h" Source--------- #ifndef pause #define pause #include conio.h //Pause function int pause(); int pause() { int wait_for_key=0; while(wait_for_key==0) { wait_for_key=kbhit(); } } #endif Any tips would be greatly appreciated! RyanC --------------------------- Games are only as good as you make them... Edited by - ryanc on 10/15/00 9:51:35 PM
---------------------------<code>!ERR(0493): An error occured in cerebral cortex at location 0x00f230f8!: Buffer Overflow
A. Buza
A. Buza
while(!kbhit() {}

The above code should work (its basically a shorter version of what you are doing), but I think that occasionally there might be an extra char in the buffer which would cause it not to work...maybe caling fflush(stdin) before might help? I'm not to familiar with the inner workings of console/DOS I/O, so hopefully someone will correct me if I'm wrong.

Oh, by the way, if you use the 'source' instead of the 'code' (or other way around?) tags, you shouldn't have problems with missing brackets and such.

Edited by - A. Buza on October 15, 2000 12:23:08 AM
Cygon
Cygon
Simple:

kbhit() just checks if a keystroke is in the buffer, but doesn''t remove it.
There are two solutions:

    
int pause(void) {
while(!kbhit())
; // Do nothing


getch();
return;
}

int pause(void) {
getch();
return;
}


Besides that, it looks like your pause() function is contained in the header file. You should just place the prototype
// rc_pause.h
#ifndef rc_pause_h
#define rc_pause_h
void pause();
#endif rc_pause_h

in the header file and declare the function in a c source file, like
// rc_pause.c
#include "rc_pause.h"

void pause() {
...
}

Placing the function in the header won''t cause problems since you placed an #ifndef..#endif around the code, but normally, all function bodys of not-inlined functions should be placed in c source files.

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
ryanc
ryanc
Well, i tried both and the same problem still occurred. I figured it was something like the input wasn't being "flushed" or something like so. Thanks anyways.

-Cygon- by the way, I was just using the header file to see if it would work. I'm still learning ya know

Anyone else know of another way or what I'm doing wrong, please by all means help me.

RyanC
---------------------------
Games are only as good as you make them...

Edited by - ryanc on October 16, 2000 8:53:11 PM
---------------------------<code>!ERR(0493): An error occured in cerebral cortex at location 0x00f230f8!: Buffer Overflow

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.