Catchinmg the Enter Key for c++

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

The discussion focuses on capturing the Enter key in a C++ program, specifically within the context of a StockBroker application. Users suggest various methods, including using `getch()` to detect the ASCII value of 13, which corresponds to the Enter key. The original poster's attempt to use `sscanf` and `cin` for this purpose is deemed ineffective. Recommendations include using `cin.getline()` to read input and checking for the Enter key by comparing the input string to "\r".

PREREQUISITES
  • Understanding of C++ input/output operations
  • Familiarity with ASCII values and character mapping
  • Knowledge of functions like `getch()` and `cin.getline()`
  • Basic programming concepts such as loops and conditionals
NEXT STEPS
  • Research the use of `getch()` in C++ for capturing key presses
  • Learn how to implement `cin.getline()` for reading strings in C++
  • Explore character comparison techniques in C++ to detect specific key inputs
  • Investigate alternative libraries for handling keyboard input in C++ applications
USEFUL FOR

C++ developers, particularly those working on console applications that require user input handling, and anyone looking to improve their understanding of keyboard event detection in C++.

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
4K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
12
Views
3K
Replies
10
Views
2K