Press any key to continue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Altman

    Press any key to continue

    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


  • Peter van der Goes

    #2
    Re: Press any key to continue


    "Bob Altman" <rda@nospam.com > wrote in message
    news:OOMX%2384G FHA.3968@TK2MSF TNGP14.phx.gbl. ..[color=blue]
    > 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
    >
    >[/color]
    Here's one approach...

    char cont;
    cout << "Press Enter to continue.\n";
    cin.get(cont);
    // whatever is next...
    --
    Peter [MVP Visual Developer]
    Jack of all trades, master of none.


    Comment

    • Å krat Bolfenk

      #3
      Re: Press any key to continue

      "Peter van der Goes" wrote:
      [color=blue]
      >
      > "Bob Altman" <rda@nospam.com > wrote in message
      > news:OOMX%2384G FHA.3968@TK2MSF TNGP14.phx.gbl. ..[color=green]
      > > 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
      > >
      > >[/color]
      > Here's one approach...
      >
      > char cont;
      > cout << "Press Enter to continue.\n";
      > cin.get(cont);
      > // whatever is next...
      > --
      > Peter [MVP Visual Developer]
      > Jack of all trades, master of none.
      >
      >
      >[/color]

      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.

      Comment

      • Bob Altman

        #4
        Re: Press any key to continue

        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


        Comment

        • Serge Baltic

          #5
          Re: Press any key to continue

          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;

          --
          Serge


          Comment

          • Hendrik Schober

            #6
            Re: Press any key to continue

            Bob Altman <rda@nospam.com > wrote:[color=blue]
            > [...] 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?[/color]

            I don't think you can do this portably in C++.
            [color=blue]
            > - Bob[/color]


            Schobi

            --
            SpamTrap@gmx.de 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


            Comment

            • Julie

              #7
              Re: Press any key to continue

              Bob Altman wrote:[color=blue]
              > 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?[/color]

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

              Comment

              Working...