473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

timer for getline() function

hiii all
i want to read a string from commandline through getline() in c++. For that i want to add a timer of 5 sec. If no string read, then the program will terminate. can anybody know ?? I am using Centos 6.3
thanks
Mar 20 '13 #1
11 2167
/* Wait 5 seconds. */
alarm(5);

/* getline */

/* Cancel alarm. */
alarm(0);
Mar 20 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
What are you trying to do? Wait 5 seconds for user input and then go ahead?
Mar 20 '13 #3
Banfa
9,065 Expert Mod 8TB
getline can read from any input stream including cin
Mar 20 '13 #4
weaknessforcats
9,208 Expert Mod 8TB
You're right. I'll edit my post.
Mar 20 '13 #5
thnq suvarna.. it worked well.
But, at last it is giving one message "Alarm clock". How to suppress that one ??? and also if i want to do another task after 5 sec with exiting from program.. is this one also possible ??
-suvarna271
Mar 21 '13 #6
-weaknessforcats
Exactly... but, not in wait. In that 5 sec time it needs to go to some other function and come back to the same getline(). Then with in 5 sec if i not entered any string, then again it has to got to that function and come back (looping)..
Main purpose is__ Timer for getline(), with in that time it needs to execute another function and come back .. any ideas ??
thanks
Mar 21 '13 #7
weaknessforcats
9,208 Expert Mod 8TB
In that case you need to implement a multi-threaded program where your input is on a worker thread. That allows yur main thread to go elsewhere but also to return occasinally and inquire on the worker thread.

Not a beginner solution but multithreading is very common.
Mar 21 '13 #8
thank you -weaknessforcats
i already got the solution using <pthreads>
Now, i dnt want to use that. is there any other way to solve the same problem ..?? Challenging one... !!

thanks
Mar 22 '13 #9
donbock
2,426 Expert 2GB
I don't believe this can be accomplished within the Standard C Library, but perhaps your implementation provides the necessary functions...

Do you have an input function that returns immediately with a success/failure status rather than blocking until the input is obtained? If so, then you can write your main function to do its main job and only occassionally check for input.
Mar 22 '13 #10
-donbock
ha ha.. main problem is that only.. if i have a proper function to return immediately, i never go for threads and alarm and timers all these. okay you came to my point. Now give your ideas about immediate returning(atleast 2 sec) from getline() either with a success or failure .. ??
thanks
Mar 23 '13 #11
weaknessforcats
9,208 Expert Mod 8TB
You will need to write your own input functions. Especially the one for the user input. All the standard functions wait indefinitely for the user to press the enter key. Your functon must only 2 secs for a keypress so you need to write your own input functions.

The standard input functions are just to get yoou started. Most applicatons of any size don't use them. Windows does not use them.
Mar 23 '13 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
5
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
4
by: Tcc | last post by:
Hi all, I have a question about using the function getline().. e.g. I have defined a structure: struct s { string *a; Queue *b; }; then in my main function:
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
4
by: Hagay Lupesko | last post by:
Hi, I've encountered a strange phenomena which appears to me as a bug: I have an engine that uses a System.Threading.Timer to invoke a delegate every X minutes. The code looks something...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
1
by: l46kok | last post by:
Just wondering, for character of arrays, you can set the delimiter manually like cin.getline(character array, size, delimeter) But for the STL string, apparently, you have to use the following...
8
by: sabby | last post by:
I want to use the getline() so that i can enter a entire name in on line. (with spaces) The prob is that i am initializing the variable as "N/A" and saving it to a text file. it is declared as a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.