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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yapy
    New Member
    • Aug 2010
    • 3

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

    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);
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    difftime returns the difference in seconds expresssed as a double. So use a double in your code:

    Code:
    if (difftime(timeA, timeB) > 60.0)
    {
    etc...

    Comment

    • Yapy
      New Member
      • Aug 2010
      • 3

      #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)

      }

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        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

        Comment

        • Yapy
          New Member
          • Aug 2010
          • 3

          #5
          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.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            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?

            Comment

            Working...