I have a program that prompts the user to put in to dates and then calculates the days the problem is if someone were to put in a / the code would not work properly
i have managed to put in code to make it so the program stops and displays a invalid massage
is it possible to make it so the computer skips write over it and calculates for the numbers anyway
/* this program will prompt the user to type in two dates then*\
\*the computer will calculate the amount of years between them*/
#include <iostream>
usingnamespace std;
// inputs year and outputs febuary as 1 or 2 to be used in the MonthToDays function
int FebuaryMonth(int year)
{
bool Divisible_4;
bool Divisible_100;
bool Divisible_400;
// divides by 4 and checks if there is a remander
if((year % 4) == 0)
{
Divisible_4 = true;
}
else
{
Divisible_4 = false;
}
// checks if years is divisible by 100
if((year % 100) == 0)
{
Divisible_100 = true;
}
else
{
Divisible_100 = false;
}
// checkes if years is not divisible by 400
if((year % 400) == 0 && year >= 400)
{
Divisible_400 = true;
}
else
{
Divisible_400 = false;
}
if((Divisible_4 && ! Divisible_100) || (Divisible_100 && ! Divisible_400))
{
return 1;
}
else
{
return 2;
}
}
// inputs month and Year and outputs amount of days
int MonthToDays(int Month, int Year)
{
int Febuary = FebuaryMonth(Year);
int days;
Month = Month - 1;
switch(Month)
{
case 1:
days = (days + 1) * 30;
break;
case 2:
days = (days - Febuary + 1) * 30;
break;
case 3:
days = (days - Febuary + 2) * 30;
break;
case 4:
days = (days - Febuary + 2) * 30;
break;
case 5:
days = (days - Febuary + 3) * 30;
break;
case 6:
days = (days - Febuary + 3) * 30;
break;
case 7:
days = (days - Febuary + 4) * 30;
break;
case 8:
days = (days - Febuary + 5) * 30;
break;
case 9:
days = (days - Febuary + 5) * 30;
break;
case 10:
days = (days - Febuary + 6) * 30;
break;
case 11:
days = (days - Febuary + 6) * 30;
break;
case 12:
days = (days - Febuary + 7) * 30;
break;
default:
cout << " You Have typed a invalid month" << endl;
}
return days;
}
int main()
{
int Day1;
int Month1;
int Year1;
int Day2;
int Month2;
int Year2;
double TotalDay1;
double TotalDay2;
char Spacer;
char again;
bool Running = true;
cout << " **Welcome to the Date Day Calculator** " << endl;
while(Running == true)
{
// user information
cout << " Plese type in the date in numbers " << endl;
cout << " make sure you type in the whole year not just the last two digits " << endl;
cout << " Press enter after each number" << endl;
cout << " this program will caculate the days inbetween two Dates " << endl;
cout << "Date 1: " << endl;
// input Date 1
cin >> Month1;
cin >> Spacer;
if(Spacer == '/')
{
cout << " Invalid operation " << endl;
break;
}
cin >> Day1;
cin >> Spacer;
if(Spacer == '/')
{
cout << " Invalid operation " << endl;
break;
}
cin >> Year1;
TotalDay1 = MonthToDays(Month1, Year1) + Day1 + (Year1 * 365.25);
cout << "Date 2: ";
// input Date 2
cin >> Month2;
cin >> Spacer;
if(Spacer == '/')
{
cout << " Invalid operation " << endl;
break;
}
cin >> Day2;
cin >> Spacer;
if(Spacer == '/')
{
cout << " Invalid operation " << endl;
}
cin >> Year2;
TotalDay2 = MonthToDays(Month2, Year2) + Day2 + (Year2 * 365.25);
// Displays Days between moths alone with dates
cout <<" There is " << TotalDay1 - TotalDay2 << " Between " << Month1 << "/" << Day1 << "/" << Year1 << " and "
<< Month2 << "/" << Day2 << "/" << Year2 << endl << endl << endl << endl;
// asks user if they would like to redo program
cout << " Would You Like To Run this Program Again ( y or n ) " << endl;
cin >> again;
if(again == 'y' || again == 'Y')
{
Running = true;
}
elseif(again == 'n' || again == 'N')
{
Running = false;
}
}
system("PAUSE");
return 0;
}
/* this program will prompt the user to type in two dates then*/
/*the computer will calculate the amount of years between them*/
#include <iostream>
usingnamespace std;
// inputs year and outputs febuary as 1 or 2 to be used in the MonthToDays function
int FebuaryMonth(int year)
{
bool Divisible_4;
bool Divisible_100;
bool Divisible_400;
// divides by 4 and checks if there is a remander
if((year % 4) == 0)
{
Divisible_4 = true;
}
else
{
Divisible_4 = false;
}
// checks if years is divisible by 100
if((year % 100) == 0)
{
Divisible_100 = true;
}
else
{
Divisible_100 = false;
}
// checkes if years is not divisible by 400
if((year % 400) == 0 && year >= 400)
{
Divisible_400 = true;
}
else
{
Divisible_400 = false;
}
if((Divisible_4 && ! Divisible_100) || (Divisible_100 && ! Divisible_400))
{
return 1;
}
else
{
return 2;
}
}
// inputs month and Year and outputs amount of days
int MonthToDays(int Month, int Year)
{
int Febuary = FebuaryMonth(Year);
int days;
Month = Month - 1;
switch(Month)
{
case 1:
days = (days + 1) * 30;
break;
case 2:
days = (days - Febuary + 1) * 30;
break;
case 3:
days = (days - Febuary + 2) * 30;
break;
case 4:
days = (days - Febuary + 2) * 30;
break;
case 5:
days = (days - Febuary + 3) * 30;
break;
case 6:
days = (days - Febuary + 3) * 30;
break;
case 7:
days = (days - Febuary + 4) * 30;
break;
case 8:
days = (days - Febuary + 5) * 30;
break;
case 9:
days = (days - Febuary + 5) * 30;
break;
case 10:
days = (days - Febuary + 6) * 30;
break;
case 11:
days = (days - Febuary + 6) * 30;
break;
case 12:
days = (days - Febuary + 7) * 30;
break;
default:
cout << " You Have typed a invalid month" << endl;
}
return days;
}
int main()
{
int Day1;
int Month1;
int Year1;
int Day2;
int Month2;
int Year2;
double TotalDay1;
double TotalDay2;
char Spacer;
char again;
bool Running = true;
cout << " **Welcome to the Date Day Calculator** " << endl;
while(Running == true)
{
// user information
cout << " Plese type in the date in numbers " << endl;
cout << " make sure you type in the whole year not just the last two digits " << endl;
cout << " Press enter after each number" << endl;
cout << " this program will caculate the days inbetween two Dates " << endl;
cout << "Date 1: Month: " << endl;
// input Date 1
cin >> Month1;
cout<<"input separator"<<endl;
cin >> Spacer;
if(Spacer == '/')
{
cout << " Invalid operation " << endl;
break;
} cout<<"day: "<<endl;
cin >> Day1;
cout<<"input separator"<<endl;
cin >> Spacer;
if(Spacer == '/')
{
cout << " Invalid operation " << endl;
break;
}
cout<<"insert year:"<<endl;
cin >> Year1;
TotalDay1 = MonthToDays(Month1, Year1) + Day1 + (Year1 * 365.25);
cout << "Date 2: month2: ";
// input Date 2
cin >> Month2;
cout<<"input separator"<<endl;
cin >> Spacer;
if(Spacer == '/')
{
cout << " Invalid operation " << endl;
break;
} cout<<"day: "<<endl;
cin >> Day2;
cout<<"input separator"<<endl;
cin >> Spacer;
if(Spacer == '/')
{
cout << " Invalid operation " << endl;
}
cout<<"insert year"<<endl;
cin >> Year2;
TotalDay2 = MonthToDays(Month2, Year2) + Day2 + (Year2 * 365.25);
// Displays Days between moths alone with dates
cout <<" There is " << TotalDay1 - TotalDay2 << " Between " << Month1 << "/" << Day1 << "/" << Year1 << " and "
<< Month2 << "/" << Day2 << "/" << Year2 << endl << endl << endl << endl;
// asks user if they would like to redo program
cout << " Would You Like To Run this Program Again ( y or n ) " << endl;
cin >> again;
if(again == 'y' || again == 'Y')
{
Running = true;
}
elseif(again == 'n' || again == 'N')
{
Running = false;
}
}
system("PAUSE");
return 0;
}