OK, I've acheived HULK SMASH levels of frsutration with this one. I simply want to parse a date in the form of ##/##/####

The code below works peicemeal, ie each part SEEMS to work, but if I cout at the bottom the day is dissapearing or spitting out erroneous crap.

#include <cctype>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>

#include <cctype>
#include <string>
#include <ctime>

using namespace std;

void dateParse(string);
int main()
{

    string dte1 = "2/5/2014";
    string dte2 = "2/15/2014";
    string dte3 = "12/5/2014";
    string dte4 = "12/15/2014";

    string dts;
    dateParse(dte1);
    dateParse(dte2);
    dateParse(dte3);
    dateParse(dte4);

}

void dateParse(string date)
{
    char day[2] = "";
    char month[2] = "";
    char year[5] = "";
    char buffer[80] = "";

   year = date.substr(date.find("/")+1,date.length());
   strcpy(buffer, year.c_str());


    strcpy(buffer, date.c_str());
    strcpy(day, strtok(buffer,"/"));

    strcpy(buffer, date.c_str());
    strcpy(year, strrchr(date.c_str(), '/'));
    for(int i = 0; i < 5; i++)
    {
        year[i] = year[i + 1];

    }



    strcpy(buffer, date.c_str());

    strcpy(buffer, ((strrchr(date.c_str(),'/')),(strchr(date.c_str(), '/') + 1)));
    strcpy(month, strtok(buffer,"/"));



    cout << "DAY: " << day << endl;
    cout << "MONTH: " << month << endl;
    cout << "YEAR: " << year << endl;
    cout << "--------------------------------" << endl;

}

here's my main:

