Press any key to continue

  • Thread starter Thread starter Bob Altman
  • Start date Start date
B

Bob Altman

Hi all,

I have an unmanaged console app written in C++. I can't figure out how to
get C++ to do the standard issue "press any key to continue" thing:

cout << "Press any key to continue: ";
cin >> <your code goes here>;

TIA,

- Bob
 
Bob Altman said:
Hi all,

I have an unmanaged console app written in C++. I can't figure out how to
get C++ to do the standard issue "press any key to continue" thing:

cout << "Press any key to continue: ";
cin >> <your code goes here>;

TIA,

- Bob
Here's one approach...

char cont;
cout << "Press Enter to continue.\n";
cin.get(cont);
// whatever is next...
 
Peter van der Goes said:
Here's one approach...

char cont;
cout << "Press Enter to continue.\n";
cin.get(cont);
// whatever is next...

Try this:

#include <iostream>

using namespace std;

int main()
{
std::cout << "Hello, world!" << std::endl;

system("pause");
}

I know it works in std C++ - unmanaged. If any one knows for managed wrapper
please post it.
 
Thank you both! The first approach [cin.get()] serves the intended purpose,
but requires that the user press ENTER. The second approach
[system("pause")] does exactly what I wanted, but it's seriously
non-portable.

As an exercise for my own education, let me rephrase the question. Suppose
I have an application that allows the user to enter a command by pressing a
single key on the keyboard. How would I accept the command into a char
variable?

- Bob
 
BA> As an exercise for my own education, let me rephrase the question.
BA> Suppose I have an application that allows the user to enter a
BA> command by pressing a single key on the keyboard. How would I
BA> accept the command into a char variable?

1) getch(), that's for plain C ;)

2) Maybe this:

char ch;
cin >> ch;
 
Bob Altman said:
[...] Suppose
I have an application that allows the user to enter a command by pressing a
single key on the keyboard. How would I accept the command into a char
variable?

I don't think you can do this portably in C++.


Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
 
Bob said:
As an exercise for my own education, let me rephrase the question. Suppose
I have an application that allows the user to enter a command by pressing a
single key on the keyboard. How would I accept the command into a char
variable?

There isn't a portable solution. You either must accept key+enter or resort to
non-portable/platform-specific solutions.
 
Back
Top