PDA

View Full Version : Catchinmg the Enter Key for c++


ermines
Sep26-05, 04:54 AM
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???

ComputerGeek
Sep26-05, 08:51 AM
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?

dduardo
Sep26-05, 03:30 PM
You probably want to use system("pause");

neurocomp2003
Sep26-05, 04:32 PM
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

robphy
Sep26-05, 05:11 PM
Try getch()

http://www.google.com/search?q=getch+cpp

gerben
Sep26-05, 05:53 PM
like this

do
key = getch();
while(key != 13)

cAsper89
Sep28-09, 02:05 PM
"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 :)