We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,503 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Time difference in hrs & minutes?

Good day!

I have this code that is assume to get the difference in time. Is it possible to get the difference of from a string time variable? Or, whats the best way to implement this?

#include<iostream>
using namespace std;
int main()
string timediff, timein, timeout;
cout<<"Enter player time-in between 08:00 to 17:00: ";
cin>>timein;
cout<<"Enter player time-out between 08:00 to 17:00: ";
cin>>timeout;
    if(timeout<"17:00")
     {
       cout<<"This player is playing in undertime"<<endl;
     }
timediff=timeout-timein;
cout<<"The time that the players played is: "<<timediff<<endl;
system("pause");
return 0;
}

Any help is gladly appreciated! Thank you!

4
Contributors
9
Replies
1 Day
Discussion Span
9 Months Ago
Last Updated
10
Views
Question
Answered
blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

Take a look at the ctime header. It includes types and functions for manipulating dates and times. With minimal effort you can parse your time string into a tm structure, convert those into time_t objects with mktime(), and finally get the difference with difftime().

deceptikon
Challenge Accepted
Administrator
3,460 posts since Jan 2012
Reputation Points: 822
Solved Threads: 474
Skill Endorsements: 57

Thank you deceptikon.
Ive google ctime but I cant find a simple example to use difftime();
Should it be something like this?

    #include<iostream>
    #include<time>
    using namespace std;
    int main()
    time_t timediff, timein, timeout;
    cout<<"Enter player time-in between 08:00 to 17:00: ";
    cin>>timein;
    cout<<"Enter player time-out between 08:00 to 17:00: ";
    cin>>timeout;
    if(timeout<17:00)
    {
    cout<<"This player is playing in undertime"<<endl;
    }
    timediff=difftime(timeout,timein);
    cout<<"The time that the players played is: "<<timediff<<endl;
    system("pause");
    return 0;
    }

I havent tested the above code? Any correction?

Thank you!

blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

I havent tested the above code?

Why not? How do you know what needs to change if you haven't bothered to try it?

Any correction?

Too much to go into. Based on what you've posted you need to go way back to the basics. Like
1) the infamous Hello World program for basic program layout
2) basic input into integer variables
3) basic comparison (if) statements
4) not understanding what time_t variables are

Did you do a search for info at all?

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

Thank you WaltP!

"I havent tested the above code?"
Well, for this reason, I cant test it co'z im at work. Ive just re-arranged my post so that others can make comment or the best way to do it!

"Any correction?"
I need somebody whos knowledge is better that me to correct if Im mistaken, thus if they are willing to hep & contribute..

We are not talking about basics here! If somebody has a solution to the post, better show it.

I will post changes if this really works!

Thank you!

blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

Ive tested the code and I think it is working except that the all cin>> for time-in and time-out was skip.Any Idea?

My code so far:

    #include<iostream>
    #include<ctime>
    using namespace std;
    int main()
    time_t timein, timeout;
    float timediff;
    cout<<"Enter player time-in between 08:00 to 17:00: ";
    cin>>timein;
    cout<<"Enter player time-out between 08:00 to 17:00: ";
    cin>>timeout;
    if(timeout<17:00)
    {
    cout<<"This player is playing in undertime"<<endl;
    }
    timediff=difftime(timeout,timein);
    cout<<"The time that the players played is: "<<timediff<<endl;
    system("pause");
    return 0;
    }

Thank you!
blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

your missing the starting bracket for main, next

if(timeout<17:00)

you can't use a time format for comparison

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

thank you zeroliken!

If I cant use time for comparision, how to compare if the player is undertime? meaning his stop playing before 17:00?

blocker
Posting Whiz
308 posts since Jan 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 9 Months Ago by WaltP, zeroliken and deceptikon

If I cant use time for comparision, how to compare if the player is undertime?

convert timein and timeout variable to either in minutes or hours (as long as it has a single value) then compare

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

Here's a very simple example of what I was talking about:

#include <ctime>
#include <iostream>
#include <sstream>
#include <string>
#include <tuple>

using namespace std;

time_t make_time(tuple<int, int> new_time)
{
    time_t now = time(0);    // Find the current time
    tm *info = gmtime(&now); // Convert it to calendar info

    // Update the minute and second
    info->tm_min = get<0>(new_time);
    info->tm_sec = get<1>(new_time);

    // Convert back into a time_t (fixing errors in the process)
    return mktime(info);
}

tuple<int, int> parse_time(const string& s)
{
    istringstream iss(s);
    int min, sec;

    // Naive parsing, assumes the string is valid
    iss >> min;
    iss.get();
    iss >> sec;

    return make_tuple(min, sec);
}

int main()
{
    time_t time_in, time_out;
    string s;

    cout << "Enter player time-in between 08:00 and 17:00: ";
    cin >> s;
    time_in = make_time(parse_time(s));

    cout << "Enter player time-out between 08:00 and 17:00: ";
    cin >> s;
    time_out = make_time(parse_time(s));

    cout << "Play time was " << difftime(time_out, time_in) << " seconds\n";
}
deceptikon
Challenge Accepted
Administrator
3,460 posts since Jan 2012
Reputation Points: 822
Solved Threads: 474
Skill Endorsements: 57

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3226 seconds using 2.82MB