1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void intro (string);
void outro (string);
void calcleap(int);
int main()
{
string name;
cout << "Please enter your user name:";
cin >> name;
intro (name);
char x=1;
while ((x!='0')||(x=='1'))
{
char leap;
if (cin >> leap && !isdigit(leap)) //here I would like the variable leap to be checked for up to 4 characters to see if they are all of numeric value... is there any way?
{
while (!(isdigit(leap)))
{
cout << "You have entered incorrectly, please enter again:";
cin >> leap;
}
}
calcleap(leap);
cout << "Would you like to run the program again, " << name << "?(1 for yes, 0 for no)\n";
cin >> x;
if (x=='1')
cout << "You have chosen to enter and test another year.\nPlease enter in a 4 digit number:";
else
cout << "You have chosen to quit the program.\n";
}
outro (name);
return 0;
}
//Function Definition
void intro (string name)
{
cout << "Welcome, " << name << ".\n\nThis program is designed to receive an input value(a whole number year) from the user, and decipher whether or not it is a leap year.";
cout << "\nRemember, a leap year comes ever 4 years, and a century year (every 100 years) is only considered a leap year every 400 years.";
cout << "\n\nIf the year you enter is not a leap year, this program will calculate the closest leap year before and after the year entered.\n\nPlease enjoy the program, and use it as long as you'd like!";
cout << "\n\nPlease enter in a whole number year you would like to have calculated:";
}
void calcleap(int leap)
{
int a=leap-1, b=leap-2, c=leap-3, d=leap+1, e=leap+2, f=leap+3, g=leap-4, h=leap+4;
{
if ((leap%4)==0)
{
if ((leap%100==0)&&(leap%400==0))
cout << "Coincidentally, the year you have entered is a leap year and also a century year.\n";
else if ((leap%100==0)&&(leap%400!=0))
{
cout << "The year you have entered is a century year, but is not a leap year.\n\n";
cout << "The leap year prior to and after the year you entered is:";
cout << g << " and " << h << ", respectively.\n\n";
}
else
cout << "The year you have entered is a leap year.\n\n";
}
else if (leap%4==1)
{
if (a % 100 == 0)
a=leap-5;
if (f % 100 == 0)
f=leap+7;
cout << "Sorry, though, the year you have entered is not a leap year.\n\nThe leap year prior to and after the year you entered is:";
cout << a << " and " << f << ", respectively.\n\n";
}
else if (leap%4==2)
{
if (b % 100 == 0)
b = leap - 6;
if (e % 100 == 0)
e = leap +6;
cout << "Sorry, though, the year you have entered is not a leap year.\n\nThe leap year prior to and after the year you entered is:";
cout << b << " and " << e << ", respectively.\n\n";
}
else if (leap%4==3)
{
if (c % 100 == 0)
c = leap - 7;
if (d % 100 == 0)
d = leap +5;
cout << "Sorry, though, the year you have entered is not a leap year.\n\nThe leap year prior to and after the year you entered is:";
cout << c << " and " << d << ", respectively.\n\n";
}
}
}
void outro (string name)
{
cout << "Please return some time to test leap years!\nFor now, have a great day, " << name << "!\n";
}
|