Yes, this is for an assignment. I do not mind working to get an answer and I do not want the exact answer! :) This is my first C++ class. I've come into this class with prior knowledge of VBA, MySql, CSS, and HTML.
We are required to write a program with several different functions. One of them is required to receive the date input in a "MM/DD/YYYY"
format.
While that in of itself is easy enough; as a beginner I would just put
cin >> month >> day >> year;
And insert the "/" afterwards when displaying back to the user.
However, I believe our professor would like the user to input the date by exactly typing "12/5/2013", or any other date.
Per his instructions:
The
'/'
can be read by cin. So read the'/'
character and ignore it. Set day to equal the 3rd input, month to the first input, and year to the fifth input. Discard the 2nd and 4th input.
^ That is where I am thrown off course.
So far we have only experienced cin when the user hits enter after each input. So I don't know if he wants the user to hit enter after 12, then again after '/', then after 5, after '/', and lastly after '2013' (using the prior example of 12/5/2013 for December 5th, 2013).
Does anyone more experienced have any possible insight as to what I should be doing? We have only been taught how to use "cin" to receive inputs (so we know no other methods for receiving input), and I have no idea how to go about "ignoring a character" when entered as a string such as '12/5/2013' exactly.
I would greatly appreciate any help with this!
edit: I have looked for answers on here but all of the ones that I have come across are beyond the scope of what we have been taught and are therefore not allowed in the assignment. While I can go about understanding the logic of more advance coding easily enough, I am frustrated at my lack of ability to solve these simpler problems with any amount of ease. Hence my posting on here. I have spent several hours scanning our textbook for possible answers or clues to 'ignoring' characters in an input string but have come up short.
follow this pseudo code:
and voilà you have your day, month and year from input.
It's actually pretty easy! The thing is: you can input more than just one thing. That means, if you write
int d; std::cin >> d;
, it's perfectly fine to input30/06/2014
. The value ofd
becomes30
and the rest of the input is not yet read. If you write the nextstd::cin
statement, the content that is available is/06/2014
.You then need to consume the
/
, read the month, consume again and finally read the year.If you have the guarantee that the input format will be correct, it's OK to just write
Alternatively, If you're not comfortable with using
std::cin.get()
, it's just as good as reading a character:Here are some demos:
std::cin.get()