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
3,460 posts since Jan 2012
Reputation Points: 822
Solved Threads: 474
Skill Endorsements: 57
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
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
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
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
3,460 posts since Jan 2012
Reputation Points: 822
Solved Threads: 474
Skill Endorsements: 57