C++ Auto Timing

Hey guys, can someone help me with a function subprogram or a set of statements that allow me to do something within a time limit. I want to create a self-regulated test such that:

cout <<"Press 'S' to start"<<endl;
cin >>ready;

while(!((ready == 's')||(ready == 'S')))
{
cout <<"Press 'S' to start"<<endl;
cin >>ready;
}
if((ready == 's')||(ready == 'S'))
while(time != 0) *this is the part I need*
{
//here we have the actual test
}

Is this possible?
This is possible, but there's nothing in the standard library which will handle it for you. Timing depends on counting CPU clock cycles, which is very operating-system dependent.
You can set an integer equal to time(0) and then check the time against that integer until whatever time has passed.
1
2
3
4
5
6
7
8
9
10
11
#include <ctime> 

int main()
{
    int x = (time(0)) + 30;
    while(x > y)
    {
        y = (time(0)); 
    }
    return 0; 
} 
Ok im trying to play around with those, thanks for the replies
Ok i've incorporated the <ctime> method but it gives an infinite loop, here is the complete prototype:

#include <iostream>
//#include <fstream>
//#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

const string ans = "41";
char opt,ready,conf;
string name,surn,answer;
int y,x = (time(0) + 120);

int main()
{
system ("title M220 Self Regulating Test");
cout <<" Welcome To The Self-Efficient Test Trial"<<endl;cout <<endl;
cout <<" Enter First Name"<<endl;cout <<endl;

cout <<" ";cin >>name;cout <<endl;
cout <<" Enter Surname"<<endl;cout <<endl;
cout <<" ";cin >>surn;cout <<endl;
cout <<" "<<name<<", are you ready to begin your M220 test? (Y/N)"<<endl;
cout <<" ";cin >>opt;

if((opt == 'Y')||(opt == 'y'))
{
cout <<" Please Note"<<endl;
cout <<" ***************"<<endl;cout <<endl;
cout <<" You Have 120 Seconds To Complete This Test"<<endl;
cout <<" ==========="<<endl;cout <<endl;
cout <<" Lets Begin !"<<endl;cout <<endl;
cout <<" Press `S' to Start"<<endl;
cout <<" ";cin >>ready;
while(!((ready == 'S')||(ready == 's')))
{
cout <<"Signal readiness by pressing `S'"<<endl;
cout <<" ";cin >>ready;
}

if((ready == 'S')||(ready == 's'))
{
while(x > y)
{
y = (time(0));
system("cls");
cout <<endl;cout <<endl;cout <<endl;cout <<endl;cout <<endl;cout <<endl;
cout <<" You Have 120 Seconds To Complete This Test"<<endl;cout <<endl;
cout <<" Question 1 - Given the system: "<<endl;
cout <<" ============ "<<endl;
cout <<endl;
cout <<" 3x - 2y + z = 1 "<<endl;
cout <<endl;
cout <<" 2x + y - 3z = 6 "<<endl;
cout <<endl;
cout <<" x + 2y + 2z = 0 "<<endl;
cout <<endl;
cout <<" Evaluate the determinant by expanding"<<endl;
cout <<" along the second row."<<endl;
cout <<endl;cout <<endl;
cout <<" Enter Answer "<<endl;
cout <<" Det. = ";cin >>answer;cout <<endl<<endl;
cout <<" Confirm Answer: ["<<answer<<"] ? (Y/N) (No Undo!!)"<<endl;
cout <<" ";cin >>conf;
while(!((conf == 'Y')||(conf == 'y')))
{
cout <<" Re - Enter Answer "<<endl;
cout <<" Det. = ";cin >>answer;
cout <<" Confirm Answer: ["<<answer<<"] ? (Y/N) (No Undo!!)"<<endl;
cout <<" ";cin >>conf;
}
if(answer == ans)
{
cout <<endl;
cout <<" Correct, Congradulations!!"<<endl;cout <<endl;
cout <<" Score : 98% "<<endl;
}
else
{
cout <<endl;
cout <<" Incorrect answer"<<endl;cout <<endl;
cout <<" Score : 0% "<<endl;
}
cout <<endl;
cout <<" End of Test"<<endl;cout <<endl;
system("pause");
}
}
}

return 0;
}


It doesn't give an infinite loop. The loop will break after 120 seconds, if the conditions are evaluated after 120s.
-What you want is a break; for when the test is completed, so as to not run the test again.
-The timing to start only after they have entered their name and stuff.

***-Ideally, a separate thread running to constantly evaluate the time and to break the loop if the time is reached. This is very advanced stuff.

-More realistically, just to say the test was failed if y > x when the test is completed.

Edit: Typo
Last edited on
Yeah I see what you mean, that's the most probable thing to do. I was just wondering if I can actually do the statements while the time is counting down.

Can you shed some light on how to create the separate thread, I'm familiar enough with C++ t follow
You can use the C++11 <thread> if you're compiler supports that.
http://en.cppreference.com/w/cpp/thread

Otherwise, you have the option of boost thread: http://www.boost.org/doc/libs/1_55_0/doc/html/thread.html

But yeah basically you should follow this tutorial (which is marvelous):

http://www.codeproject.com/Articles/598695/Cplusplus11-threads-locks-and-condition-variables

Post up something in this thread (lol) if you're struggling when it comes to incorporating this into your own code.
Topic archived. No new replies allowed.