Link to home
Start Free TrialLog in
Avatar of justinY
justinY

asked on

substring, date

Hi experts,
I have 2 columns: row_elem[1], and row_elem[10]
for example.
row_elem[1] ,                                 row_elem[10]
aaaaa NFLFAN sssss,                          01022004      date format mmddyyyy
aaaaa NFLFAN sssss,                          01152004
...                                                     ...
I check row_elem[1] first, if substring has NFLFAN, and if dd=15, then print 01162004 ( adding 1 to day, that is 16, always 16 )
otherwise print 01022004

My code is like this, but its not working. please help
...
main(int arc, char *arv[])
{
    ifstream fin("test.csv");
    ofstream fout("fout.txt");
    string row_read;
    while( getline(fin, row_read) )
    {

        vector<string> row_elem;
        GetFields(row_read, row_elem, ',');
....
string mm;
string dd;
string yyyy;

mm = row_elem[10].substr(0,2);
dd = row_elem[10].substr(2,2);
yyyy = row_elem[10].substr(4,4);
//cout << mm.c_str() << endl;

char pch[50];
char *ss1;
ss1 = strstr(row_elem[1],"NFLFAN");
if ( ss1 )
{
     cout << mm16yyyy <<endl;
}
else
     cout << mmddyyyy << endl;
}   // end of while
}

Avatar of Indrawati
Indrawati

I don't really understand your question, but I found a mistake in your code:

if ( ss1 )
{
     cout << mm16yyyy <<endl;
}
else
     cout << mmddyyyy << endl;

Should be

if ( ss1 )
{
     cout << mm << 16 << yyyy <<endl;
}
else
     cout << mmddyyyy << endl;
Have you tried:

if ( ss1 )
{
     cout << mm << "16" << yyyy << endl;
}
else
     cout << mm << dd << yyyy << endl;
}
Sorry, posted same time
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
... or:

char *ss1;
ss1 = strstr(row_elem[1].c_str(), "NFLFAN");
if ( ss1 )
{
     cout << mm << 16 << yyyy <<endl;
}
else
     cout << mm << dd << yyyy << endl;
Avatar of justinY

ASKER

Thanks guys,
I forgot this if dd = 15, then cout <<mm<<16<<yyyy;
so my code is

stringstream oss1;
stringstream oss2;

if ( ( row_elem[1].find("NFLFAN") != string::npos) && (aoti(dd) = 15))
     {
        oss1 << setw(8) << mm << "16" << yyyy;
     }
     else
         oss2 << setw(8) << mm << dd << yyyy;
    }

      fout << setw(8) << oss1.str().c_str() << endl;
but I have compiling error, whats wrong ?
>> && (aoti(dd) = 15)

suppose you want

     && (dd == 15)

instead.

Regards, Alex
Correction: dd is a string, so you need

&& (dd == "15")


Regards, Alex

Avatar of justinY

ASKER

Hi Alex,

I fixed it by doing this
 && (aoti(dd.c_str()) == 15)

Thanks