`

#include <cctype>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>

#include <cctype>
#include <string>
#include <ctime>

using namespace std;

void dateParse(string);
int main()
{

    string dte1 = "2/5/2014";
    string dte2 = "2/15/2014";
    string dte3 = "12/5/2014";
    string dte4 = "12/15/2014";

    string dts;
    dateParse(dte1);
    dateParse(dte2);
    dateParse(dte3);
    dateParse(dte4);

}

This is the output
DAY: 2
MONTH:

YEAR: 2014

DAY:
MONTH:

YEAR: 2014

DAY: 12♣
MONTH:

YEAR: 2014

DAY:
MONTH:

YEAR: 2014

Process returned 0 (0x0) execution time : 0.063 s
Press any key to continue.

The annoying this is if I cout the day month and year right after they're parsed it looks like it's working. Can someone help me parse this stupid date? I inventing new and horrible swear words here.

Recommended Answers

All 12 Replies

Your function dateparse creates a char array named year, and then you set that to a string, somehow, and then you call c_str on it, even though it began life as a char array.

Does this code even compile?

Yes, it compiles the out put is in the original post.

I'm feeding dateparse a string, and using c_str() on that, not the year. The date is the string, and the month, day and year are all char arrays. Hope that helps.

when I cout day, month and year the output looks correct, but somewhere things get screwy when I print them all after all parsing is done, like this:

void dateParse(string date)
{
    char day[2] = "";
    char month[2] = "";
    char year[5] = "";
    char buffer[80] = "";

    strcpy(buffer, date.c_str());
    strcpy(day, strtok(buffer,"/"));

    cout << "DAY: " << day << endl;



    strcpy(buffer, date.c_str());

    strcpy(buffer, ((strrchr(date.c_str(),'/')),(strchr(date.c_str(), '/') + 1)));
    strcpy(month, strtok(buffer,"/"));
 cout << "MONTH: " << month << endl;
    strcpy(buffer, date.c_str());
    strcpy(year, strrchr(date.c_str(), '/'));
    for(int i = 0; i < 5; i++)
    {
        year[i] = year[i + 1];

    }
cout << "YEAR: " << year << endl;



    cout << "--------------------------------" << endl;

}

output:
DAY: 2
MONTH: 5

YEAR: 2014

DAY: 2
MONTH: 15

YEAR: 2014

DAY: 12
MONTH: 5

YEAR: 2014

DAY: 12
MONTH: 15

YEAR: 2014

Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.

It doesn't compile here:
http://ideone.com/23gUpy

I'm feeding dateparse a string, and using c_str() on that, not the year.

year.c_str() Sure looks like you're calling c_str() on year.

what line is that? I can't find it in my code. If it didn't compile I wouldn't be able to post output.

Here's your code, from the first post. I see that a later post contains different code. Do we take it that the code in the first post was actually not the code you meant?

#include <cctype>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <string>
#include <ctime>
using namespace std;
void dateParse(string);
int main()
{
    string dte1 = "2/5/2014";
    string dte2 = "2/15/2014";
    string dte3 = "12/5/2014";
    string dte4 = "12/15/2014";
    string dts;
    dateParse(dte1);
    dateParse(dte2);
    dateParse(dte3);
    dateParse(dte4);
}
void dateParse(string date)
{
    char day[2] = "";
    char month[2] = "";
    char year[5] = "";
    char buffer[80] = "";
   year = date.substr(date.find("/")+1,date.length());
   strcpy(buffer, year.c_str());
    strcpy(buffer, date.c_str());
    strcpy(day, strtok(buffer,"/"));
    strcpy(buffer, date.c_str());
    strcpy(year, strrchr(date.c_str(), '/'));
    for(int i = 0; i < 5; i++)
    {
        year[i] = year[i + 1];
    }
    strcpy(buffer, date.c_str());
    strcpy(buffer, ((strrchr(date.c_str(),'/')),(strchr(date.c_str(), '/') + 1)));
    strcpy(month, strtok(buffer,"/"));
    cout << "DAY: " << day << endl;
    cout << "MONTH: " << month << endl;
    cout << "YEAR: " << year << endl;
    cout << "--------------------------------" << endl;
}

what line is that? I can't find it in my code. If it didn't compile I wouldn't be able to post output.

I'm not sure what that compiler is having problems with. strcpy works with cstrings, that's why I have to convert the string object date to a char array using the .c_str[] -

strcpy works with cstrings, that's why I have to convert the string object date to a char array using the .c_str[] -

Alternatively, you could save yourself the bother of char arrays by working with C++ strings instead. strcpy works with c-strings, but = works with C++ strings.

I'm not sure what that compiler is having problems with.

year = date.substr(date.find("/")+1,date.length()); That line. The thing on the right is a C++ string. The thing on the left is a char array. The assignment operator = does not know how to assign a C++ string to a char array. But that's not relevant anyway; the code you posted in your first post isn't the code you're actually working with.

strcpy(buffer, ((strrchr(date.c_str(),'/')),(strchr(date.c_str(), '/') + 1))); This makes no sense. It is functionally identical to:
strcpy(buffer, ((strchr(date.c_str(), '/') + 1)));

Oh sonofa - Dude, I am so sorry. Sigh - that shouldn't be in there. it was a chunk of commented out code had deleted when I pasted into the posting area. My total bad.

#include <cctype>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>

#include <cctype>

#include <ctime>

using namespace std;

void dateParse(string);
int main()
{

    string dte1 = "2/5/2014";
    string dte2 = "2/15/2014";
    string dte3 = "12/5/2014";
    string dte4 = "12/15/2014";

    string dts;
    dateParse(dte1);
    dateParse(dte2);
    dateParse(dte3);
    dateParse(dte4);

}

void dateParse(string date)
{
    char day[2] = "";
    char month[2] = "";
    char year[5] = "";
    char buffer[80] = "";

cout << "\n----Here it looks like it works ---" << endl;
    strcpy(buffer, date.c_str());
    strcpy(day, strtok(buffer,"/"));
cout << "DAY: " << day << endl;

    strcpy(buffer, date.c_str());

    strcpy(buffer, ((strrchr(date.c_str(),'/')),(strchr(date.c_str(), '/') + 1)));
    strcpy(month, strtok(buffer,"/"));
    cout << "MONTH: " << month << endl;

    strcpy(buffer, date.c_str());

    strcpy(year, strrchr(date.c_str(), '/'));
    for(int i = 0; i < 5; i++)
    {
        year[i] = year[i + 1];

    }
    cout << "YEAR: " << year << endl;
 cout << "\n---Here it's messing up --------" << endl;

    cout << "DAY: " << day << endl;
    cout << "MONTH: " << month << endl;
    cout << "YEAR: " << year << endl;



    cout << "-------- End Function Call ------" << endl;

}

I am perpetually thrashing around with this posting interface. It seems to have a very strange memory. Sometimes I'll try to edit a post, and the post I get shown to edit was actually several posts previous.

strcpy(buffer, ((strrchr(date.c_str(),'/')),(strchr(date.c_str(), '/') + 1)));

What I'm trying to do here is extract the month from between the two back slashes, the strchr finds the first instance starting from the begining of the string and the strrchr starts it's search at the end. What's weird is the code works (or at least seems to) when I cout after every "parse" but when I cout at the end it goes nuts.

It appears that you may be going out of bounds on your char arrays which may be resulting in a stack corruption.

Try the following.

Change these three variables...

char day[2] = "";
char month[2] = "";
char year[5] = "";

to....

char day[3] = "";
char month[3] = "";
char year[6] = "";

Note the one byte increase in size for all three variables.

This parsing could be a breeze if you use C++ string and stringstream to parse ... using :

istringstream iss( cppStrToParse ); // construct iss

getline( iss, cppStrMth, '/' ); // to get month and then

getline( iss, cppStrDay, '/' ); // to get day and then

getline( iss, cppStrYr ); // to get the year at the end

If you need to use C string types ... I have a readWord.h with readWord that will parse a C string line into (dynamic C string) words ... with your choice of delimiters.

/* readWord.h */ /* this ver 2013-07-11 */

/*  http://developers-heaven.net/forum/index.php/topic,46.0.html  */

/*
    Safe string data entry from file or keyboard ...
    BUT ... free new memory when done with it!
    Don't forget that C strings are pointers to a block of char,
    with a '\0' char at the terminal end ... Call like this:

    char* word = readWord(stdin, 31, "\n\t ,.;:", &lastChr);//note delimiter str

    or ...

    while((word = readWord(FILEp, startChunkSize, delimiterStr, &lastCharRead)))
    {
        // do stuff ...
    }

    Note 1: select your initial word chunk size to minimize calls to realloc
    Note 2: address of last char passed in/out so can flush stdin or file line
*/
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.