Closing the input stream after a certain time

This is a discussion on Closing the input stream after a certain time within the C++ Programming forums, part of the General Programming Boards category; Hello, I am a fairly experienced C programmer and now I am trying my hand at C++. Is there any ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    Closing the input stream after a certain time

    Hello, I am a fairly experienced C programmer and now I am trying my hand at C++.
    Is there any way to close the input stream in a program after a certain amount of time has passed? Also, is there any way to indicate that the time is passing?
    Thank you very much.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    AFAIK there are only non-standard ways of doing this that depend on your OS and compiler. Normally cin waits for the user to enter their input before returning control to the program. A function like kbhit() might be available on your platform that returns true only if the user hit a button. You can use Sleep or sleep in a loop to check periodically and after a certain amount of time stop checking.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Probably the same way you would do it in C, and that would depend on which OS and Compiler you have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, there's a kinda semi-standard way of getting around what you want... you can't really close the input stream, but you can use your own input stream and cut it off, and AFAIK, the only way to time something and accept input at the same time is system-dependant (like others have said)
    Code:
    #include <iostream>	//for I/O
    #include <ctime>	//for standard time functions
    
    void timeAndSpin(const int seconds);	//a timer with a spinner
    
    int main()
    {
    	char*line=new char[20];		//to hold the input
    	std::istream*input=&std::cin;	//assign the standard input to your
    					//stream
    	std::cout<<"Enter your name: ";	//ask for input	
    	input->get(line,20,'\n');	//get it with your stream
    	std::cout<<"You Entered: "<<line<<std::endl;	//confirmation
    	
    	input=0;	//"close" your stream
    	delete[]line;	//free up the memory
    	
    	timeAndSpin(5);	//run the timing function
    
    	return 0;
    }
    
    void timeAndSpin(const int seconds)	
    {
    	char*spinner="|/-\\";	//use a spinner to time
    	int i=0;		//for the position in the spinner
    	int time=seconds*CLOCKS_PER_SEC;	//use a standard constant
    	int start=clock();	//start the timer
    	int end;		//save memory for the end of the timer
    
    	//here we create an interval - how often the spinner moves.  I set it
    	//to every 10th of a second
    	const int INTERVAL=static_cast<int>(CLOCKS_PER_SEC*0.1f);
    	
    	//start the spinner
    	std::cout<<'|';
    	
    	do
    	{
    		//set 'end' variable to now
    		end=clock();
    		//if it's time to update the spinner
    		if(end%INTERVAL==0)
    		{
    			//update the 'i' variable
    			i=(i<4?++i:0);
    			//output some backspaces, the spinner, and the time
    			//NOTE: I/O functions take ALOT of time and processing
    			//power - try not to keep this in a tight loop
    			std::cout<<"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
    				<<spinner[i]<<' '<<end<<std::flush;
    		}
    	}while(end-start<=time);	//keep doing it until time runs out
    }
    of course putting the timing and input together right is up to you, and that method is pretty processor-intensive (using clock() with a loop)... basically you'll need to find a way to time something and accept input at the same time, and a way to, when you get the input or time runs out, break out of that routine.
    Last edited by major_small; 10-08-2005 at 01:28 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flushall()
    By salvadoravi in forum C Programming
    Replies: 21
    Last Post: 12-24-2007, 12:39 PM
  2. Buffering??
    By Brain Cell in forum C Programming
    Replies: 15
    Last Post: 09-21-2004, 07:57 AM
  3. Input looping......it appears once extra time!?
    By siumui in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 10:19 PM
  4. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 07:51 PM
  5. clearing input stream
    By Sub in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2002, 08:59 PM

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