Time limit on cin

This is a discussion on Time limit on cin within the C++ Programming forums, part of the General Programming Boards category; Can you make it so that you have to enter a number... But! If you don't within 2 seconds, it ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Exclamation Time limit on cin

    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

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    i wouldnt use goto commands, they are basically dead in programming, and arent safe.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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 07: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

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I would use The Brains idea but my compiler doesn't have the include file <windows.h>

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Why are goto's bad?

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    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.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Hmm. I don't quite understand what you mean.
    Could you give me an example to work with?
    (I am a begginer at C++.)

  8. #8
    Gamer BloodyMaskKali's Avatar
    Join Date
    Feb 2005
    Posts
    1

    Goto's

    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.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    What would you recomed using instead of goto's then?

  10. #10
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    erm, Brain... maybe its the beer but, your example doesnt take any input.
    The program accepts user input here?
    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

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >>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

  12. #12
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 03:03 AM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 04:41 AM
  3. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM
  4. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM
  5. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21