Thread: need help with ctime

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    need help with ctime

    Code:
    #include < iostream >
    using namespace std;
    
    int main()
    {
    	int num;
    
    	cout << "Enter your lucky number"  << endl;
    	cin  >> num;
    
    	//if the user takes longer than 5 seconds to enter a number...
    	
    	//cout << "You took too damn long" << endl;
    	//exit ( EXIT_FAILURE );
    
    	return 0;
    }
    I'm not sure what to do with that if statement. I'm guessing you include ctime or some time header file that can accomplish such a task.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    There is no standard way to do non-blocking input.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Won't be ctime... At least not only ctime. You have to implement a timer that can constantly be polling the system, or provide a callback to the system. Should be able to do it with threads (check out pThreads perhaps).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    I had no idea it would that challenging to do something like that. Are you sure there isn't an easy way of doing that?

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Speaking of time, does anyone know how to make a "real" timer? I'm not talking about something cheap like this:

    Code:
    #include < iostream >
    using namespace std;
    
    int main()
    {
    	int miliseconds = 0;
    	int seconds     = 0;
    	int minutes     = 0;
    	int hours       = 0;
    
    	for ( ; ; )
    	{
    		cout << hours   << " : "
    			 << minutes << " : "
    			 << seconds << " : "
    			 << miliseconds++;
    		
    		system ( "cls" );
    
    		if ( miliseconds == 60 )
    		{
    			miliseconds = 0;
    			seconds++;
    		}
    
    		if ( seconds == 60 )
    		{
    			seconds = 0;
    			minutes++;
    		}
    
    		if ( minutes == 60 )
    		{
    			minutes = 0;
    			hours++;
    		}
    	}
    
    	return 0;
    }

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try the clock( ) function.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    in a console app can't you only have 1 thing going on at a time? so i guess having a timer within the dos app while asking for user input wont work

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You can have multiple threads of execution. What you'd need to do to make the timer is to create two threads of execution - one for user input, and one for the timer (which would essentially be a for loop which polls the system). If the time reached a certain point, the timer thread must cancel the input thread. If that time is not reached, then the threads must be rejoined into the main execution thread.

    Probably a bit overkill for your program.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes in C++
    By Kool4School in forum C++ Programming
    Replies: 22
    Last Post: 01-21-2009, 09:26 PM
  2. Replies: 5
    Last Post: 09-26-2006, 01:51 PM
  3. Stop ctime from breaking line?
    By trenzterra in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2005, 11:09 AM
  4. time precision (ctime), tm struct question
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2003, 01:51 PM
  5. ctime() error...implementation maybe?
    By [EMOBA] in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2002, 03:57 PM