Thread: timed queston?

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    timed queston?

    hi

    Is it possible to make a question with a seartain time to answer it?
    like:
    Code:
    char question1[20];
    cout << "What are you?: ";
    cin << question1;                   
    //sleep here for 10 sec. and skip question if no answer?//
    
    if (question1 == "humen")
         {
          cout << "\nYou are right";
          }
    else cout << "I wouldnt think so";
    getch();
    And btw. Im a newbie at c\c++ programming still:P

    Thanks in advance.
    Boozy

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    not easily...

    also, you need to use strcmp() instead of question1 == "human"...you can't compare c-strings like that. c-strings are arrays. arrays aren't first class variables in c/c++. you have
    to compare each element in the array seperately which is what strcmp() does for you.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    thats true.. never tested that program and just wrote something so ppl could get some understanding af what Im trying to ask. so is not possible in c\c++ programming. was just thinking about mudgames; where a mobile in the game talkes while you walk around and something can hit you etc while you can do something else.. cant seem to understand that from anything about c\c++ I have read.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    you are pointing the c++ code on c form, any way what u have done is right in terms of C++ language, i.e b'cose == operator has been overloaded to check the two string.

    but in 'C' it is not , misplaced has explained why?

    s.s.harish

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by ssharish
    you are pointing the c++ code on c form, any way what u have done is right in terms of C++ language, i.e b'cose == operator has been overloaded to check the two string.

    but in 'C' it is not , misplaced has explained why?

    s.s.harish

    wrong..run the code
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish
    b'cose == operator has been overloaded to check the two string.
    This would be true if they were using the string class. However they're just using character arrays, so this is not the case.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Quote Originally Posted by boozy
    Is it possible to make a question with a seartain time to answer it?
    Yes it is. The problem you have with "waiting" is that the wait blocks, ie. no other code will run until the wait is done. So you're going to have to do something more than simply wait.

    A couple of options:
    • Write a loop which checks the current time against the time when the question was asked. Inside the loop, check to see if the user has pressed a key/given input. Exit the loop when the user has inputted an answer, or when the time check results in a timeout. Then process the result accordingly.
    • Create a thread with a timer in it, and use the main thread to wait for input. If the timeout occurs, the thread exits, and signals the other thread of the timeout.
    • Use the Windows API, and use the Timer functionality (SetTimer(), KillTimer()).


    Good luck.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    hmm.. thx:P but Im not 100 sure it will work.. hehe.. if I use a loop I cant make it exit afer 10 sec. but I can make it exit if time has passed 10 sec when the user hits a key, or I can get him to answer and say "you where to slow! sorry!" and move on to next question. the same would be for the rest of them.. if Im wrong could you please write a short program or just the loop af what you meen?:P

    thanks alot.

  9. #9
    sohlo
    Join Date
    Sep 2005
    Posts
    1
    I have same kind of problem. I have done this with threads but I think it is too complicated solution for later use. However this is how I have done it. In one thread I put timer waiting n seconds. And to other thread I put code that receives user input. Timer thread must set its cancellation state as PTHREAD_CANCEL_ENABLE so it can be cancelled if user gives input inside timeout. Both of these threads are created from main thread that stays wait joining of the timer thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Processing both I/O and timed events
    By drunken_scot in forum Linux Programming
    Replies: 6
    Last Post: 02-24-2009, 11:31 AM
  2. Timed loop in C program
    By bilmiyor in forum C Programming
    Replies: 10
    Last Post: 03-26-2008, 11:00 AM
  3. Input in timed loop
    By glo in forum Game Programming
    Replies: 12
    Last Post: 05-20-2006, 06:04 AM
  4. "connection timed out" for VNC
    By nomer in forum Tech Board
    Replies: 1
    Last Post: 04-27-2006, 06:31 PM
  5. A Timed function in C
    By jaro in forum C Programming
    Replies: 5
    Last Post: 04-03-2006, 07:46 AM