So I am trying creating a proof of concept for a bigger project. I am currently working on a timed quiz, that has only 1 question, and you have 10 seconds to answer.
What im really asking
I know I can read the users input by doing
"cin << Var" or "Var = _getch()"
and I can make a timer by doing
clock_t timer;
timer = clock();
//Code
timer = clock() - t;
But how do you put that all together? can you have a timer running while it's asking for input? It doesn't seem like it would, since c++ goes line by line executing each part and waiting until its done before moving on. But there has to be a way! Here is what i have came up with...
bool Question(int Correct) { int Answer = 0; cin >> Answer; if (Answer == Correct) { return true; } else { return false; } } int main() { cout << "1 + 1 is: "; clock_t Timer; Timer = clock(); bool Is_Correct = Question(2); Timer = clock() - Timer; cout << "You Answered: "; if (Is_Correct) { cout << "Correct!"; } else { cout << "Wrong!"; } cout << "\nAnd by the way, you answered the question with " << 10 - (Timer / CLOCKS_PER_SEC) << " Seconds to Spare.\n"; cin.get(); cin.get(); return 0; }
Sorry about the spacing, it got kinda messed up.