By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

How do I stop a loop after 60 seconds by using the difftime(time_t, time_t) function

P: 3
This is part of the program I wrote. I need to use the
difftime(time_t, time_t) function but I am not sure of how to use it.


do {
switch(operation)
{
case 1:
answer=random_integer1 + random_integer2;
time(&start);
cout << random_integer1 << "+" << random_integer2 << endl;
cin >>value;
if (answer == value)
{
cout<<"You are right!"<<endl;
}
else
{
cout<<"You are wrong!"<<endl;
}
time(&end);

}
dif = difftime (end,start);

}while (dif <60);
Aug 27 '10 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
difftime returns the difference in seconds expresssed as a double. So use a double in your code:

Expand|Select|Wrap|Line Numbers
  1. if (difftime(timeA, timeB) > 60.0)
  2. {
  3. etc...
Aug 27 '10 #2

P: 3
How do i loop the program then? If i use do while loop, what is the condition that i should use? thanks

if(difftime(end,start)<60)
{
do
{
cout<<"What's your name?"<<endl;
cin>>name;
}while (condition)

}
Aug 27 '10 #3

Nepomuk
Expert 2.5K+
P: 3,112
OK, so you want to run the while loop for how long? Exactly, while the difference is smaller than 60. So, put that in the condition and voila! A functioning loop.

Of course, the difference between start and end has to be calculated every time you check, not just once - otherwise you'll get an infinite loop.

Greetings,
Nepomuk
Aug 27 '10 #4

P: 3
After analyzing all the given suggestions, I came up with this code:

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;


int main()
{
srand((unsigned)time(0));
time_t start,end;
int random_integer4, random_integer5, adv_operation, adv_value, random_integer6;
int adv_ans;
double dif_total=0.00;


adv_operation = (rand()%2)+1;

do{
switch(adv_operation)
{
case 1:
random_integer4 = (rand()%30)+1;
adv_ans= random_integer4 * random_integer4;
time (&start);
cout << random_integer4 << (char)253 <<endl;
cin >> adv_value;
if (adv_ans == adv_value)
{
cout<<"You are right!"<<endl;
}
else
{
cout<<"You are wrong!"<<endl;
}

time (&end);
dif_total = dif_total + difftime(end,start);

break;

case 2:
random_integer5 = (rand()%30)+1;
random_integer6 = random_integer5 * random_integer5;

adv_ans = random_integer5;
time (&start);
cout << (char)251 << random_integer6 << endl;
cin >>adv_value;
if (adv_ans == adv_value)
{
cout<<"You are right!"<<endl;
}
else
{
cout<<"You are wrong!"<<endl;
}

time (&end);
dif_total = dif_total + difftime(end,start);
break;
}


}while(dif_total<10.0);

cout<<"Your score is"<<endl;

return 0;
}

Now I have problem putting the difftime(end, start) into a function itself. How do I put it into a function?
Thanks.
Aug 28 '10 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
Just write a function that takes three arguments. The two times and the limit value. Return false if the return from difftime exceeds the limit, otherwise return true.

Since there wouldn't be aby logic in the function beyond the call to difftime, why do you feel the need to put difftime in its own function?
Aug 28 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.