How to Get Timed Input using cin


 
Thread Tools Search this Thread
Top Forums Programming How to Get Timed Input using cin
# 1  
Old 02-08-2005
Bug How to Get Timed Input using cin

Hi,

I am trying to get the input like option from the user using cin but if the user is not responding for some time I want to use the default value which does not need users interaction can any one help to do this in unix c++?

int main()
{
char sex = 'M' ;
cout << "Are you Male/Female (M/F)?" << flush ;
cin >> setw(1) >> sex ; // Here I don't want to wait indefinatily

switch(sex)
{
case 'F' :
// do something
break ;
default :
case 'M' :
// do something
break ;
}

retrurn 0 ;
}

Thanks In advance.
Balasubramnaian
# 2  
Old 02-09-2005
Wait some time for the input otherwise use default value.

Hi,

I am trying to get the input like option from the user using cin but if the user is not responding for some time I want to use the default value which does not need users interaction can any one help to do this in unix c++?

int main()
{
char sex = 'M' ;
cout << "Are you Male/Female (M/F)?" << flush ;
cin >> setw(1) >> sex ; // Here I don't want to wait indefinatily wait for
// some 20 seconds for the user input if he is not
// provided the input by that time use default

switch(sex)
{
case 'F' :
// do something
break ;
default :
case 'M' :
// do something
break ;
}

retrurn 0 ;
}

Thanks In advance.
Balasubramnaian
# 3  
Old 02-22-2005
Quote:
Originally Posted by uxbala
Hi,

I am trying to get the input like option from the user using cin but if the user is not responding for some time I want to use the default value which does not need users interaction can any one help to do this in unix c++?

int main()
{
char sex = 'M' ;
cout << "Are you Male/Female (M/F)?" << flush ;
cin >> setw(1) >> sex ; // Here I don't want to wait indefinatily wait for
// some 20 seconds for the user input if he is not
// provided the input by that time use default

switch(sex)
{
case 'F' :
// do something
break ;
default :
case 'M' :
// do something
break ;
}

retrurn 0 ;
}

Thanks In advance.
Balasubramnaian
see non blocking read() help, you can do with that
# 4  
Old 02-22-2005
Thanks for your reply I will try to do that.


Quote:
Originally Posted by nmds
see non blocking read() help, you can do with that
# 5  
Old 02-22-2005
Quote:
Originally Posted by uxbala
Thanks for your reply I will try to do that.
Sample Application to implement timed read
Code:
#include <sys/time.h>

int main()
{
   fd_set fdset;
   struct timeval timeout;
   int  rc;
   int  val;

   timeout.tv_sec = 6;   /* wait for 6 seconds for data */
   timeout.tv_usec = 0;


   FD_ZERO(&fdset);

   FD_SET(0, &fdset);

   rc = select(1, &fdset, NULL, NULL, &timeout);
   if (rc == -1)  /* select failed */
   {
       printf("ERROR path\n");
       val='E';
   }
   else if (rc == 0)  /* select timed out */
   {
       printf("DEFAULT path\n");
       val='D';
   }
   else 
   {
      if (FD_ISSET(0, &fdset)) 
      {
         val = getchar();
      }
   }
   printf("VAL is %c\n", val);
}


Last edited by Perderabo; 02-23-2005 at 01:28 PM.. Reason: Add code tags for readability
# 6  
Old 02-23-2005
WoW! Cool!!! It works Thanks for your help.

Quote:
Originally Posted by nmds
Sample Application to implement timed read
#include <sys/time.h>

int main()
{
fd_set fdset;
struct timeval timeout;
int rc;
int val;

timeout.tv_sec = 6; /* wait for 6 seconds for data */
timeout.tv_usec = 0;


FD_ZERO(&fdset);

FD_SET(0, &fdset);

rc = select(1, &fdset, NULL, NULL, &timeout);
if (rc == -1) /* select failed */
{
printf("ERROR path\n");
val='E';
}
else if (rc == 0) /* select timed out */
{
printf("DEFAULT path\n");
val='D';
}
else
{
if (FD_ISSET(0, &fdset))
{
val = getchar();
}
}
printf("VAL is %c\n", val);
}
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Timed Scripts

Hi all I need a little bit of help, i am looking for a script that can have different events in it and then if it is a certain day email me about it some sort of email reminder system any ideas thanks (4 Replies)
Discussion started by: ab52
4 Replies

2. Solaris

I/O timed out

I have Ultra 45 Sun solaris box with Solaris 10 installed. My problem is when i boot the unix box, i got the message: What does this message meant? then it does not continue to boot successfully. Please help. Thanks in advance. (5 Replies)
Discussion started by: etcpasswd
5 Replies

3. Programming

C++ cin problem

Hi, I have recently started using C++. I use g++ on Unix. I could not get a simple C++ program working. The program is; #include <iostream> using namespace std; int main() { double a, b; cout << "enter your number"; cin >> a; b = a + 1.15; ... (4 Replies)
Discussion started by: apprentice
4 Replies

4. Programming

Timed wait?

Is there any way in which I can make my wait signal to wait for a specified time for child job to complete. And if that time is over, the program gets out of the wait signal to process other things (4 Replies)
Discussion started by: anjul_thegreat
4 Replies

5. HP-UX

connection timed out

I am trying to connect with my hp machine using "dialup networking." It times out after 30 seconds. Is there a way to adjust this time. Would it have anything to do with rexec? thanks (0 Replies)
Discussion started by: paschal
0 Replies

6. UNIX for Dummies Questions & Answers

timed commands

Hello, How can I set up events to be executed at a certain time? And do I need some kind of privilege such as being in cron group? (2 Replies)
Discussion started by: rayne
2 Replies

7. UNIX for Advanced & Expert Users

deferred: connection timed out with NT

We recently installed a new release of SCO UNIX (5.0.6) and when I try to relay e-mail from the UNIX box to my NT server (the mail server) I get the following message from sendmail. Deferred: Connection timed out with nt I have nt set up as my relay server in sendmail.cf and the mail seems to... (8 Replies)
Discussion started by: jmossman
8 Replies

8. UNIX for Advanced & Expert Users

Connection Timed out

I connect to a Sun Box through telnet but it timed out in couple of minutes. Advance thanks for any idea...help... (2 Replies)
Discussion started by: s_aamir
2 Replies
Login or Register to Ask a Question