Catchinmg the Enter Key for c++

  • C/C++
  • Thread starter ermines
  • Start date
  • Tags
    C++
In summary, the problem is that the user doesn't know how to capture the enter key and is trying to do so using a character value. The user may try using getch() to try and capture the enter key. Alternatively, they may try cin.getline() to see if the string includes the enter key. If either of these methods don't work, the user may try strlen() to see if the string has the enter key.
  • #1
ermines
45
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;
cout << endl;

holder.showBalance();
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?
 
Technology news on Phys.org
  • #2
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;
cout << endl;

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

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

1. What is the purpose of catching the Enter key in C++?

The Enter key is used to indicate the end of user input in many programs. By catching the Enter key, the program can respond to this input and perform specific actions.

2. How can I catch the Enter key in my C++ program?

In order to catch the Enter key, you can use the getch() function from the conio.h library. This function reads a single character of input from the keyboard, including the Enter key. You can then compare the input with the ASCII value for the Enter key (13) to trigger a specific action.

3. Can I use the cin function to catch the Enter key in C++?

No, the cin function will not register the Enter key as user input. Instead, it will wait for the user to input a different character or press the Enter key twice to indicate the end of input.

4. How can I ignore the Enter key in my C++ program?

If you do not want your program to respond to the Enter key as input, you can use the ignore() function from the iostream library. This function will ignore the next character in the input buffer, which will be the Enter key, and allow the program to continue without responding to it.

5. Are there any alternative methods for catching the Enter key in C++?

Yes, there are other methods for catching the Enter key in C++ such as using the getchar() function or using a loop to continuously check for the Enter key input. However, using the getch() function is the most efficient and reliable method for catching the Enter key.

Similar threads

  • Programming and Computer Science
Replies
2
Views
870
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
10
Views
956
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top