Like Tree1Likes
  • 1 Post By CommonTater

time dependent program

This is a discussion on time dependent program within the C Programming forums, part of the General Programming Boards category; Hey friends. . . I wanna make a program in which a ques. Would ask to the person who run ...

  1. #1
    Registered User shubham's Avatar
    Join Date
    May 2011
    Posts
    14

    time dependent program

    Hey friends. . .
    I wanna make a program in which a ques. Would ask to the person who run this program. . .after putting the ques. A time of 10 seconds will appear and if the person can not answer the ques. Than the just next loop won't execute. . .how can i do this?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Using a combination of the signal() and alarm() system calls.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    16
    This is definitely more of a question than an answer, but could the OP create a new thread and then call something like (sleep) within that thread?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,197
    If I understand this correctly, you want a program to:
    - ask the user a question
    - count down from 10 seconds
    - if they cannot answer the question (or, I assume, answer it wrong) then the next loop won't be executed.

    This was kind of fun to put together. The trick you're looking for, I suspect, is a way to monitor for user input (to answer the question) but if no user input is present, to continue executing the program (to count down the timer).

    If so, then this can be accomplished with the "kbhit()" function. The "Sleep()" function is used to delay one second, and the count down is decreased by one. An argument of 1000 is passed to the "Sleep()" function (1000 milliseconds = 1 second).

    If the time runs out, the SECOND LOOP won't execute. If the user enters a guess, the countdown is skipped and, only if the answer is correct, the SECOND LOOP will execute.

    I hope this helps!

    - Matt

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    // -- FUNCTION DECLARATIONS -----------------------------------------------
    
    void PrintQuestion(int seconds);
    int  CheckInput(void);
    void PrintResult(int win);
    
    // -- MAIN FUNCTION -------------------------------------------------------
    
    int main()
    {
        int seconds = 10;   // count down timer
        int guess_input;    // users input
        int win = 0;        // "win" flag
    
        // start the countdown loop
        for(seconds = 10; seconds >= 0; seconds--)
        {
            // clear the screen
            system("cls");
            // print out the question, possible answers, and time remaining
            PrintQuestion(seconds);
            // call 1 second delay (1000 milliseconds)
            Sleep(1000);
            // check for user input - if none, this function is skipped (returns 0)
            guess_input = CheckInput();
            // if answered,
            if(guess_input != 0)
            {
                // ...if answer is correct, set "win" flag and leave loop
                if((guess_input == 'C')||(guess_input == 'c'))
                {
                    win = 1;
                    seconds = -1;
                }
                // ...if answer is wrong, clear "win" flag and leave loop
                else
                {
                    win = 0;
                    seconds = -1;
                }
            }
        }
    
        // print out the result of
        PrintResult(win);
    
        // SECOND LOOP (will not execute if the question is answered wrong)
        while(win)
        {
            // SECOND LOOP CODE HERE
            printf("\n\nSECOND LOOP IS EXECUTED\n\n");
            win = 0;    // clear loop variable when ready to exit the SECOND LOOP
        }
    
        return 0;
    }
    
    // -- FUNCTION DEFINITIONS -------------------------------------------------------
    
    //-----------------------------------------------------------------------
    // prints out the question, possible answers, and time remaining
    void PrintQuestion(int seconds)
    {
        printf("\n\nHow many ounces are in a cup?\n\n");
        printf("(A)  6\n");
        printf("(B) 12\n");
        printf("(C)  8\n");
        printf("(D) 16\n\n");
    
        printf("\n\nSeconds Remaining:  %2d\n\n",seconds);
    }
    
    //-----------------------------------------------------------------------
    // check for user input - if none, this function is skipped (returns 0)
    int CheckInput(void)
    {
        char keypress;
        int guess = 0;
    
        // if a key is hit, it is assigned to "guess" and returned to the main function
        // if a key is not being hit, zero is returned and the program continues
        if(kbhit())
        {
            keypress = (char)getch();
            // if keypress is between 'A' and 'D' (both upper/lower cases are checked for)
            if( ((keypress >= 'A' )&&(keypress <= 'D')) || ((keypress >= 'a' )&&(keypress <= 'd')) )
                guess = keypress;
            else
                // if bad input, "guess" variable defaults to 'A'
                guess = 'A';
        }
        // return the users input if present, or zero if not so the program will continue
        return guess;
    }
    
    //-----------------------------------------------------------------------
    // print the result of the guess
    void PrintResult(int win)
    {
        if(win)
            printf("\n\nCORRECT!\n\n");
        else
            printf("\n\nwrong answer...\n\n");
    }
    Last edited by Matticus; 06-16-2011 at 09:31 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    This was kind of fun to put together.
    I'm glad you enjoyed writting the OP's program for him... but how much do you think he learned from that?
    Did you help him become a better programmer or did you simply make him more reliant upon scoop and poop coding?

    Announcements - General Programming Boards
    Salem likes this.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,197
    Quote Originally Posted by CommonTater View Post
    Did you help him become a better programmer or did you simply make him more reliant upon scoop and poop coding?
    Announcements - General Programming Boards
    My apologies. I suppose in my eagerness to assist, I was overly helpful. I thought it would be clearer for the OP to see the function used in a functional program, and they could break it down to apply it to their own program, as my example was overly-simplied to illustrate the concept.

    I'll try to exercise more discretion in the future, though. Criticism accepted.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    My apologies. I suppose in my eagerness to assist, I was overly helpful. I thought it would be clearer for the OP to see the function used in a functional program, and they could break it down to apply it to their own program, as my example was overly-simplied to illustrate the concept.

    I'll try to exercise more discretion in the future, though. Criticism accepted.
    Good stuff... I didn't mean to be harsh but we do have to realize that handing out working examples of entire programs both violates the board's Homework policy and fails to actually help the questioner. Most often they just copy and paste (scoop and poop) the code into their own IDE, compile it to make sure it works... then hand it in. A better tactic is the one most here use... get them to post their attempts so far --"post your code..."-- and help them fix it, even if we see a far simpler and easier way to do it. If they do the work with hints from us, we know their brain is engaged far enough to try to think the problem through.

    Pay attention to Salem, Adak, Quzah and a few others... they are all good teachers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Time Dependent Movement?
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-30-2009, 07:01 AM
  2. Mutually dependent classes
    By MacNilly in forum C++ Programming
    Replies: 9
    Last Post: 03-17-2009, 03:39 AM
  3. time program showing time since epoch?
    By cus in forum Linux Programming
    Replies: 5
    Last Post: 01-10-2009, 12:56 PM
  4. non-dependent name in template function
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-11-2008, 06:07 AM
  5. Excel dependent on .Net Framework Runtime?
    By George2 in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 06:04 AM

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