|
A program, you can only enter numbers, you cannot enter other characters ~? If you enter other characters, an error message appears
How to change? It is best to add a FUNCTION 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;
Ranch
// 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;
Ranch
// 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;
} |
|