ga******@gmail.com wrote:
I have to hours like this 20:30:15 (HH:MM:SS) loeaded in two
std:string, and I wan't to compare them. I looked at ctime but I didn't
found, first, somethig to convert from string to time and then some
function to compare them.
How can I do this?
There are a couple of ways you can do this. One would be to wrap the
string in a "time" class that understands the format of times. If this
type of data was used extensively in a program, I would probably go this
rout. Another would be to implement a times_equal and times_less that
takes two strings as parameters. I'm going to assume the latter in this
case.
Take the code below and add to it where it says "insert code here" until
it will compile without errors or warnings and when run, will display
"Works so far!" on the screen.
If you have any problems, post what you have so far and I'll help you
through them. Once you get it working, post what you did and I'll help
you with the next step.
----- begin code -----
#include <cassert>
#include <iostream>
#include <string>
using namespace std;
bool times_equal( const string& lhs, const string& rhs ) {
// insert code here
}
bool times_less( const string& lhs, const string& rhs ) {
// insert code here
}
int main() {
string a = "20:30:15";
assert( times_equal( a, a ) );
cout << "Works so far!\n";
};
--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.