# Convert MM/DD/YY hh:mm stream to integer?

• November 7th, 2012, 09:27 AM
AwkwardRaven
Convert MM/DD/YY hh:mm stream to integer?
Hello,

I'm trying to read in two instances of date and time (from user input) into a string value (MM/DD/YY hh:mm).
I need to be able to calculate the difference between the two in order to return how much time has elapsed from the first date/time to the second date/time.

What would be the most efficient way to go about obtaining said results?

I was thinking I need to convert each year, month, day, hour, and minutes into its own integer variable, after researching immensely online I'm still not sure how to convert from a string of characters to an integer.

I just learned starting learning C++ couple months ago, I would appreciate any advice. I hope to learn.
• November 7th, 2012, 09:39 AM
GCDEF
Re: Convert MM/DD/YY hh:mm stream to integer?
• November 7th, 2012, 10:03 AM
AwkwardRaven
Re: Convert MM/DD/YY hh:mm stream to integer?
thank you for replying but that's not what I would hope to do.

This is what I have so far:
Code:

```#include <iostream> #include <string> #include <iomanip> #include <cmath> #include <cstring> //void calculateParkingHours(string, string); //void calculateFee(float, float); using namespace std; int main () { int enterY, enterM, enterD, enterH, enterMi, exitY, exitM, exitD, exitH, exitMi; string enteringDate, enteringTime; cout << "Please enter the date and time the car is entering the parking garage" << '\n'         <<  "in the following format: MM/DD/YY hh:mm" << endl; cin >> enteringDate >> enteringTime; enterY = atoi ( enteringDate.substr( 6, 2 )); enterM = atoi ( enteringDate.substr( 0, 2 )); enterD = atoi ( enteringDate.substr( 3, 2 )); enterH = atoi ( enteringTime.substr( 9, 2 )); enterMi = atoi ( enteringTime.substr( 12, 2 )); //cout << "Please enter the date and time the car is exiting the parking garage in the following format:" << '\n' //        << "MM/DD/YY hh:mm" << endl; }```
I'm having problems with using "atoi" is there a more efficient way to convert different parts of a string to integers?
• November 7th, 2012, 10:13 AM
GCDEF
Re: Convert MM/DD/YY hh:mm stream to integer?
I think using the built in functions would be more reliable. However, you need to use the c_str() member of string to use atoi, like this.

enterY = atoi ( enteringDate.substr( 6, 2 ).c_str());
• November 7th, 2012, 10:44 AM
AwkwardRaven
Re: Convert MM/DD/YY hh:mm stream to integer?
Why do I need .c_str() also if I use difftime can I define my own times?
• November 7th, 2012, 11:02 AM
GCDEF
Re: Convert MM/DD/YY hh:mm stream to integer?
Because atoi takes a pointer to a C style null terminated string as an argument, and that's what c_str returns.

difftime would be pretty useless if you couldn't specify the times you were interested in.
• November 7th, 2012, 02:12 PM
AwkwardRaven
Re: Convert MM/DD/YY hh:mm stream to integer?
then how would I specify the time for difftime from a string formatted in MM/DD/YY hh:mm?
• November 7th, 2012, 02:23 PM
GCDEF
Re: Convert MM/DD/YY hh:mm stream to integer?
Quote:

Originally Posted by AwkwardRaven
then how would I specify the time for difftime from a string formatted in MM/DD/YY hh:mm?

You'd have to convert it to a time_t. You can use mktime for that.
http://www.cplusplus.com/reference/c.../ctime/mktime/
• November 10th, 2012, 04:59 PM
AwkwardRaven
Re: Convert MM/DD/YY hh:mm stream to integer?
so if I extrapolated separate integers from a stream MM/DD/YY hh:mm what would be the exact syntax to use to convert that into a time format C++ can recognize and contrasts between two different times?

for example: if the user entered 03/26/12 20:08 I would have:
03 stored in a integer variable enterM
26 stored in a integer variable enterD
12 stored in a integer variable enterY
20 stored in a integer variable enterH
08 stored in a integer variable enterMin
• November 10th, 2012, 09:22 PM
Paul McKenzie
Re: Convert MM/DD/YY hh:mm stream to integer?
Quote:

Originally Posted by AwkwardRaven
so if I extrapolated separate integers from a stream MM/DD/YY hh:mm what would be the exact syntax to use to convert that into a time format C++ can recognize and contrasts between two different times?

Please write small test functions to familiarize yourself with how time functions work. That is how any programmer goes about learning how to use functions that are new to them. That is much easier than to have one of us spend time giving you "exact" syntax.

Here are the functions:

http://www.cplusplus.com/reference/clibrary/ctime/tm/
http://www.cplusplus.com/reference/c.../ctime/mktime/
http://www.cplusplus.com/reference/c...time/difftime/

Regards,

Paul McKenzie
• November 12th, 2012, 12:50 PM
AwkwardRaven
Re: Convert MM/DD/YY hh:mm stream to integer?
so
Code:

```void calculateParkingHours () { enteringMins += ( enterY * 365 * 29 * 60 ); enteringMins += ( enterM * 12 * 29 * 60 ); enteringMins += ( enterD * 29 * 60 ); enteringMins += ( enterH * 60 ); enteringMins += ( enterMi ); exitingMins += ( exitY * 365 * 29 * 60 ); exitingMins += ( exitM * 12 * 29 * 60 ); exitingMins += ( exitD * 29 * 60 ); exitingMins += ( exitH * 60 ); exitingMins += ( exitMi ); struct tm * enterTime; struct tm * exitTime; enterTime-> tm_min = enterMi; enterTime-> tm_hour = enterMi; enterTime-> tm_mday = enterD; enterTime-> tm_mon = enterM; enterTime-> tm_year = enterY; exitTime-> tm_min = exitMi; exitTime-> tm_hour = exitH; exitTime-> tm_mday = exitD; exitTime-> tm_mon = exitM; exitTime-> tm_year = exitY; mktime (enterTime); mktime (exitTime); feeTime = difftime(enterTime,exitTime); //feeTime = ((enteringMins - exitingMins) * -1); }```