cin?

This is a discussion on cin? within the C++ Programming forums, part of the General Programming Boards category; how can i configure cin to wait until enter is pressed?...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    106

    cin?

    how can i configure cin to wait until enter is pressed?
    C++ Makes you Feel Better

    "Gravity connot be held reponsible for people falling in love"--Albert Einstein

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Please elaborate; I can't think of a case where cin doesn't wait until enter is pressed.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    106
    normaly cin wait for a char or number to be entered and then pressed i mean that cin wait for just a enter
    C++ Makes you Feel Better

    "Gravity connot be held reponsible for people falling in love"--Albert Einstein

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Oh, I get it.

    No, cin by itself doesn't do that, but you can use the function cin.get() to do that.

    Just add this:

    Code:
    cin.get();
    ...where you want it to do that.

    Oh, and if the program skips over it without waiting, add this right before it:

    Code:
    cin.ignore('\n', 10);
    That should do it.

    Alternately, you could also use this:

    Code:
    system("PAUSE");
    ...to make it wait until any key is pressed. The downside of that method is that it has a predefined message that it displays when it waits.
    Last edited by Gabu; 07-22-2002 at 05:28 AM.

  5. #5
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128

    Using Getch

    I think you could also use getch by asking it to look for the ASCII value of return, I think its '13' I did a similar thing in an exercise a bit ago.

    declare the control variable as an int, so it looks for ASCII value.

    Code:
    int enter;
    
    
    while (enter!='13')
    {
        getch(enter);
    
        if (enter != '13')
        {
            cout<<"do it again.";
        }
    }
    I'm pretty sure that should work.
    Last edited by UnclePunker; 07-22-2002 at 07:06 AM.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    While 'getch()' is an option, be aware that it doesn't take an argument. (This will become apparent when you attempt to compile the code, of course, but I thought I'd throw it out.)

    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    {
    char key;
    
         do
         {
          clrscr();     // makes it look like nothing's happening
          std::cout << "Press ENTER to continue...";
          key = getch();
         }while (key != '\r');  // C++ (not ASCII) escape char for carriage return
    
         std::cout << "\n\nThanks for finally pressing the ENTER key!";
    
    getch();
    return 0;
    }
    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 12:14 PM
  2. Check if a cin is null
    By HumbuckeR in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2006, 09:16 AM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 03:57 PM
  5. multiple instances of cin
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2004, 05:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21