使用 CIN 提示并接收日期“MM/DD/YYYY”,忽略“/”字符?(在 C++ 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24483655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:45:02  来源:igfitidea点击:

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

c++dateinput

提问by Kaitlyn

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.

是的,这是一个任务。我不介意努力得到答案,我不想要确切的答案!:) 这是我的第一个 C++ 课程。我是带着 VBA、MySql、CSS 和 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

我们需要编写一个具有多种不同功能的程序。其中之一需要以某种"MM/DD/YYYY"格式接收日期输入。虽然这本身很容易;作为初学者,我只想说

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.

但是,我相信我们的教授希望用户通过准确输入“12/5/2013”​​或任何其他日期来输入日期。



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.

'/'可以通过CIN读取。所以读取'/'字符并忽略它。将日设置为等于第三个输入,将月设置为第一个输入,将年设置为第五个输入。丢弃第 2 个和第 4 个输入。

^ 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).

到目前为止,我们只经历了用户每次输入后点击回车时的cin。所以我不知道他是否希望用户在 12 点之后按 Enter,然后在 '/' 之后再次点击,然后在 5 之后,在 '/' 之后,最后在 '2013' 之后(使用 12/5/2013 的先前示例) 2013 年 12 月 5 日)。

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.

有没有更有经验的人对我应该做什么有任何可能的见解?我们只学过如何使用“cin”来接收输入(所以我们不知道其他接收输入的方法),我不知道当作为字符串输入时如何“忽略一个字符”,例如 '12/ 5/2013' 正好。

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.

编辑:我在这里寻找答案,但我遇到的所有答案都超出了我们所教授的范围,因此不允许在作业中使用。虽然我可以很容易地理解更高级编码的逻辑,但我对我缺乏轻松解决这些更简单问题的能力感到沮丧。因此我在这里发帖。我花了几个小时浏览我们的教科书,寻找可能的答案或“忽略”输入字符串中的字符的线索,但结果很短。

回答by stefan

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 dbecomes 30and the rest of the input is not yet read. If you write the next std::cinstatement, the content that is available is /06/2014.

其实很简单!问题是:您可以输入不止一件事。这意味着,如果你写int d; std::cin >> d;,输入 完全没问题30/06/2014。的值d变成30了,输入的其余部分尚未读取。如果你写下一个std::cin语句,可用的内容是/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:

或者,如果您不习惯使用std::cin.get(),它就像阅读一个字符一样好:

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

Here are some demos:

下面是一些演示:

回答by pipja

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.

瞧,您从输入中获得了您的日、月和年。

回答by Tanuj Nagpal

Why not use stringstreams?

为什么不使用字符串流?

string input;
int year, month, day;
cin >> input; // input can be 2005:03:09 or 2005/04/02 or whatever
stringstream ss(input);
char ch;
ss >> year >> ch >> month >> ch >> day;