Convert MM/DD/YY hh:mm stream to integer?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

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

  1. #1
    Join Date
    Nov 2012
    Posts
    7

    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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,043

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


  3. #3
    Join Date
    Nov 2012
    Posts
    7

    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?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,043

    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());
    Last edited by GCDEF; November 7th, 2012 at 10:15 AM.

  5. #5
    Join Date
    Nov 2012
    Posts
    7

    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?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,043

    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.

  7. #7
    Join Date
    Nov 2012
    Posts
    7

    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?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,043

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

    Quote Originally Posted by AwkwardRaven View Post
    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/

  9. #9
    Join Date
    Nov 2012
    Posts
    7

    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

  10. #10
    Join Date
    Apr 1999
    Posts
    27,421

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

    Quote Originally Posted by AwkwardRaven View Post
    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
    Last edited by Paul McKenzie; November 10th, 2012 at 09:25 PM.

  11. #11
    Join Date
    Nov 2012
    Posts
    7

    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);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center