I'm parsing a date into three ints, then manipulating them. I then want to convert them back into strings so I can concactenate them with the slashes so I can search an array of dates saved in string format. I looked around and this code seems like it should work, but everytime run it it crashes.

string sMonth, sYear, sDay = "";

// Everything in this code is initialized and works well until it hits this itoa

    char *Mptr;
    sMonth = itoa(lastM, Mptr, 10);
    sMonth = Mptr;

    char *Dptr;
    Dptr = itoa(lastD, Dptr, 10);
    sDay = Dptr;

    char *Yptr;
    Yptr = itoa(lastY, Yptr, 10);
    sYear = Yptr;

   lastYear = sDay + "/" + sMonth + "/" + sYear;

    cout << endl << lastYear; //lastM << "/" << lastD << "/" << lastY << endl;

Recommended Answers

All 2 Replies

it crashes because the second parameter to itoa() must be a characte array, not just a pointer. See the example here.

THANK YOU! I actually tried it correctly at one point (at least I think I did : S ) But messing with cstrings, ints, strings et all while parsing can get pretty sticky pretty quickly, if you're not careful. When I get frustrated I'm known to start madly slapping pointers on everything (shameful I know). Thanks again, converted to a char[] slapped it in and works like a charm - posting the code on the off chance it may help someone:

/ Reassample the date as a searchable string
    string sMonth, sYear, sDay = "";

    char cd[3];
    itoa(lastD, cd, 10);
    sDay = cd;

    char cm[3];
    itoa(lastM, cm, 10);
    sMonth = cm;

    char cy[5];
    itoa(lastY, cy, 10);
    sYear = cy;

    cout << sMonth << "/" << sDay << "/ " << sYear << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.