473,545 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
+ 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(operatio n)
{
case 1:
answer=random_i nteger1 + 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 4261
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_oper ation)
{
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,st art);

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,st art);
break;
}


}while(dif_tota l<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
7781
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 enabled for the profile the Alter User username identified by newpassword fails with a ORA-20003 Verify password has failed. The function works...
4
3310
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 only run the form one month at a time, so i would enter the first of the month, say 01/01/2002 and the end of the month 01/31/2002 and it would run the...
2
29101
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 a click event on a button). If anyone could help it would be sincerely appreciated. Thank You!
2
1858
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 i686-pc-cygwin, compiled by GCC gcc (GCC) 3.2 20020927 (prerelease) (1 row)
1
1971
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
966
by: PLS | last post by:
Short example: namespace A { template<class Z> void f(Z &) {... } }
3
2536
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 output 28-02-2007. or if i input 01/01/2006 it should output 31/12/2006. example would be... program asks day, then month, then year, then it outputs...
1
2184
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 ) { vector<T>::iterator i; // ERROR, can't use iterator }
1
3445
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 refresh - which makes me think I'm doing a postback. I've also tried "return false" in both the onclick and OnClientClick events. No Luch. Can I use...
5
1930
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; printf("function call: %d time \n", i); i++; main();
0
7918
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7766
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5981
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5341
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
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 we have to send another system
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.