C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 10th, 2003, 05:50 PM
Neildadon Neildadon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 12 Neildadon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
calculating user time and elapsed time

Hi im a little bit stuck on this program im working on.

im creating a program which uses the data type Mytime the output should look something like

Enter start hh mm : 7 30

Enter finish hh mm : 7 30

Time elapsed from 07:30 until 07:30 is 00:00

2 functions have not yet been defined display1Time() & elapsed(). The program currently compiles in unix g++ i have filled the un defined function to get the program running. Basically i cant work out how to get the time displayed and the total time elapsed working. I think the time the user enters has to be converted to minutes. Can anyone help please ?

Code:
// Incomplete version
#include <iostream>
using namespace std;

struct Mytime
{
   int hours;
   int mins;
};

int timecmp(Mytime,Mytime);
// returns 0 if times are same
// returns >0 if first is later than second
// returns <0 if first is before second

int setTime(Mytime& t, int h, int m );
// if valid time sets hh and mm to h and m 
// and returns 0
// if invalid returns integer > 0 as error code
// error code +1 = underflow hours
// error code +2 = overflow hours
// error code +4 = underflow mins
// error code +8 = overflow mins

void display1Time(Mytime t);
// displays in form hh:mm

void elapsed(Mytime t1, Mytime t2, Mytime& duration);
// determines the time duration between t1 and t2
// t1 assumed to be earlier than t2

void main()
{
    Mytime now;
    Mytime then;
    Mytime howlong;
    
    int h,m;
    
    do // validate input
    {
        cout << "Enter start hh mm : ";
        cin >> h >> m ;
    } while (setTime(now,h,m));
    
    do
    {
        cout << "Enter finish hh mm : ";
        cin >> h >> m ;
    } while (setTime(then,h,m));
    
    elapsed(now,then,howlong);

    cout << "Time elapsed from ";
    display1Time(now);
    cout << " until ";
    display1Time(then);
    
    cout << " is ";
    display1Time(howlong);
    cout << endl;
}

int timecmp(Mytime t1,Mytime t2)
{
   if (t1.hours == t2.hours)
   {
      if (t1.mins == t2.mins)
      {
         return 0;
      }
      else // mins not same
      {
         if (t1.mins > t2.mins)
         {
            return 1; // greater than zero
         }
         else
         {
            return -1;
         }
      }
   }
   else // hours not same
   {
      if (t1.mins > t2.mins)
      {
         return 1;
      }
      else
      {
         return -1;
      }
   }
}

int setTime(Mytime& t, int h, int m )
{
    int errorCode = 0;
    if (h < 0) errorCode += 1;
    if (h > 23) errorCode += 2;
    if (m < 0) errorCode += 4;
    if (m > 59) errorCode += 8;
    if (errorCode==0){t.hours = h; t.mins = m;}
    return errorCode;
}

//////////// THE FOLLOWING FUNCTIONS ARE INCOMPLETE

void display1Time(Mytime t)
{
     cout << "00:00";
}


void elapsed(Mytime t1, Mytime t2, Mytime& duration)
{
    setTime(duration,0,0);
}

Reply With Quote
  #2  
Old February 10th, 2003, 06:08 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 31 m 48 sec
Reputation Power: 4080
For displaying the time, you can use setw to set the width of the displayed variables and setfill to leftpad the numbers with 0. Don't forget to #include iomanip on top, as these functions are declared there.

As for computing the time difference, the thing to do is convert the times from hours and minutes into minutes. Then convert the resulting difference back into hours and minutes. Please note that the following code is off the top of my head and is untested, but it should go something like this:
Code:
#include <iomanip>
...
...
void display1Time(Mytime t)
{
     cout << setw(2) << setfill('0') << x << ":" << setw(2) << setfill('0') << y;
}

void elapsed(Mytime t1, Mytime t2, Mytime& duration) {
   int mins1, mins2, difference;

   // Convert the times into minutes
   mins1 = t1.hours *60 + t1.mins;
   mins2 = t2.hours *60 + t2.mins;

   // Compute the difference
   difference = mins2 - mins1;

   // Convert the minutes back into hours and minutes
   duration.hours = difference / 60;
   duration.hours = difference % 60;
}

Hope this helps!

Reply With Quote
  #3  
Old February 10th, 2003, 08:02 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 9 sec
Reputation Power: 13
If you are trying to compute the length of some operation, the code below might work for you...

Code:
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <string>
#include <iostream>

const int MINUTE = 60;
const int HOUR   = MINUTE * 60;
const int DAY    = HOUR * 24;

string get_elapsed(int sec)
{
    int days    =0;
    int hours   =0;
    int minutes =0;
    int seconds =0;

    char* dstr = new char;
    char* et   = new char;

    if (days    = int(sec / DAY))    sec -= DAY * days;
    if (hours   = int(sec / HOUR))   sec -= HOUR * hours;
    if (minutes = int(sec / MINUTE)) sec -= MINUTE * minutes;

    seconds = sec;
    sprintf(et, "%02d:%02d:%02d", hours, minutes, seconds);
    string res(et);

    if (days) {
        sprintf(dstr, "%s Day%s, ", days, &"s"[days == 1]);
        strncat(dstr, et, strlen(et));
        res = string(dstr);
    }

    delete dstr;
    delete et;
    return res;
}

int main(void) {

    // cache start time
    time_t start, end;
    (void)time(&start);

    // do something...
    sleep(5);

    // get end time
    (void)time(&end);

    cout << "elapsed: " << get_elapsed(end - start) << endl;
    return 0;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > calculating user time and elapsed time

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap