Hey, I'm working on another program for class which is to determine the day of the year given the date the user inputs.

The date will be input in the xx-xx-xxxx format. Is there a way I can cin Mon, skip char, cin Day, skip char, cin Year ?

How would you suggest initializing the variables given this type of input?

Just cin >> 3 different variables. One being the Month, then the Day, then the Year.

Then cout << in the format you need it to be displayed.

>Is there a way I can cin Mon, skip char, cin Day, skip char, cin Year ?
Too complex for cin , way easier with getline() . Just use it to grab the entire line, and then parse out the numbers in between the tokens (perhaps use strtok ?)

>Is there a way I can cin Mon, skip char, cin Day, skip char, cin Year ?
Too complex for cin , way easier with getline() . Just use it to grab the entire line, and then parse out the numbers in between the tokens (perhaps use strtok ?)

this sounds great, how do you use strtok?

my current string code:

string line ;
    getline ( cin, line ) ;
    int length = line.length () ;
    if (length != 10)
    {
        cout << "--  !! Invalid Date !!  ---  !! Terminating Program !!  --" << endl ;
        return 1 ;
    }

>this sounds great, how do you use strtok?
Actually I take that back, it's probably a better idea to use C++ functions than to mess with C and C++ strings.

Take a look at find_first_of member function of std::string, it returns the location of the first character of found in a set of symbols you give it (in this case, they hyphen).

Then to separate the first part of the string use substr. Next, use find_first_of on the remaining part of the string, and then use substr ... until everything's parsed out.

>Is there a way I can cin Mon, skip char, cin Day, skip char, cin Year ?
Too complex for cin , way easier with getline() . Just use it to grab the entire line, and then parse out the numbers in between the tokens (perhaps use strtok ?)

Not really.

It's not very robust, but

int mm,dd,yy;
  char x1,x2;

  cin >> mm >> x1 >> dd >> x2 >> yy;
  cout <<  mm << ' ' << dd << ' ' << yy << endl;

works... ;)

This article has been dead for over six months. Start a new discussion instead.