Sign in to follow this  
Sabonis

Quick question

Recommended Posts

Sabonis    169
if im using c++ and i want to have a function called pressEnter() where all it does is waits until the user hits enter, how would i do this? thanks!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster

void pressEnter()
{
char input = 0;
do
{
while(!_kbhit());
input = _getch();

if(input == 0)
input = _getch();
}
while(input != 13); // is 13 enter?
}

Share this post


Link to post
Share on other sites
Sabonis    169
hi Dave,
I tried your solution but it doesnt work unless you enter and character or number and then hit enter.... I need it to work if just the enter key is hit

Share this post


Link to post
Share on other sites
dave    2187
Quote:
Original post by Sabonis
hi Dave,
I tried your solution but it doesnt work unless you enter and character or number and then hit enter.... I need it to work if just the enter key is hit


Oh poo, sry. Early morning here.

Share this post


Link to post
Share on other sites
Sabonis    169
im using unix to make a c++ program.. just need it to wait until user presses enter key
*bump*
sorry for being annoying but i need to have this code done for the morning! thanks

Share this post


Link to post
Share on other sites
Endar    668
That should be fine. When I was using linux, I always used 'getchar', but it shouldn't matter.


while( std::cin.get() != '\n' );


Although, you might want to check if there is something in the file stream already.


if( /* input still in the stream from previous user input */ )
while( std::cin.get() != '\n' );

cout << "Press any key to continue." << endl;

while( std::cin.get() != '\n' );


Or, the second one might not be suitable for your program.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this