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!

Recommended Answers

All 9 Replies

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().

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!

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?

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!

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!

your missing the starting bracket for main, next

if(timeout<17:00)

you can't use a time format for comparison

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?

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

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";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.