Link to home
Start Free TrialLog in
Avatar of scully12
scully12

asked on

Press any key to continue...

What's an asy way to pause in my program. I want display some info to the user andthe I'll cout "Press any key to continue..." What can I put after that to pause the program until they hit a key. I'd rather not have it so they have to type something and then hit enter. I just want them to a hit on key.
Avatar of Billy_Pilgrim
Billy_Pilgrim

two ways:  using DOS or windows 95 / NT use the following call:

system("pause") ;

which is a com prg for dos, and only if you are writing console apps.

otherwise, do the following:

#include <stdio.h>
#include <conio.h>

int main()
{
   fprintf(stderr, "Press any key to continue. . .\n") ;
   _getch() ;  // this will pause you
   return 0 ;
}
Sounds like an answer to me.
yes, but here at EE we generally post answers as comments, thereby not locking the questions.  

when the questions are locked experts are not as likely to look at them.

the asker of the question can accept the comment as an answer. . .
>> Sounds like an answer to me.
It depends on the OS and compiler he is using.

scully12, standard C++ provides no way for you to do this because it line-buffers input.  That means the user must press the enter key before input is read, so if you use only standard C++ the user must press at least two keys, the last one being enter.

So if you want it to work based on only one key being pressed, you will have to resort to non-standard or OS-specific procedures.  These procedure may make your program unportable.  Billy_Pilgrim, has suggested a few, there are others, some that are likely to be better depending on the OS and compiler that you are using.
>> yes, but here at EE we generally post
>> answers as comments, thereby not locking
>> the questions.  
Not if you are confident that it is the right answer.  If you are reasonabley confident, lock the answer, this prevents disputes and saves other experts from wasting their time reading the question.  But I agree that in this case, since your "answer" depends on a guess as to what OS he is using, it would be inappropriate to lock the question with an answer.
ASKER CERTIFIED SOLUTION
Avatar of sumant032199
sumant032199
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sumant, you are once again answering with answers proposed by other experts.  That is unethical.  I could understand it if you did it once by mistake, but you keep doing it, that has to be intentional.  
Avatar of scully12

ASKER

Thanks, the second part did the trick.
I want to give the points to Billy_Pilgrim. How can I do that when sumant proposed his sentence as an answer.
You can't now.  When you grade a question the points go to the expert that has answered.  When an expert answers, you either accept that expert's answer awarding them the points, or you can reject that answer, which opens the question up to other experts.  You could have rejected sumant's answer and invited Billy_pilgrim to answer, then graded his answer.  Now you have accepted sumant's answer, so that is not possible.
thanks for clearing that up, nietod.

scully12-- no harm done.  but now you know how it works.
I am sorry. I won't do it again.