Quick question

Started by
8 comments, last by Sabonis 18 years, 4 months ago
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!
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Advertisement
void pressEnter(){     char input = 0;     do     {          while(!_kbhit());          input = _getch();             if(input == 0)               input = _getch();     }     while(input != 13);  // is 13 enter?}
You could do:
#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
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
#include <windows.h>
...
while(!GetKeyState(VK_RETURN));
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
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
would I be wrong with the code :
char temp
while(cin >> temp != '\n')
{}

please help guys!
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
thanks!
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...

This topic is closed to new replies.

Advertisement