I did it!!!!

This is a discussion on I did it!!!! within the C++ Programming forums, part of the General Programming Boards category; Thanks to all you guys who helped me out on making my program pause without using getch() or system("pause"); here ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    I did it!!!!

    Thanks to all you guys who helped me out on making my program pause without using getch() or system("pause");

    here is a short example of what I did (I had to google as I was going insane!)

    Code:
    //this is an example of another method to pause screen
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    
    cout << "Hello I am an example" << endl;
    
    cin.ignore(cin.rdbuf()->in_avail() + 1); // This is the NEW line!!
    
    return 0;
    
    }
    The only prob is that after it pauses you have to press a key then hit enter to quit the program, but what do you guys think

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    HOw about pressing just enter ? works fine for me

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    22
    Quote Originally Posted by swgh
    Thanks to all you guys who helped me out on making my program pause without using getch() or system("pause");

    here is a short example of what I did (I had to google as I was going insane!)

    Code:
    //this is an example of another method to pause screen
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    
    cout << "Hello I am an example" << endl;
    
    cin.ignore(cin.rdbuf()->in_avail() + 1); // This is the NEW line!!
    
    return 0;
    
    }
    The only prob is that after it pauses you have to press a key then hit enter to quit the program, but what do you guys think
    Hey, you know, if you just want to pause the screen like you did, you can use this:
    cin.get();
    that will allow you to pause and then after you press enter it continues, but if you are planning to have some if statements or switch statements, typing something inside the blank space of cin.get(); might interfere with them.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    How about if I want to press "any key" without enter?

    I mean in terms of streams- not getch().

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    AFAIK it is not possible in standard C++.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    Can you tell me why?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Because different operating systems handle console input in different ways, and so it would be hard for a standard C++ function to know when the user hit a button. The way it works now is that only after the user hits enter does the OS send the data to cin. Non-standard functions like getch don't wait for the OS to send the input to cin, they work with OS specific code to get the keypress immediately.

Popular pages Recent additions subscribe to a feed

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