Thread: getch() twice??

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    getch() twice??

    im writing a simple console program and i want this simple console program to stop at certain points and wait for the user to hit enter.

    this simple task is accomplished by me putting the following:

    Code:
    cout << "Press enter to continue...";
    getch();
    the only problem is, the code wont stop there. but if i put getch() in there twice, like so:

    Code:
    cout << "Press enter to continue...";
    getch();
    getch();
    it works. whats going on here, and how do i get getch() to work like its supposed to?
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You probably have some input left in the buffer from an ealier cin or something similar.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    How it's supposed to work is undefined. You've probably got a stray '\n' in a buffer somewhere that is being eaten up by the first getch(). Perhaps if you state your compiler/os some-one may be able to give you another non-standard way of doing the same thing that doesn't look like a kludge.
    Joe

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    ok, i guess now would be a good time to ask:

    what is a buffer and how do i keep input from being left in it? *blush*

    also, im using Dev C++
    I came up with a cool phrase to put down here, but i forgot it...

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    A buffer is a region in memory that can be used in a similar way to a shock absorber. To even out frequent i/o operations in your case. You can empty a buffer by calling a function that removes i/o that has been stored there (as you have done with the double getch()).

    However, without seeing the code that is filling your particular buffer, it's hard to suggest an alternative for emptying it (I was under the impression that most implementations of getch() didn't use buffered i/o anyway). However if you don't mind the characters being echoed on the screen you could try cin.get().
    Joe

  6. #6
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    If the getch thing doesn't work, you could always, (and only if you NEED to) use system("pause"); it's good, but a tad slow.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Slow?
    Hehe. Who cares if it's a bit slow, when the whole purpose is to pause and wait for the user?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    getche() will also "echo print" the input.

    The trick is to add cin.ignore(x, '\n') or cin.ignore(x, '\0') following getch(), where 'x' represents an arbitrarily significant number of chars meant to completely flush the buffer, i.e. 10, 20, 100.

    All that being said, don't use getch() or getche(). They're both non-standard and non-portable. cin.get() is both standard and, to the best of my knowledge, portable.

    The potential problem with cin.get() is that get() reads up to - keeping this very specific to the topic - '\n', for example, but leaves it in the stream. get() doesn't consume the newline character the way that getline() does (for those taking notes).

    One way to skirt the portability issue (suggested by Prelude) is to create a function such as 'pause();'. Simply define the function using suitable code, even a system call, that a user can easily change to accomodate his/her environment:
    Code:
    // my methodology here
    
    #include <cstdlib>
    
    inline void pause() { system("PAUSE"); };
    
    int main(void)
    {
    // code
    
    pause();
    return 0;
    }
    Here 'pause();' can be used anywhere in the code and can be changed at the function declaration/defintion without messing with anything else in the program. (Documentation would be a very nice touch, by the way.)

    Note that on a Windows O/S, you're going to get that "Press any key to continue..." message on a 'system' pause, which may not be acceptable for all applications.

    -Skipper

    P.S. Agreed with Sang-drax. We're stopping the program here.
    Last edited by skipper; 11-12-2002 at 06:15 PM.
    "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. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 11:24 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM