Solved

substring, date

Posted on 2004-10-27
266 Views
Last Modified: 2010-04-02
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
}

0
Question by:justinY
    9 Comments
     
    LVL 3

    Expert Comment

    by: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;
    0
     
    LVL 3

    Expert Comment

    by:HendrikTYR
    Have you tried:

    if ( ss1 )
    {
         cout << mm << "16" << yyyy << endl;
    }
    else
         cout << mm << dd << yyyy << endl;
    }
    0
     
    LVL 3

    Expert Comment

    by:HendrikTYR
    Sorry, posted same time
    0
     
    LVL 39

    Accepted Solution

    by:
    That is wrong as well:

    >>>> ss1 = strstr(row_elem[1],"NFLFAN");

    change to

          if ( row_elem[1].find("NFLFAN") != string::npos)
         {
            cout << mm << "16" << yyyy << endl;
         }
         else
             cout << mm << dd << yyyy << endl;
        }

    Regards, Alex

    0
     
    LVL 3

    Expert Comment

    by:HendrikTYR
    ... or:

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

    Author Comment

    by:justinY
    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 ?
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    >> && (aoti(dd) = 15)

    suppose you want

         && (dd == 15)

    instead.

    Regards, Alex
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    Correction: dd is a string, so you need

    && (dd == "15")


    Regards, Alex

    0
     

    Author Comment

    by:justinY
    Hi Alex,

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

    Thanks
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now