I'm new to C++, and need to know how to you wait for someone to hit the enter key at the console before you call a function?
Here is what I have, and it's not working:
string str;
cin %26gt;%26gt; str;
if (str == "\n")
{
celldetect();
display2();
}
else
{
if (str == "q")
{
exit(1);
}
In C++, how do you wait for someone to hit the enter key?
Hello,
In the conio.h library, there is a function that is exactly what your looking waiting for...
=====================
// The include
#include %26lt;conio.h%26gt;
int main()
{
// The method to wait for keystroke
cout %26lt;%26lt; "Please press a Key ";
getch();
}
=====================
The getch() function call causes the program to wait for a single keystroke.
Or you can use kbhit() in the same header file..
=====================
#include %26lt;conio.h%26gt;
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
while(1)
{
if(kbhit())
{
break;
}
}
}
=====================
Now you want to find if the "Enter" key is pressed, you can do the following ...
=====================
void main()
{
int ch;
while(1) // Wait till Keyboard hit ...
{
ch = _getch();
if (ch == 13 ) {
cout %26lt;%26lt; "You pressed Enter" %26lt;%26lt; endl
break;
}
}
}
=====================
I hope that helped...
I think the above code compiles hehe... If not, search on those two terms and you will get what your looking for..
Take a look at the thread below for a detailed response
Reply:use getch(), getchar() or getche() functions defined in conio.h header file
Reply:u need an infinite loop, all ifs and keyboard watching included in loop ;) good luck!
marguerite
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment