Question

I'm working on a project in school and our assignment goes like this.

There is a input text file given with the following information

08:30 Jane 10:15 

08:35 Eric 11:20

08:45 Austin  11:45

09:20 Mclaren 12:50

This is the entry time, name, and leave time for four employees who work at some company.

What I'm supposed to do is find the person who stayed inside the longest and display their name.

(All 3 informations are type string.)

For this I have to subtract the entry time from the leave time for a person, make that into an integer value, do this for all other persons, and then compare all of these integers and find out which is the biggest.

The problem is that I have no idea how to convert (for example) 08:45 to an integer. I know C++ can lexicographically compare the two and would be able to tell that 10:15 is greater than 9:45 but unfortunately it cannot subtract them. Is there a method that would do that in C++?

Or would I have to do some complicated shit and make some sort of delimiter (the ":") and then take the first part of the string like 08, and take that as the hour, multiply it by 60 (after 08 is converted to the int 8 somehow) to obtain minutes, then take the value after delimiter and add it to that (after the minutes were converted to int), to obtain the total minutes? Sounds very complicated to me, also my knowledge in C++ is limited but I would guess this way of doing things would need a huge switch table which would convert '08' to the integer 8 and so on and so forth all the way from 00 to 60 (60 cases).

There must be a far easier way. Any help would be appreciated.

EDIT: To clarify, I already know how to isolate the times out of the text file and put them into seperate strings. I need help with the conversion itself. How to convert the time in that string format to a useable integer value.

EDIT2: Solved with substring and atoi. Thanks for help.

Était-ce utile?

La solution

Well if you know that the input strings will always be in that format you could just take the first five characters and the last five characters of each string to isolate the two times.

(Hint use the substring function)

Now that you have the two times as separate strings you can parse the integer values out of them.

(Hint use sscanf)

This will leave you with two hour values and two minute values. I'm sure you can work out the maths to see who stayed longer.

Autres conseils

The library <string> has a lot of useful tools to help with this: http://www.cplusplus.com/reference/string

Getting the time itself isn't too bad. Getting the first time, you can do something to the effect of:

string line;

if(infile.is_open()){
            while(getline(infile, line)){
                    line.resize(5);
                    // Be careful though, this'll discard the remaining information
                    // Use the built-in string iterator to 
                    // traverse the string and manage the data

Just adapt that and get the second time first, name second, and first time last. There is probably a more efficient method though.

In addition to other answers, your title says you want to convert a time format into an integer.

Many libraries use the smallest time unit and convert all the others into that unit.

For example, let's use seconds as the base.
Let D be the number of days.
Let H be the number of hours.
Let M be the number of minutes.
Let S be the number of seconds.

Integral value, in seconds = (D * 24 hours / day) * (60 minutes per hour) * (60 minutes / second)
+ H * 3600
+ M * 60 + S

Another popular time unit is milliseconds.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top