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");
}