|
一个程序, 想只能输入数字, 不能输入其他的字符~? 如果输入其他的字符就出现错误消息
怎么改? 最好是加个FUNCTION功能.
#include <iostream.h>
#include <conio.h>
int main () {
//declare and initialise variables
int year1=0;
int year2=0;
int countyear=0;
//Display the "introduction"
cout<<"\n ---------Count Leap Year Program---------";
//obtain and input of the start year and end year from the user
cout<<"\n Please enter the start year: ";
cin>>year1;
cout<<"\n Please enter the End Year: ";
cin>>year2;
//loop through the leap years from start year and end year
for (int i = year1; i <=year2; i++) {
//year can be divide by 4 left 0, and year can be divide by 100 not equal to 0 or year can be divide by 400 is a leap year. otherwise will not display the years.
if ((i%4==0)&&(i%100!=0)||(i%400==0)){
//display the leap years that user has input
cout <<"\n"<< i;
// add the current input to the total
++countyear<<i;
}
}
cout <<"\n The number of Leap year is: "<<countyear<<" of leap years";
cout<<"\n\n Program finished........";
getch();
return 0;
}
|
|