CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2003
    Location
    Bangalore
    Posts
    38

    small doubt about cin>>

    Hi ,

    We know that when we want to get input from console most frequently used one is cin and it waits infinitely for input.

    But I want to use cin to get input without waiting more than specified time.That time may be some thing like after 5 sec.

    Is it possible or not?

    If so, can any one tell me the way to do it?


    with regards...
    seshu
    Last edited by seshreddy; April 30th, 2003 at 12:04 AM.
    make it possible

  2. #2
    Join Date
    Jan 2002
    Location
    TamilNadu, India
    Posts
    158
    Nope! there is no time specific input readers.
    Muthu

  3. #3
    Join Date
    Apr 2003
    Location
    Bangalore
    Posts
    38
    how it will be if u use operator overloading?
    but what happens when we want to infinitely for the input in some conditions of the application.

    please think abnout it..


    with regards...
    seshu
    make it possible

  4. #4
    Join Date
    Jan 2002
    Location
    TamilNadu, India
    Posts
    158
    Sesh,
    I am not really sure how overloading will be effective in this matter. A possible workaround could be running a separate thread with our regular cin to wait for the input. The parent process can do a Waitforsingleobject(handle,SPECIFIEDTIME);
    If the input has been entered, return and come out. Otherwise kill the thread.
    I am not really sure abt the overloading idea u r trying to say.
    Muthu

  5. #5
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    Would a cout be read by cin? Maybe you test this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void cin_timeout( void* pVoid )
    {
    long timeout = pVoid;
    Sleep( timeout );
    cout << "timeout";
    }
    
    int main()
    {
    string s;
    _beginthread( cin_timeout, 0, 5 );
    cin >> s;
    return 0;
    }

  6. #6
    Join Date
    Apr 2003
    Location
    Bangalore
    Posts
    38
    Hi,

    thank u all.

    i tried to run the program using thread but i am getting error C2065: '_beginthread' : undeclared identifier

    this is vc++ compiler and windows 2000.

    this is the code that our friend has given abd which i too tried.

    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <process.h>

    using namespace std;

    void cin_timeout( void* pVoid )
    {
    long timeout = (long)pVoid;
    Sleep( timeout );
    cout << "timeout";
    }

    int main()
    {
    string s;

    _beginthread( cin_timeout, 0, 5 );
    cin >> s;
    return 0;
    }

    bye
    seshu
    make it possible

  7. #7
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    You need to compile with /MT, but I just tried and it doesn't work

  8. #8
    Join Date
    Apr 2003
    Location
    Hyderabad,India
    Posts
    486
    you may get rid of the error with proper settings. i feel.

    If you try your code in a MFC supported console application will do the work.
    start a console project but in the next step select "an application that supports MFC" this will start a console application with tmain()

    then you can do the work.
    this is may work as a solution for your error.
    and don't forget to add #include<process.h>
    it solves the problem of the compiler error

    cheers
    mahanare
    Last edited by mahanare; May 2nd, 2003 at 01:31 AM.
    Thanks n Regards
    Harinath Reddy
    Learn Hello World Program
    A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible. There are no prima donnas in engineering. - Freeman Dyson

  9. #9
    Join Date
    Apr 2003
    Location
    Bangalore
    Posts
    38
    hello



    how can we do it with mfc support console application?

    i tried to do but again same error is coming...

    look at this..

    bye
    seshu
    make it possible

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured