Hi, Im trying to make a project that generates a random math question. And gives you limited time to answer it. But what happens is you can't answer it until the time is up. Then you have unlimited time. I don't know how to multitask, so that might be a solution. How else can i make it time me.
As @Softrix suggested, the most intuitive method is to make it multithreaded. However, there are other work arounds as well. Probably the easiest would simply be to store a timepoint of the time before the question is being answered, then see how long they took to answer the question. This does mean that its doesn't end it immediately if they run out of time, but when they do eventually answer it will tell them that they took too long. Here is a basic example:
#include <chrono>
// ...
using std::chrono::steady_clock::now;
using std::chrono::seconds;
using std::chrono::duration_cast;
std::cout << "What is " << one << " " << op << " " << two << "? ";
std::chrono::steady_clock::time_point start, finish;
start = now();
std::cin >> answer;
finish = now();
seconds time_taken = duration_cast<seconds>(finish-start);
if (time_taken.count() >= 5) {
std::cout << "You took too long!";
// exit in some way
}
(Note: There may be a syntax error in there, I haven't used std::chrono for a while).
I tried using thread, when I do <thread> and build it, it justs opens a new page that looks like this:
// Copyright (C) 2007-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file bits/c++0x_warning.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{iosfwd}
*/
#if __cplusplus < 201103L
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif
As the page says, you need to enable C++ 11 support. To do this (again, as the page says), enable it with -std=c++11 on the command line. If you are using Code::Blocks, you can also do this by going:
The Complier Settings / Compiler Flags section should be open by default, just scroll down the list of options until you find one saying something like "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]", check that and you should be fine to go.