[英]Read formatted input with cin?
Let's imagine this input : 01202014 (ddmmyyyy) . 让我们想象一下这个输入:01202014(ddmmyyyy)。
My question is : 我的问题是:
How can I read the input to three separated variables using pure C++ ? 如何使用纯C ++读取三个独立变量的输入?
As far as I know this would work , but this would be a mix C/C++ and I'm wondering if there is any solution that is pure C++. 据我所知这是可行的,但这将是C / C ++的混合,我想知道是否有任何纯C ++的解决方案。
#include <iostream>
int main()
{
int mm, dd, yy;
scanf_s("%2d%2d%4d", &mm, &dd, &yy);
//How can I do the same with Cin? std::cin
std::cout << mm << "/" << dd << "/" << yy;
system("pause");
}
Example : 范例:
Input : 01232009
输入:
01232009
Objective : 目的:
mm = 1;
dd = 23;
yy = 2009
Since you need in DDMMYYYY format, you can have following : 由于您需要DDMMYYYY格式,因此可以具有以下内容:
std::string date ;
std::getline( std::cin, date );
int dd,mm,yy;
if ( date.size() == 8 ) // Other checkings left for you
{
mm = std::stoi( date.substr(0,2) );
dd = std::stoi( date.substr(2,2) );
yy = std::stoi( date.substr(4) );
std::cout << dd << "/" << mm << "/" << yy ;
}
And now please don't change question ! 现在请不要更改问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.