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
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; }
Last edited by The Brain; 04-10-2005 at 06:43 PM. Reason: accounted for the case where current_tick could equal two_second_delay by using the => operator
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
I would use The Brains idea but my compiler doesn't have the include file <windows.h>
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.
The program accepts user input here?erm, Brain... maybe its the beer but, your example doesnt take any input.
Code:keydown=getch();
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
>>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
Good suggestion though.
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel