使用scanf函数报内存错误
程序代码:
#include <iostream> #include <stdio.h> using namespace std; class Date{ private: int year; int month; int day; public: bool IsLeapYear(); const void display(); Date():year(1800),month(12),day(10){} Date(int a,int b,int c):year(a),month(b),day(c){} bool DateInput(){ cout<<"请输入年份"<<"如:1900"<<endl; scanf("%d",this->year); while (this->year>9999||this->year<1000) { cout<<"请重新输入年份"<<endl; scanf("%d",this->year); } cout<<"请输入月份"<<"如:12"<<endl; scanf("%d",this->month); while (this->month>12||this->month<1) { cout<<"请重新输入月份"<<endl; scanf("%d",this->month); } cout<<"请输入日子"<<"如:25"<<endl; scanf("%d",this->day); while (this->day>31||this->month<1) { cout<<"请重新输入日子"<<endl; scanf("%d",this->day); } return 0; } }; bool Date::IsLeapYear(){ if((year % 4 &&!( year %100)) || (year % 400 )) return 1; else return 0; } const void Date::display(){ if (IsLeapYear()==1) { cout<<this->year<<"is a leap year!"<<endl; } else cout<<this->year<<"is not a leap year!"<<endl; } void main(){ Date date1(2008,12,2); Date date2(2000,12,2); Date date3(2100,12,2); date1.display(); date2.display(); date3.display(); Date date4; date4.DateInput(); }程序正常运行,为什么我一输入年份,就报内存错误呢?