Can you make it so that you have to enter a number...
But! If you don't within 2 seconds, it executes a goto command.
Thanks, August
Printable View
Can you make it so that you have to enter a number...
But! If you don't within 2 seconds, it executes a goto command.
Thanks, August
i wouldnt use goto commands, they are basically dead in programming, and arent safe.
Code:#include<iostream>
#include<windows.h>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
long current_tick, two_second_delay = (GetTickCount()+2000);
string user_input="";
char keydown;
do{
cout << "Enter something: " << user_input;
if(kbhit())
{
keydown=getch();
user_input+=keydown;
}
current_tick = GetTickCount();
clrscr();
}while(current_tick < two_second_delay && keydown!='\n');
if(current_tick => two_second_delay)
cout << "You snooze, you lose.";
//else
//continue processing the rest of the program
return 0;
}
I would use The Brains idea but my compiler doesn't have the include file <windows.h> :(
Why are goto's bad? :confused:
erm, Brain... maybe its the beer but, your example doesnt take any input.
Cool-August: You can check stdin to see if there is anything there waiting for you by using select(). Use a 2 second time out and don't read from stdin unless select() indicates that the file descriptor has changed status.
Hmm. I don't quite understand what you mean.
Could you give me an example to work with?
(I am a begginer at C++.)
The bad thing about goto's is that they usally create hard to read code
and that can mess your program up in the later stages. The only good use for goto's I can think of is if you are in a very very deep nest of if's and loops then you need your program to go back to a earlier loop or if AND that is even arguable if you should do that.
What would you recomed using instead of goto's then?
The program accepts user input here?Quote:
erm, Brain... maybe its the beer but, your example doesnt take any input.
Code:keydown=getch();
>>The program accepts user input here?
you edited your code an hour and a half after my post. The original just waited 2 seconds and exited without ever taking input :rolleyes:
Good suggestion though.