I wanted to know,,,clock(); functioning in C++,,,actually I am making a program based on IQ analysis and It has certain time limit in each question for answering,,,but as I don't know how to use clock(),,,,,that's why it's creating a problem for me.....plzzz help me out.....I would be very thankful...
alaa sam -2 Junior Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Your link appears to be broken -- I get an IE error that the page can not be displayed.
Fbody 682 Posting Maven Featured Poster
Try this one:
http://cplusplus.com/reference/clibrary/ctime/difftime/
I think it was the intended destination.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
That link works ok, but it does not sole the OPs question.
@valkyrie: You will want to ask the question(s) in another thead so that the main thread can cancel it out when the time has expired. Something on this order (assuming MS-Windows operating system). Note that you don't have to use clock() at all.
int questionThread(void* ptr)
{
// display and ask a question here
}
int main()
{
for each question
{
create thread using questionThread()
wait for thread to finish normally or timeout expires. WaitForSingleObject() will do that for you.
}
}
Edited by Ancient Dragon because: n/a
alaa sam -2 Junior Poster
<<I think it was the intended destination.
yes it was
pseudorandom21 166 Practically a Posting Shark
valkyrie 0 Newbie Poster
thanks for your...response...I have got to know how to implement clock();......but here's the real problem......whenever I launch my program,,,it works properly...all the questions are working within time limit...but it seems like they are taking much more time than expected....I guess perhaps...this clock() works on CPU time...isn't??
valkyrie 0 Newbie Poster
have a look at my program....
#include<iostream.h>
#include<conio.h>
#include<time.h>
#include<stdio.h>
#include<process.h>
void main()
{ clrscr();
char name[20],choice;
int score=0,user;
clock_t t1,t2;
double dif;
cout<<"\t\t\t\tNt$ MULTIBAY\n\n\n";
cout<<"ENTER YOUR NAME:";
cin.getline(name,20);
cout<<"\nBONJOUR"<<" "<<name<<" "<<"THIS PROGRAM IS NOW GOING TO GUIDE YOU THROUGH SOME UNUSUAL"<<"\n MENTAL TACTICS.\n";
cout<<"\n\t\t\t**INSTRUCTIONS**\n";
cout<<"*\nTHERE ARE SOME SIMPLE OBJECTIVE QUESTIONS.\n"<<"\n*QUESTIONS ARE BASED ON"<<"\nMENTAL ABILITIES AND CREATIVITY.\n"<<"\n*THERE IS CERTAIN TIME LIMIT FOR EACH SUCCESSIVE QUESTION,WHICH IS MENTIONED"<<"\n BESIDE IT. If you fall in out of time, then program exits automatically.\n"<<"\n*THERE IS CERTAIN KIND OF MARKING SCHEME ALSO,WHICH WILL BE SHOWN AFTER THE"<<"\n COMPLETION OF USER TEST.\n"<<"\n*USE OF DIGITAL GADGETS (calculator,mobile etc.) FOR ANSWERING IS NOT ALLOWED.";
cout<<"\nSO BE READY FOR ANALYSIS !!! ";
cout<<"\n\nEnter key code for beginning test.";
cin>>user;
if(user==007)
{ unsigned long MAX_TIME_LIMIT=7000/CLOCKS_PER_SEC;
t1=clock();
cout<<"\n\n\nQUE 01> A farmer had 20 hens. All but 2 died.How many hens are still alive?"<<" "<<"\nT=20seconds";
cout<<"\n\na)20 hens"<<"\nb)18 hens"<<"\nc)2 hens"<<"\nd)0 hens"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score=1;
break;
case 'b': score=2;
break;
case 'c': score=10;
break;
case 'd': score=4;
break;
case 'e': score=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 2> Divide 60 by half and deduce 20 from it. What do you get?"<<"\nT=20 seconds";
cout<<"\n\na)10"<<"\nb)60"<<"\nc)100"<<"\nd)120"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 3> The hour hand of an analog clock moves 1/60th of a"<<endl;
cout<<"degree every minute. Then how many degrees will the hour hand move"<<endl;
cout<<"in one hour?"<<"\nT=20 seconds"<<"\n\na)120 degree"<<"\nb)360 degree"<<"\nc)1 degree"<<"\nd)240 degree"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 4> If 5+3+2 = 151012, 9+2+4 = 183662, 8+6+3 = 482466, 5+4+5 = 202504 then"<<"\nwhat will be the answer of 7+2+5?"<<"\nT=20 seconds"<<"\n\na)198264"<<"\nb)138416"<<"\nc)143542"<<"\nd)156824"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 5> Five men were eating apples. A finished before B, but behind C."<<"\nD finished before E,but behind B. What was the finishing order?"<<"\nT=20 seconds"<<"\n\na)A.B.C.D.E"<<"\nb)A.C.B.D.E"<<"\nc)C.A.B.D.E"<<"\nd)D.A.C.B.E"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 6> Some pencils are erasers. All erasers are papers. Some papers are"<<"\npens and some pens are erasers. So pencils are obviously pens?"<<"\nT=20 seconds"<<"\n\na) It doesn't matter"<<"\nb)Can't say"<<"\nc)False"<<"\nd)True"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 7>Arnold bought a brand new car with one extra tyre. Each tyre can sustain"<<"\nfor maximum of 1200 kms."<<"\nIf all the tyres were used equally, how far can the car travel?"<<"\nT=20 seconds"<<"\n\na)900 kms"<<"\nb)1000 kms"<<"\nc)1500 kms"<<"\nd)1200 kms"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 8>Santa Express is one kilometer long and always travels at the speed of 60 kilometers per hour."<<
"\nIt passes through a tunnel which is one kilometer long. How long the Express"<<"\n will take to pass completely through the tunnel?"<<"\nT=20 seconds"<<"\n\na)60 minutes"<<"\nb)0.5 minute"<<"\nc)2 minutes"<<"\nd)1 minute"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 9>The game of Tic-Tac-Toe is being played between two players. Only the last"<<"\n mark to be placed in the game as shown.Who will win the game, O or X?"<<"\nAssume that both the players are intelligent enough."<<"\nT=20 seconds"<<"\n\na)X will win"<<"\nb)O will win"<<"\nc)Both have the same probablity"<<"\nd)Tie breaker"<<"\ne)How do you get such questions";
cout<<"\n\n\tX"<<"\tO"<<"\n\tX"<<"\n\tO"<<"\tO"<<"\tX"<<"\n\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 10>What would be the seventh rung of the following pyramid?"<<"\n\t1"<<"\n\t11"<<"\n\t21"<<"\n\t1211"<<"\n\t111221"<<"\n\t312211"<<"\n\t13112221"<<"\n\t________?"<<"\nT=40 seconds"<<"\n\na)1313513416"<<"\nb)5817251412"<<"\nc)1113213211"<<"\nd)4567214213"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
t1=clock();
cout<<"\n\nQUE 11>Count down the numbers of '#' from the given figure:"<<"\nT=40 seconds"<<"\n\n\t****#*#*###*@@@#****"<<"\n\t**#@#@@*@##**@@"<<"\n\t#*@*@**@@#"<<"\n\t@*@*#"<<"\n\t#"<<"\n\t#"<<"\n\t#*@*@"<<"\n\t#*@*@**@@#"<<"\n\t**#@#@@*@##**@@"<<"\n\t****#*#*###*@@@#****";
cout<<"\na)26"<<"\nb)27"<<"\nc)28"<<"\nd)29"<<"\ne)none of these"<<"\n\nEnter your choice:";
cin>>choice;
t2=clock();
dif=difftime(t2,t1);
if(dif>MAX_TIME_LIMIT)
{ cout<<"\nOUT OF TIME.";
exit(0);
}
switch(choice)
{ case 'a': score+=1;
break;
case 'b': score+=2;
break;
case 'c': score+=10;
break;
case 'd': score+=4;
break;
case 'e': score+=0;
break;
default: cout<<"wrong choice.";
}
cout<<"your total score:"<<" "<<score;
}
else
cout<<"\nINVALID KEYCODE.PROGRAM IS NOW TERMINATING.";
getch();
}
Edited by WolfPack because: Added [CODE=c++][/code] tags
valkyrie 0 Newbie Poster
and yah..is there any way to add images to the output screen of my C++??(m using borland!!!)....images may be of extensions like..bitmap..jpeg etc..
template<> 37 Junior Poster
Do you know how to use classes yet?
It would be cleaner to model your problem using classes.
pseudorandom21 166 Practically a Posting Shark
Your teachers don't care what technologies you use for that? That seems ood...
valkyrie 0 Newbie Poster
nope,,,we haven't reach to "classes" yet....
template<> 37 Junior Poster
If you must use clock then you problably do'nt want to use difftime since difftime is expecting time_t as input rather than clock_t. Note the function signature. double difftime ( time_t time2, time_t time1 );
Sample code show what happens when you pass clock_t into difftime rather than using time_t. You get a much larger delay.
1 #include <iostream>
2 #include <time.h>
3
4
5 int main()
6 {
7 clock_t c1 = clock();
8 time_t t1 = time(0);
9
10 sleep(1);
11
12 time_t t2 = time(0);
13 clock_t c2 = clock();
14
15 size_t cdiff = difftime(c2,c1);
16 size_t tdiff = difftime(t2,t1);
17
18 std::cout << "clock diff=[" << cdiff << "]" << std::endl;
19 std::cout << "time diff=[" << tdiff << "]" << std::endl;
20
21 return 0;
22 }
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.