hey, i'm looking to write a function that will have a similar effect as system("pause"); and i can get it to where the program will pause until the enter key is pressed, but how would i have it pause until any key is pressed? is it doable? thanks
hey, i'm looking to write a function that will have a similar effect as system("pause"); and i can get it to where the program will pause until the enter key is pressed, but how would i have it pause until any key is pressed? is it doable? thanks
Registered Linux User #380033. Be counted: http://counter.li.org
Well if you're using an implementation that has conio.h (not standard, but fairly common) getch() does the trick quite nicely.
Sure, but it's not as easy without knowing your compiler and OS, and does it really matter what the user types to stop the program? I mean, what do you really gain by letting them hit any key as opposed to enter?is it doable?
Just because I don't care doesn't mean I don't understand.
Windows XP Pro SP2 | Dev-C++ 4.9.9.2
mm..i'm lazy. its easier to hit the space bar than the enter key. lol
and portability would be nice, but not absolutely necessary. lol i dual boot fedora core 4 x86_64
Last edited by willc0de4food; 08-31-2005 at 08:08 PM.
Registered Linux User #380033. Be counted: http://counter.li.org
getch also exists in the ncurses library under unix, but if you want to be truly portable, you can't "wait for a key" because the C standard doesn't require or understand the notion of a keyboard.
thats more portable than i was thinking :P
anything i'd want to use it for would have a keyboard.. lol
Registered Linux User #380033. Be counted: http://counter.li.org
you could always switch to c++ and use
cin.get();
mm...i have it working in C. i like C better than C++, i find it easier to get around.. esp. with file i/o. but here's the code i have:
Code:#include <stdio.h> #ifdef WIN32 #include <conio.h> #elif LINUX #include <ncurses.h> #else #error Need another OS #endif void pause (void) { int ch; /* flush the input buffer, just in case */ while ((ch = getchar()) != '\n' && ch != EOF); printf("Press any key to continue.. "); getch(); }
Registered Linux User #380033. Be counted: http://counter.li.org
If only it were that easy. With ncurses you have to call initscr to initialize the curses window and cbreak to disable line oriented mode for getch to work properly, then when you're done with the curses part of your program, you need to call endwin as the inverse to initscr. Or you could just write your own getch using Linux terminal controllers:
>you could always switch to c++ and useCode:#ifdef WIN32 #include <conio.h> #else #include <termios.h> #include <unistd.h> #include <stdio.h> int getch ( void ) { struct termios term, save; int c; /* Get the old terminal and save it */ tcgetattr ( STDIN_FILENO, &term ); save = term; /* Enter non-canonical mode */ term.c_lflag &= ~ICANON; term.c_lflag &= ~ECHO; term.c_cc[VMIN] = 1; term.c_cc[VTIME] = 0; /* Apply the new settings */ tcsetattr ( STDIN_FILENO, TCSANOW, &term ); /* getch! */ c = getchar(); /* Restore the original terminal */ tcsetattr ( STDIN_FILENO, TCSANOW, &save ); return c; } #endif void pause ( void ) { int c; while ( ( c = getchar() ) != EOF && c != '\n' ) ; printf ( "Press any key to continue..." ); fflush ( stdout ); getch(); }
>cin.get();
And that solves the problem how? get has the same problem as getchar in that input in C and C++ is line oriented. get is not the same as getch.
My best code is written with the delete key.
Be careful of using the function name "pause" under unix and its derivatives, as it's implemented already to wait for a signal.
oh i didnt read the problem as clear as i should, i though
he just wanted a way to pause the screen without running
an independent program, that was portable.
getche() does exactly what you are looking for. But it isn't standard, either.Windows XP Pro SP2 | Dev-C++ 4.9.9.2
[edit]
Or see the FAQ.
[/edit]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
i know it does what i'm looking for. lol
(post 4)i dual boot fedora core 4 x86_64
i'd like it to work in linux too.
thanks guys
Registered Linux User #380033. Be counted: http://counter.li.org
Found that on another board. It's supposed to be portable. It works by setting the IO-buffersize of stdin to 0. ( that is effectively clearing the buffer ) and setting it to the original size afterwards.
KurtCode:#include <stdio.h> void wait () { puts("press enter to continue."); setvbuf(stdin,NULL,_IONBF,0); setvbuf(stdin,NULL,_IOFBF,BUFSIZ); getchar(); }
Last edited by ZuK; 09-02-2005 at 04:55 AM.
That's a clever and creative solution. But it's not portable. setvbuf has to be called after the stream is open--okay so far, stdin is opened by the runtime startup code--and before any other input functions are called. So it can't be used to discard leftover characters in the stream.It works by setting the IO-buffersize of stdin to 0.
Just because I don't care doesn't mean I don't understand.