[英]How to take input on same line in c++?
i want to input Date in this format 2/11/2015.
我想以这种格式输入日期 2/11/2015。 how can i do this using C++ ?
我如何使用 C++ 做到这一点? Thanks The following method isn't working.
谢谢 以下方法无效。
cin>>day >>month >>year ;
and also user don't have to press enter .
并且用户不必按 enter 。
my Code is
我的代码是
#include <iostream>
using namespace std;
class Date
{
private :
int day,month,year;
char slash;
public :
void inputdate(void)
{
cout<<"Enter Date in Formate (day/month/year)"<<endl;
cin >> day >> slash >> month >> slash >> year;
}
void checkdate(void)
{
if (day<=0 || day>=32)
{
cout<<"Day is Wrong ! "<<endl;
}
if (month==2 && day>=29)
{
cout<<"February can have max 28 days !"<<endl;
}
if (month<=0 || month>=13)
{
cout<<"Month is wrong !"<<endl;
}
if (year<=1799 || year>=3000)
{
cout<<"Year is Wrong !"<<endl;
}
if ((month==4 || month==6 || month==9 || month==11)&&(day>30))
{
cout<<"Day is wrong ! September ,April ,June and November can have maximum 30 days ."<<endl;
}
}
void showdate(void)
{
checkdate();
cout<<"Date is : "<<day<<"/"<<month<<"/"<<year<<endl;
}
};
C++ does not inherently understand text dates; C++ 本身并不理解文本日期; you will need to either use a library which provides this functionality, or create a function yourself to convert between the text format and the internal integer format (which is generally the number of seconds or milliseconds, depending on platform, since the "Epoch" (00:00 1st January 1970)).
您将需要使用提供此功能的库,或者自己创建一个函数来在文本格式和内部整数格式之间进行转换(通常是秒数或毫秒数,取决于平台,因为“纪元”( 1970 年 1 月 1 日 00:00))。
To do this you will need to:为此,您需要:
Having said all this, the first option of using a Library is probably best as it will also contain functions to switch between string and internal date format;说了这么多,使用库的第一个选项可能是最好的,因为它还将包含在字符串和内部日期格式之间切换的函数; which library you choose is up to you and will largely depend on the platform you're coding for.
您选择哪个库取决于您,并且在很大程度上取决于您正在编码的平台。
You can use a dummy char
variable to read past the /
separator:您可以使用虚拟
char
变量来读取/
分隔符:
int day, month, year;
char slash; // dummy to skip past the '/'
cin >> day >> slash >> month >> slash >> year;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.