Catchinmg the Enter Key for c++

  • C/++/#
  • Thread starter ermines
  • Start date
  • #1
45
0

Main Question or Discussion Point

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???
 

Answers and Replies

  • #2
379
0
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
dduardo
Staff Emeritus
1,898
3
You probably want to use system("pause");
 
  • #4
1,356
2
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:
  • #5
robphy
Science Advisor
Homework Helper
Insights Author
Gold Member
5,462
680
  • #6
508
1
like this
Code:
do
  key = getch();
while(key != 13)
 
  • #7
1
0
"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 :)
 

Related Threads for: Catchinmg the Enter Key for c++

Replies
1
Views
1K
Replies
4
Views
4K
Replies
3
Views
4K
  • Last Post
Replies
0
Views
1K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
0
Views
2K
  • Last Post
Replies
5
Views
8K
Top