I was wondering how to call the "press any key to continue" display in c++ using visual c++.
I was wondering how to call the "press any key to continue" display in c++ using visual c++.
in a console application, you can do
#include <stdlib.h>
system("pause");
or, a little more drawn out, but useful if the above code doesnt work:
#include <iostream.h>
int x;
cout << "Press any key to continue...";
cin >> x;
I came up with a cool phrase to put down here, but i forgot it...
Well here it is. If you use stdio.h and conio.h,
printf("Press any key to continue");
ch = getche(); \\ ch must be defined, its a variable
if you use iostream,
cout<<"Press any key to continue";
cin>>ch; \\ ch must be defined, its a variable
I havent tried it but I'm pretty sure that worksCode:#include <iostream.h> #include <conio.h> //I think... int main() { cout<<"Press any key to continue..."<<endl; if(!kbhit()) { exit(0); } return 0; }
+++
++
+ Sekti
++
+++
Quoting Homer Simpson: "Where's the any key?"
Yeah I know...dumb...but I couldn't resist.
- purple - http://purplexed.tk
just so you know, the cin >> variable method wont work cause that one waill wait until it gets an input before it's done so if you click enter before having written anything the only thing that happens is that it moves to the next line.
some body wrote a thing like this i think:
Dunno if it works....Code:#include <conio.h> void pause() { while(!kbhit()); getch(); }
The bad thing is that conio.h is generally screwed up.
The eziest way is:
#include <stdlib.h>
...
system("PAUSE");
OR
#include <iostream.h>
#include <conio.h>
...
cout << "Press any key to continue...";
getch();
dont use pause - its very pooor programming practice for various reasons - go with kbhit - its the best way
you couls also use
int wait;
cout<<"Any key to continue!\n";
wait=getch();
You don't have to use return values, so you don't need wait.wait=getch();
Code:cout<<"Any key to continue!\n"; getch();
All spelling mistakes, syntatical errors and stupid comments are intentional.
#include <iostream>
using namespace std;
int main()
{
printf( "Press any key to continue..." );
fflush( stdin );
getchar();
return ( 0 );
}
Or you could not...
>fflush( stdin );
UN-DE-FINED. You cannot use fflush on input streams.
Here's a run down of this thread, summarized into neato little Prelude comments for your enjoyment.
>how to call the "press any key to continue" display in c++
Ask the user for input
>system("pause");
Godawful slow, only use in extreme cases of need.
>cin >> x;
This is good, but you don't need a variable since all you're asking the user to do is enter 'something'.
>ch = getche(); \\ ch must be defined, its a variable
ch is not needed, getche is not standard and may not exist
>if(!kbhit())
Once again not standard and may not exist, so why bother if you don't have to have it?
>Quoting Homer Simpson: "Where's the any key?"
Ctrl+Alt+Delete
>getch();
I wonder how many programmer's heads will pop if we broadcast across the world that conio is not standard C or C++.
>system("PAUSE");
Easy, yes. Bad idea, oh yea.
>its very pooor programming practice for various reasons
Depends on what you are doing
>getch();
C_Coder...you're excused since everyone else was suggesting this. I'll assume you were going with the flow
>fflush( stdin );
Wrong, very wrong
>getchar();
Standard, effective, efficient...this is the best solution offered in this thread. I would have suggested it if Unregistered didn't. cin.get() would also work well.
-Prelude
My best code is written with the delete key.