Thread: Press a key

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Question Press a key

    I have written a console program. It should wait untill user presses a key. What function should I use to get a key from keyboard?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    You can use fgetc(), or getch() if your compiler has conio.h
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    It works. Thanks. Last time I tried to use getc() from stdio.h but it didn't work as I wanted.
    Now, how can I flush the buffer?
    Last edited by siavoshkc; 01-18-2006 at 05:15 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    When I use
    Code:
    cout<<"Press a key to exit..."<<endl;
    getch();
    It works fine.

    But when I write
    Code:
    cout<<"Press a key to exit...";
    getch();
    It doesn't print last line to screen. But after I press a key it prints it and exits. Why?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you are mixing c++-style io-functions ( cout << ... ) and c-style functions ( getchar() ). that is never a good idea.
    if you would just use
    Code:
    char x;
    cout<<"Press a key to exit...";
    cin >> x;
    you would not have that problem because cin and cout are synchronized.
    if you still want to use your way you could flush cout before waiting for a char.
    Code:
    cout<<"Press a key to exit...";
    cout.flush();
    getchar();
    Kurt

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I'm pretty certain it has something to do with endl - IIRC it flushes the output buffer. Not sure if that's a problem, someone else will have to verify that.

    Note that getchar() and getch() are two different functions, he's using the latter.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by ahluka
    I'm pretty certain it has something to do with endl - IIRC it flushes the output buffer. Not sure if that's a problem, someone else will have to verify that.
    That is right outputting endl on cout will automatically flush the buffer. '\n' at the end of the string would not. But the OP didn't even have a '\n' at the end of his outputstring I assumed that he wants the curser to stay on the same line. In that case flush is the only way to make shure the string gets displayed.
    Quote Originally Posted by ahluka
    Note that getchar() and getch() are two different functions, he's using the latter.
    Ok I missread that. But that makes it even worse. Don't think that a nonstandard function will be synchronized with cout.
    Kurt

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code:
    cin.get();
    That is what I have always used.

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    OK. And what about flushing input stream?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    if you want them to press "any" key to exit, just do something like this...

    Code:
    int main()
    {
    //whatever you want to go here
    system("pause");//waits till user hits any key
    }

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    What is it? Is it a system function? What should be included?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by Wraithan
    Code:
    cin.get();
    That is what I have always used.
    It doesn't work It exits without wait.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  13. #13
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Please post your updated code.

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Two varients:
    Code:
     
    cout<<"Press a key to exit...";
    cout.flush();
    getch();
    and
    Code:
    system("pause");//waits till user hits any key
    The second code is better because there is no need to flush the input buffer. My program works fine now, thanks a lot.

    But I still have two questions:
    1) How can I flush input stream? (With cin.clear()?)
    2) What is this magical function "system()"?
    Last edited by siavoshkc; 01-19-2006 at 12:57 AM.

  15. #15
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by siavoshkc
    Two varients:
    Code:
     
    cout<<"Press a key to exit...";
    cout.flush();
    getch();
    and
    Code:
    system("pause");//waits till user hits any key
    The second code is better because there is no need to flush the input buffer. My program works fine now, thanks a lot.

    But I still have two questions:
    1) How can I flush input stream? (With cin.clear()?)
    2) What is this magical function "system()"?
    Except system() hands control over to the program it calls without regard to which program it calls, which could potentially be replaced by a malicous program - security risk. Not that it's much of an issue here, just something to keep in mind. The FAQ has an article on this.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM