Hello,

I am hoping to get some code for the following problem

I need to verify that a user input the time in the following format:

cout<<"please enter the start time(hh:mm:ss): ";
cin>>stime;
//Verify user input time correctly

cout<<"please enter the end time(hh:mm:ss): ";
cin>>etime;
//Verify user input time correctly


**Then I convert the start time and end time to seconds and return elapsed seconds (dont need help on this part)**

So basically I am asking for code either using tokens or otherwise that could verify user input.
I am an old C/assembly programmer that doesnt know c++ very well, and I best understand from seeing code.
If there is a better way to collect time from user, that is also welcome.

Thanks a ton everyone who helps!!!

Daniel

>So basically I am asking for code either using tokens or otherwise that could verify user input
Hmm, you probably wouldn't like me to recommend Boost::Regex if you're not terribly comfortable with C++. Are you using C-style strings or the C++ string class to hold the input?

>I am an old C/assembly programmer that doesnt know c++ very well
That's convenient, because the libraries and techniques for dealing with dates and times hasn't changed from C to C++. You still use the old familiar time.h stuff (though the header is now called ctime).

1> I am trying to keep the code and file as small as possible, and you are right, I do not feel comfortable with using 3rd party regex libraries...

2> I want to to use C++ string classes...and this is the hardest part, because in C, this problem is fairly straightforward.

3>Is ctime much different than time.h? Like, does it still use the tm strcture? tm_hour, tm_min, tm_sec, etc?

**I'll be honest, I already wrote this program in C, and I need to write it in C++ as well, and send both sources to a future employer, and I am frustrated with how C++ handles strings, although the frustration is not so much with C++ as it is with my lack of knowledge**

I was thinking you could use tokens with c++ strings as in like

int HH;
int MM;
int SS;
cout<<"Enter time: ";
cin>>HH >>":">>MM>>":">>SS;
...and then...how would I verify that?

in c it would go something like this
while(scanf("%d:%d:%d", &hour, &min, &sec) !=3)
{
printf("Enter it again: ");
}

You feel me? I need something familiar at least to start with...

Thanks for the quick response by the way

>Is ctime much different than time.h?
I thought I made it clear that the only difference is the name of the header changed. In reality, all of the names are in the std namespace, but if you're doing using namespace std; as I suspect, you won't notice the difference.

>I need something familiar at least to start with...
Okay, how about this then:

string stime;

// Read the time

if ( sscanf ( stime.c_str(), "%d:%d:%d", &hour, &min, &sec ) != 3 ) {
  // Invalid format
}

Thanks for the quick reply and yeah that code looks quite familiar, and straight forward...

I am coding this in Linux, not in windows and am not using namespace std;

Daniel

>I am coding this in Linux
That doesn't make a difference as long as you stick to the standard.

>and am not using namespace std;
Okay, then basically for any old C functions you use (if you're including the c* header instead of the *.h header), you need to prefix the name with std:: or add a using directive:

#include <ctime>

using std::time;
using std::mktime;
// etc...
This article has been dead for over six months. Start a new discussion instead.