My problem is that when i run the program i ask the user to select a month from a menu then input a year. I then calulate the first day and janaury for that year. Then i have to find the first day of the month the user selected but when i print out my calendar the first day dostn match up and is way off. Please help
#include <iostream> // Need for cout,cin
#include <iomanip> // Need setf,fixed,showpoint,setprecision
#include <stdio.h> // Need for getchar
#include <fstream> // Needed for files
#include <cstdlib> // Needed for exit function
#include <string> // Need for string class
#include <cmath> // Needed for floor function
using namespace std;
int getmonth();
int getyear();
bool leapyear (int year);
int main ()
{
int month;
double year;
int firstday;
int x;
int daysinmonth;
int isleapyear;
int md;
int fmonth;
int days[12]={31,28,31,30,31,30,31,31,31,30,31};
month = getmonth();
year = getyear();
isleapyear = leapyear(year);
//if ((!year % 4) && ( year % 100)) || !(year % 400)
// cout << "leap year";
// calulates first day
firstday = ((((year-1)*365)+ floor((year-1)/4)- floor((year-1)/100)+ floor((year-1)/400))+1);
firstday = firstday%7;
// first day is 0 for sunday 1 for monday 2 for tuesday etc...
if ( month == 1 || month == 3 || month == 7 || month == 8 || month == 10 || month == 12)
{
md = 31;
}
else if (month == 2 && isleapyear)
{
md = 29;
}
else if (month == 2 && !isleapyear)
{
md = 28;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
md = 30;
}
if ( month == 1)
cout << "January" << setw(20) << year << endl;
else if ( month == 2)
cout << "February" << setw(20) << year << endl;
else if ( month == 3)
cout << "March" << setw(20) << year << endl;
else if ( month == 4)
cout << "April" << setw(20) << year << endl;
else if ( month == 5)
cout << "May" << setw(20) << year << endl;
else if ( month == 6)
cout << "June" << setw(20) << year << endl;
else if ( month == 7)
cout << "July" << setw(20) << year << endl;
else if ( month == 8)
cout << "August" << setw(20) << year << endl;
else if ( month == 9)
cout << "September" << setw(20) << year << endl;
else if ( month == 10)
cout << "October" << setw(20) << year << endl;
else if ( month == 11)
cout << "November" << setw(20) << year << endl;
else if ( month == 12)
cout << "December" << setw(20) << year << endl;
if (month == 1)
fmonth = firstday;
else if (month == 2)
fmonth = (days[0] % 7) + firstday;
else if (month == 3)
fmonth =((days[0] + days[1]) % 7) + firstday;
else if (month == 4)
fmonth =((days[0] + days[1] + days[2]) % 7) + firstday;
else if (month == 5)
fmonth =((days[0] + days[1] + days [2] + days[3]) % 7) + firstday;
else if (month == 6)
fmonth =((days[0] + days[1] + days [2] + days[3] + days[4]) % 7) + firstday;
else if (month == 7)
fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5]) % 7) + firstday;
else if (month == 8)
fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6]) % 7) + firstday;
else if (month == 9)
fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6] + days[7]) % 7) + firstday;
else if (month == 10)
fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6] + days[7] + days[8]) % 7) + firstday;
else if (month == 11)
fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6] + days[7] + days[8] + days[9]) % 7) + firstday;
else if (month == 12)
fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6] + days[7] + days[8] + days[9] + days [10]) % 7) + firstday;
if ((isleapyear == true) && (month != 1))
fmonth = fmonth + 1;
if (fmonth >= 7)
fmonth -= 7;
cout << "-----------------------------" << endl;
cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
int i = 0;
for (i = 0; i < fmonth; i++)
{
cout << " ";
}
for (i = 1; i <= md; i++)
{
cout << setw(4) << i;
if ((i + fmonth) % 7 == 0)
cout << endl;
}
cout.setf (ios::showpoint );
cout.setf( ios::fixed);
cout << setprecision(2);
cout << " Press Enter to continue" << endl;
cin.ignore();
char ch = getchar();
return 0;
}
//****************getmonth***Function***************************
//
// Function name: getmonth
//
// Purpose: to prompt for the month
//
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: The month selected fromt he list using a number
//
//
//************************************************************
int getmonth()
{
int tempmonth = 0;
{
cout << "Please select a month from the list" << endl;
cout << "1. January" << endl;
cout << "2. February" << endl;
cout << "3. March" << endl;
cout << "4. April" << endl;
cout << "5. May" << endl;
cout << "6. June" << endl;
cout << "7. July" << endl;
cout << "8. August" << endl;
cout << "9. September" << endl;
cout << "10. October" << endl;
cout << "11. November" << endl;
cout << "12. December" << endl;
cin >> tempmonth;
if (tempmonth > 0 && tempmonth < 13)
{
return tempmonth;
}
else
{
cout << "Please enter a selection from 1-12" << endl;
getmonth();
}
}
}
//****************getyear***Function***************************
//
// Function name: getyear
//
// Purpose: to prompt for the year
//
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: The year the user inputs
//
//
//************************************************************
int getyear()
{
int tempyear;
{
cout << " Please enter the year" << endl;
cin >> tempyear;
}
return tempyear;
}
//****************leapyear***Function***************************
//
// Function name: leapyear
//
// Purpose: To determine fi the year the user inputted is a leap year or not
//
//
// Input parameters: int year
//
// Output parameters: none
//
// Return Value: true or false
//
//
//************************************************************
bool leapyear(int year)
{
if ((!(year%4)&&(year%100))||!(year%400))
return true;
else
return false;
}