wait for keyboard entry ?

P

Pelo GANDO

Hi everybody,

It's me again...Pelo, for an other beginner question...

What the code should be in C++ to wait for a keyboard entry in order to
execute the sequal of my program...
 
P

Pelo GANDO

I found by myself...

#include <conio.h>
cout << "Press any key to terminate this program. " ; getch();

The getch() function call causes the program to wait for a single keystroke.
 
M

Mike Wahler

Pelo GANDO said:
I found by myself...

#include <conio.h>
cout << "Press any key to terminate this program. " ; getch();

The getch() function call causes the program to wait for a single
keystroke.

But alas it's not part of the C++ language. It's an
'extension' provided by your implementation, not topical
here. Here we discuss the ISO standard C++ langauge, which
has no means to do what you're asking.

See:
http://www.slack.net/~shiva/welcome.txt

-Mike
 
P

Pelo GANDO

Sir,
How would you do in ISO standard C++...
I would like knwo from you...

Thank you
 
J

Jacob Jensen

What the code should be in C++ to wait for a keyboard entry in order to
execute the sequal of my program...

You can use std::cin which redirects input from stdin (your keyboard) to a
string in your program. Example

#include <string>

int main()
{
std::string sInputString;
// Wait for input from stdin (the keyboard)
std::cin >> sInputString;

// Print out to the screen what the user just input
std::cout << "The user just input the following string: " <<
sInputString << std::endl;
}

I hope this helps
 
A

amateur

Pelo GANDO said:
Hi everybody,

It's me again...Pelo, for an other beginner question...

What the code should be in C++ to wait for a keyboard entry in order to
execute the sequal of my program...

c++ doesn't have such a concept.

If you work under character environments such as unix or old dos you'll
usually call a single function to achieve that. Non standard headers like
<conio.h> for dos may provide what you're looking for. Others will point you
to correct newsgroups.

If you work under some GUI like Windows or the X, than your whole program
concept changes. You wait for specific messages/callbacks from your
OS/GUI/Framework manager or whatever. And that's a complex topic which is
not discussed here.
 
M

Mike Wahler

Pelo GANDO said:
Sir,
How would you do

[nonblocking input]
in ISO standard C++...
I would like knwo from you...

Like I said, it cannot be done with standard C++.
You'll need to use an extension, such as the
one you mentioned. However you should note
that this will only work with that particular
compiler, others will have their own way, if
they have any at all. When you encounter this
situation, I advise you to isolate and clearly
identify such uses, so porting will be easier
when the time comes.

-Mike
 
J

Jacob Jensen

What is wrong with
std::cin >> SomeString;

Jacob



Mike Wahler said:
Pelo GANDO said:
Sir,
How would you do

[nonblocking input]
in ISO standard C++...
I would like knwo from you...

Like I said, it cannot be done with standard C++.
You'll need to use an extension, such as the
one you mentioned. However you should note
that this will only work with that particular
compiler, others will have their own way, if
they have any at all. When you encounter this
situation, I advise you to isolate and clearly
identify such uses, so porting will be easier
when the time comes.

-Mike
 
?

=?ISO-8859-1?Q?Tor_Husab=F8?=

Jacob said:
You can use std::cin which redirects input from stdin (your keyboard) to a
string in your program. Example

#include <string>

int main()
{
std::string sInputString;
// Wait for input from stdin (the keyboard)
std::cin >> sInputString;

// Print out to the screen what the user just input
std::cout << "The user just input the following string: " <<
sInputString << std::endl;
}

I hope this helps
This would require the user to press Enter before anything would happen,
and that's probably not good enough. (Assuming the 'Press any key to
continue' thing is what he's after.) :)

But printing 'Press Enter to continue.' and then reading from
std::cin could be a viable solution, although less elegant then
conio's getch() and the likes.

Tor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top