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

问题描述:

好的,我以(yyyy/mm/dd)的格式询问用户的日期,然后在这种情况下下降到函数dateSplit。我知道自从我知道它总能成为我可以模拟它的前四位数字之后,我该如何做到这一年。任何人都知道的方式做一个月,这里的天是我到目前为止的代码:C++如何从一个整数值中获取三个单独的日期值(日,月,年)

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; 

    } 
} 

这是终于做到了代码:

 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; 

    } 
+0

这是'日期'来自哪里? – BeyelerStudios 2015-01-15 17:47:04

+1

yyyy/mm/dd不是整数。你实际上从用户那里得到了什么? – pm100 2015-01-15 17:47:17

+0

从用户,我将编辑我的帖子,并包括我的主要功能 – user2311215 2015-01-15 17:47:46

只需使用部门和modulos砍它起来,像这样:

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

没有正确地分割它 – user2311215 2015-01-15 18:33:12

+0

我修改了我的代码最新编辑。但是,由于你回答了问题,因此将你标记为回答者。 – user2311215 2015-01-15 18:41:10