Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Catchinmg the Enter Key for c++

  1. Sep 26, 2005 #1
    Hello guys. I'm trying to do the command "PLEASE PRESS ENTER KEY TO RETURN TO MAIN MENU."

    The problem is that I don't know how to catch this enter key. I do know that its ascii value is 13. I tried using sscanf and getch but can't seem to make it work.

    void StockBroker::showAllBalances()
    char choice[80];
    int i;
    sscanf(choice, "%d", &i );

    StockHolder holder;
    cout << endl;

    cout << endl;
    cout << "PRESS ENTER KEY TO RETURN TO MAIN MENU..." << endl;
    cin >> choice;

    of cource, this program is under another program named broker.showAllBalances().

    So anyone who could help me???
  2. jcsd
  3. Sep 26, 2005 #2
    do you have a case statement for your menu? or some if-else statements?
  4. Sep 26, 2005 #3


    User Avatar
    Staff Emeritus

    You probably want to use system("pause");
  5. Sep 26, 2005 #4
    lol...your trying to capture "enter" using %d. That maybe your problem =]. To capture ascii values i believe you need to us %c because their a character mapping. Ah nvm. I read your code wrong. My bad sorry.

    however if you wanna get enter i suggest using printf printf("%c",value);

    your string scanf looks a bit funky because the buffer your using doesn't have a
    initial value.

    also I tend to stay away from c++ I/O, i like scanf/printf
    Last edited: Sep 26, 2005
  6. Sep 26, 2005 #5


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

  7. Sep 26, 2005 #6
    like this
    Code (Text):

      key = getch();
    while(key != 13)
  8. Sep 28, 2009 #7
    "cin" can't detect the enter key
    you may try "cin.getline()"
    and check if the string = "\r"
    or if strlen(urString)==0

    hope this can help :)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook