Results 1 to 2 of 2

Thread: waiting for <RETURN>

  1. #1

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158

    Smile waiting for <RETURN>

    Hi I'm coding a program to run under the console (UNIX) and need a way to hold the screen until the user hit's the <RETURN> key before proceeding. I have usually achieved this in other languages by just having a temp read in statement and it works fine but under C++ it behave differently.

    This is the function I made:

    Code:
        void waitForReturnKey()
        {
            char temp;
            cout << "Press <RETURN> to continue..." << endl;
            cin >> temp;
        }
    But this requires the user to type a char, but all I want is the user to hit return without typing anything. Also the problem with this is that is the user enters more than 1 char then a bug occurs.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try:
    Code:
    char temp;
    cin.get(temp);
    That will get the next character entered and then move on. So you'd need to have that in a loop to get return:
    Code:
    char temp = 'a';
    
    while(temp != '\n')
        cin.get(temp);
    ...or something
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width