Get mm dd yyyy from user input

c++, string

Solution

The problem is that your `char` buffers aren't big enough to hold the null terminator, so you're writing the null terminator past the end of the buffer. In the case of `cMonth` and `cDay`, this apparently causes them to run into each other because they are stored adjacently on the stack (don't rely on that behaviour!)

You need to make them at least one byte longer, i.e. 3, 3, and 5 bytes long. Be aware that your code is vulnerable to a buffer overflow; you may want to adjust your `.get` parameters so that they can't possibly overflow the buffers, or make the buffers longer.

Problem

This is a really simple program I have to make but I'm going blank on how to do it. Basically the user inputs the date in the from mm/dd/yyyy and all I have to do is separate the values and output them on separate lines. This is what i have so far: ``` int main () { char cMonth[2]; char cDay [2]; char cYear[4]; cout << "Enter a date in the form mm/dd/yyy: " ; cin.get(cMonth,3,'/'); cin.ignore(2,'/'); cin.get(cDay, 4, '/'); cin.ignore(2,'/'); cin.get(cYear, 5); cout << cMonth << endl << cDay << endl << cYear << endl; return 0; } ``` My program compiles but when it runs it gives wrong output, for example if i put `04/13/2013` the output will be: ``` 0413 13 2013 ```

Original source