Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompt for and receive a date "MM/DD/YYYY" using CIN, ignoring the "/" characters? (in C++)

Tags:

c++

date

input

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.

like image 204
Kaitlyn Avatar asked Mar 19 '23 04:03

Kaitlyn


1 Answers

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 input 30/06/2014. The value of d becomes 30 and the rest of the input is not yet read. If you write the next std::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.

#include <iostream>

int main()
{
   int d;
   int m;
   int y;
   std::cin >> d; // read the day
   if ( std::cin.get() != '/' ) // make sure there is a slash between DD and MM
   {
      std::cout << "expected /\n";
      return 1;
   }
   std::cin >> m; // read the month
   if ( std::cin.get() != '/' ) // make sure there is a slash between MM and YYYY
   {
      std::cout << "expected /\n";
      return 1;
   }
   std::cin >> y; // read the year
   std::cout << "input date: " << d << "/" << m << "/" << y << "\n";
}

If you have the guarantee that the input format will be correct, it's OK to just write

std::cin >> d;
std::cin.get();
std::cin >> m;
std::cin.get();
std::cin >> y;

Alternatively, If you're not comfortable with using std::cin.get(), it's just as good as reading a character:

char slash_dummy;
int d;
int m;
int y;
std::cin >> d >> slash_dummy >> m >> slash_dummy >> y;

Here are some demos:

  • code with error checks
  • ignoring errors
  • without std::cin.get()
like image 61
stefan Avatar answered Apr 28 '23 06:04

stefan