473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

3 New Member
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
5 4254
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
Yapy
3 New Member
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
3,112 Recognized Expert Specialist
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
Yapy
3 New Member
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
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

0
by: neuge | last post by:
I am trying to write a Change password process to change user password on an Oracle 8i database with a Powerbuilder client. When the PL/SQL function supplied by Oracle (Verify Password) is...
4
by: jstaggs39 | last post by:
I have a form that requires a start date and an end date as input for the parameters then runs the form which open queries which are designed to populate certain tables. As it stands now, i can...
2
by: Jim S | last post by:
I am having a problem finding information regarding vb.net allowing you to create shortcut keys for buttons using the CTRL key + function buttons such as F1, F2 etc.. (Ex: Press CTRL+F1 to activate...
2
by: Jennifer Lee | last post by:
Hello, I have a rather simple function I've been using in 7.3.4 version ------------------------------------------------------------------------ ----------------- PostgreSQL 7.3.4 on...
1
by: Joel Byrd | last post by:
I've been using an eval() statement, which has been working fine until I put it inside of a function. Is this a known problem, and is there a known solution/work-around to this?
1
by: PLS | last post by:
Short example: namespace A { template<class Z> void f(Z &) {... } }
3
by: drakkofox | last post by:
Hi there, i need to subtract 36000 seconds, or a day, to get the yesterday's date, but with consistence check. for example, if i input the date 01/03/2007(dd/mm/yyyy) on the program, it should...
1
by: wuych | last post by:
I have a question about using iterator in template function //*****code starts here***************************** #include <vector> using std::vector; template<typename Tvoid foo( vector<T& a...
1
by: Matthew Wells | last post by:
Hello. I'm trying to use an asp - not html - button without having a postback or a screen refresh. I've tried turning off "CausesValidation" and "UseSubmitBehavior" and I still get a screen...
5
by: Tinku | last post by:
I am sorry for asking this silly question, but i don't understand why it is happening please suggest me ================================= #include <stdio.h> int main() { static int i=1;...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.