Prompt for and receive a date “MM/DD/YYYY” using C

2019-03-22 04:07发布

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.

标签: c++ date input
2条回答
小情绪 Triste *
2楼-- · 2019-03-22 04:54

follow this pseudo code:

declare a char pointer to accept input
declare int to use as day, month and year
initialize day, month and year to 0
declare a int count to know what number you are up to

read `cin` operation into your input
increment through the input, stop if the current input position is NULL
    read out character
    if character != "/"
        if count == 0
            if month == 0
                month = (int)character
            else
                month = month * 10 + (int)character
            endif
        else 
        if count == 1
            if day == 0
                day = (int)character
            else 
                day = day * 10 + (int)character 
            endif
        else
            if year < 1000
                year = year * 10 + (int)character
            endif
        endif
        endif
    else count += 1 endif

and voilà you have your day, month and year from input.

查看更多
老娘就宠你
3楼-- · 2019-03-22 04:55

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:

查看更多
登录 后发表回答