Quick question
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!
void pressEnter(){ char input = 0; do { while(!_kbhit()); input = _getch(); if(input == 0) input = _getch(); } while(input != 13); // is 13 enter?}
You could do:
Note that APs solutions is non-blocking and mine isn't.
#include <iostream>using std::cin;void WaitForEnter(){ char temp; cin >> temp;}
Note that APs solutions is non-blocking and mine isn't.
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
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
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.
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
*bump*
sorry for being annoying but i need to have this code done for the morning! thanks
That should be fine. When I was using linux, I always used 'getchar', but it shouldn't matter.
Although, you might want to check if there is something in the file stream already.
Or, the second one might not be suitable for your program.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement