Hello I have a date program that takes the date from the user and then prints the date and number of days in the year. The output is supposed to look like this if 3/7/1999 is entered
07-March-1999 is day number 66 in 1999
obviously this is not how it is working so any help would be nice.
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void getDate (int &, int &, int &);
bool validDate (int, int, int);
bool isLeapYear (int);
void printDate (int, int, int);
int dayNumber (int, int, int);
int main ()
{
int day;
int month;
int year;
int totalDays;
char again;
bool valid;
cout << "This program will take a date that you enter and it will determine if the year was" << endl;
cout << "a leap year and how many days into the year you are." << endl;
do
{
getDate(day, month, year);
valid = validDate(day, month, year);
if (valid == 1)
{
printDate(day, month, year);
totalDays = dayNumber(day, month, year);
cout << "is day number" << " " << totalDays << "in" << " " << year << endl;
}
else
{
cout << "Error you did not follow the proper format" << endl;
}
cout << "Do you wish to repate the program? Enter Y or N" << endl;
cin >> again;
} while (again == 'Y' || again == 'y');
return 0;
}
void getDate (int& day, int& month, int& year)
{
char second;
char fourth;
cout << "Please enter a date in the format of m/d/yyyy" << endl;
cin >> month >> second >> day >>fourth >> year;
month = month;
day = day;
year = year;
}
bool validDate (int day, int month, int year)
{
int numberOfDays;
bool status;
bool leap;
if (year >= 1 || year <= 3000)
if (month >= 1 || month <= 12)
switch(month)
{
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
numberOfDays = 31;
if (day == numberOfDays)
{
status = true;
}
else
{
status = false;
}
break;
case 2 :
leap = isLeapYear(year);
if (leap == 1)
{
numberOfDays = 29;
if (day == numberOfDays)
{
status = true;
}
else
{
status = false;
}
}
else
{
numberOfDays = 28;
if (day == numberOfDays)
{
status = true;
}
else
{
status = false;
}
}
break;
default:
numberOfDays = 30;
if (day == numberOfDays)
{
status = true;
}
else
{
status = false;
}
}
return status;
}
bool isLeapYear (int year)
{
bool leapYear;
if (year % 4 != 0)
leapYear = false;
else if (year % 100 == 0)
if (year % 400 == 0 )
leapYear = true;
else
leapYear = true;
return leapYear;
}
void printDate (int day, int month, int year)
{
string monthOfYear;
switch (month)
{
case 1 :
monthOfYear = "January";
break;
case 2 :
monthOfYear = "February";
break;
case 3 :
monthOfYear = "March";
break;
case 4 :
monthOfYear = "April";
break;
case 5 :
monthOfYear = "May";
break;
case 6 :
monthOfYear = "June";
break;
case 7 :
monthOfYear = "July";
break;
case 8 :
monthOfYear = "August";
break;
case 9 :
monthOfYear = "September";
break;
case 10 :
monthOfYear = "October";
break;
case 11:
monthOfYear = "November";
break;
default:
monthOfYear = "December";
}
if (day >= 1 || day <= 9)
std::cout << setfill('0')
<< setw(1) << day << "-" << monthOfYear << "-" << year;
else
cout << day << "-" << monthOfYear << "-" << year;
}
int dayNumber (int day, int month, int year)
{
int numberOfDays;
int totalDays;
bool leap;
leap = isLeapYear (year);
if (leap == 1)
{
validDate(day, month, year);
for (int x = 1; x < month - 1; x++)
switch (month)
{
case 1:
numberOfDays = 31;
totalDays = numberOfDays;
break;
case 2 :
numberOfDays = 29;
totalDays = totalDays + numberOfDays;
break;
case 3 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 4 :
numberOfDays = 30;
totalDays = totalDays + numberOfDays;
break;
case 5 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 6 :
numberOfDays = 30;
totalDays = totalDays + numberOfDays;
break;
case 7 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 8 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 9 :
numberOfDays = 30;
totalDays = totalDays + numberOfDays;
break;
case 10 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 11 :
numberOfDays = 30;
totalDays = totalDays + numberOfDays;
break;
case 12 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
default:
totalDays = day;
}
}
else
{
for (int x = 1; x < month - 1; x++)
switch (month)
{
case 1:
numberOfDays = 31;
totalDays = numberOfDays;
break;
case 2 :
numberOfDays = 28;
totalDays = totalDays + numberOfDays;
break;
case 3 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 4 :
numberOfDays = 30;
totalDays = totalDays + numberOfDays;
break;
case 5 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 6 :
numberOfDays = 30;
totalDays = totalDays + numberOfDays;
break;
case 7 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 8 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 9 :
numberOfDays = 30;
totalDays = totalDays + numberOfDays;
break;
case 10 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
case 11 :
numberOfDays = 30;
totalDays = totalDays + numberOfDays;
break;
case 12 :
numberOfDays = 31;
totalDays = totalDays + numberOfDays;
break;
default:
totalDays = day;
}
}
return totalDays;
}