Catchinmg the Enter Key for c++

  • Context: C/C++ 
  • Thread starter Thread starter ermines
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around capturing the Enter key in a C++ program, specifically in the context of a menu system. Participants explore various methods and functions to achieve this, including the use of ASCII values, input functions, and alternative approaches.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty in capturing the Enter key and mentions the ASCII value of 13, indicating attempts with sscanf and getch without success.
  • Another participant questions the use of %d for capturing the Enter key, suggesting that %c might be more appropriate for character input.
  • A suggestion is made to use system("pause") as a potential solution for waiting for user input.
  • One participant recommends using getch() in a loop until the Enter key is detected.
  • Another participant suggests using cin.getline() to read input and check if the string equals "\r" or has a length of zero to determine if Enter was pressed.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to effectively capture the Enter key, with no consensus reached on a single solution.

Contextual Notes

Some participants note issues with the initial value of the buffer used in the sscanf function, and there are varying preferences for C++ I/O versus C-style functions.

ermines
Messages
45
Reaction score
0
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;
count << endl;

holder.showBalance();
count << endl;
count << "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?
 
Technology news on Phys.org
ermines said:
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;
count << endl;

holder.showBalance();
count << endl;
count << "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?

do you have a case statement for your menu? or some if-else statements?
 
You probably want to use system("pause");
 
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 want to 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:
like this
Code:
do
  key = getch();
while(key != 13)
 
"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 :)
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
5K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
13K
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
12
Views
2K