Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;如何从一个整数值中获取三个单独的日期值(日、月、年)_C++_Date - Fatal编程技术网

C++ C++;如何从一个整数值中获取三个单独的日期值(日、月、年)

C++ C++;如何从一个整数值中获取三个单独的日期值(日、月、年),c++,date,C++,Date,好的,我要求用户输入(yyyy/mm/dd)格式的日期,然后在本例中转到函数dateSplit。我知道这一年我是怎么过的,因为我知道它永远是我能做到的前四位数。任何人都知道一个月和一天的方法这是我到目前为止的代码: void dateSplit(int date, int& year, int& day, int& mon) { // date % 10000 is a floating point value but i put it into an int t

好的,我要求用户输入(yyyy/mm/dd)格式的日期,然后在本例中转到函数dateSplit。我知道这一年我是怎么过的,因为我知道它永远是我能做到的前四位数。任何人都知道一个月和一天的方法这是我到目前为止的代码:

void dateSplit(int date, int& year, int& day, int& mon)
{
    // date % 10000 is a floating point value but i put it into an int to cut the back off
    date % 10000 = year;

}
有人知道我怎么能读中间的两个数字和最后两个吗

我想我会把我的主代码放在这里,这样人们就可以看到整个画面:

int main()
{
    // Variable Declarations
    string airportCode, lat, longitude, timeZone;
    int date;
    char contin = 'Y';
    while (contin == 'Y' || contin == 'y')
    {
        // Ask User for Airport Code
        cout << "Please Enter an Airport Code: ";
        cin >> airportCode;

        //Call to retrieve information
        retrieveFromFile(airportCode, lat, longitude, timeZone);

        //Call for date
        cout << endl << "Please Enter a date(yyyy/mm/dd): ";
        cin >> date;

        // Continue running program?
        cout << endl << "Would you like to continue? (Y/N): ";
        cin >> contin;

    }
}
intmain()
{
//变量声明
字符串airportCode、纬度、经度、时区;
国际日期;
char contin='Y';
而(contin='Y'| | contin='Y')
{
//询问用户机场代码
cout>机场代码;
//调用以检索信息
检索文件(机场代码、纬度、经度、时区);
//要求约会
日期;
//继续运行程序?
康定;
}
}
这是最终完成此操作的代码:

     void dateSplit(int date, int& year, int& day, int& mon)
    {
        // Ex. if date is 20150623, then it takes that number
        // moves the decimal place over four digits
        // then cuts off after the decimal point
        // leaving just the first four digits
        year = date / 10000;
        date %= 10000;
        mon = date / 100;
        day = date % 100;




        cout << endl << "Year: " << year
             << endl << "Day : " << day
             << endl << "Month:" << mon;

    }
void dateSplit(整数日期、整数和年份、整数和日期、整数和周一)
{
//如果日期为20150623,则取该数字
//将小数位移到四位数以上
//然后在小数点后截断
//只留下前四位数字
年份=日期/10000;
日期%=10000;
周一=日期/100;
日期=日期%100;

可以用除法和模来分解它,比如:

year = date % 10000;
date /= 10000;
month = date % 100;
day = date / 100;

该日期从哪里来?yyyy/mm/dd不是整数。您从用户那里得到了什么?请确保您处理了用户进入2012/6的情况/25@user2311215如果用户正在输入<代码> 2014 / 06 / 23 ,那么您应该接收<代码> 2014 / 06 /23 < /COD>作为字符串。如果中间点有一些疯狂的过程请将其转换为整数,告诉它停止,并将日期作为字符串传递给您。您应该接受yyyy/mm/dd,并使用/作为分隔符(string.split)将字符串拆分为3部分。您现在拥有了您想要编辑的3部分代码,如我最近的编辑中所示。但将您标记为应答者,因为您已回答了它。