one thing I can tell you right away is that you can remove about 50 lines of code without changing anything.
in each month you have:
{if (leapsum=0)
days=30;
else
days=30;}
you could just as easily have done
days=30;
and be done with it. The only place that you even need the if statement is in February, all the other months have the same number of days even in a leap year.
and in each case you have a line that just says
month;
what is the purpose of this, it does nothing, except probably throw a compiler error.
And I may be missing something, but I don't see anywhere that the whole daysum thing is even used. What is daysum supposed to calculate, and how is that calculation supposed to be displayed?
This is new code. Please excuse me if it seems bad. I'm brand new at this. Please help and give me pointers on how i can fix it. Also i can't get the day to appear in startday. It just prints junk.
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
void Weekday(char[10],int&);
using namespace std;
int main ()
{
int year, month, leapsum;
int days;
int firstday, fday;
char startday [10];
cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
cout<< "Please leave a space in between the month and year when you enter them: \n";
The program has to calculate the month of any year and the day the month starts on. I working on the day that the month starts on. some help would be great.
-use the #format when uploading code
-dont do the following:
1 2 3 4 5 6 7 8
if (condition)
{
... //do someting
}
else
{
... //do excactly the same thing
}
-your function Weekday() dont work. You only need one parameter (fday), wich you have to initialize before calling it. The function needs to pass a value to startday.
-You're treating firstday and leapsum as boolvariables: what are you trying to do with the && and || stuff? This returns either true or false, and when you initialize a integer with true or false, its value become 1(true) or 0(false)
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>
usingnamespace std;
int main ()
{
string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
string months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int year, month, leapsum;
int days;
int firstday, fday;
cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
cout<< "Please leave a space in between the month and year when you enter them: \n";
cin>>month>>year;
//I can see you thinking here - but it is not correct
firstday=( ((year-1)*365) + 4*((year-1)/4) - 4*((year-1)/100) + 4*((year-1)/400)+1 )%7;
leapsum=(!(year%4)&&(year%100))||!(year%400); // keep this - will be usefull later
cout << "First day of " << months[month-1] << " of " << year << "is a " << dayNames[firstday] << endl;
system("pause");
}
You calculation for first day of the year seems to be just a wild guess.
It seems to me that you are taking the number of days of all the completed years since
year 0000 A.D and trying to add to add on the extra days to account for
the number of leap years.
Not going to work with that formula.
I think we need to come up with a more sensible/practical method.
firstday=( ((year-1)*365) + floor*((year-1)/4) - floor*((year-1)/100) + floor*((year-1)/400)+1 )%7;
that is the original calculation bui can't get the floor value to work?
error C2563: mismatch in formal parameter list
error C2563: mismatch in formal parameter list
error C2563: mismatch in formal parameter list
The first day calculation keeps telling me the first day of a year, i and its the same day for that perticular year.
"firstday=( ((year-1)*365) + 4*((year-1)/4) - 4*((year-1)/100) + 4*((year-1)/400)+1 )%7;"
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>
usingnamespace std;
constint firstYear = 1800; //This is our start point
constint dayOffset = 3; //The first day of our start year may not be a Sunday ( in 1800 it was Wednesday)
constint firstLeapYear = 1804; //help to reduce how many leap years we have to check
string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
string months[] = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //per month per year
//helper function declarations
int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);
int main ()
{
int year, month;
int days;
cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
cout<< "Please leave a space in between the month and year when you enter them: \n";
cin>>month>>year;
//Calculate how many days have passed since Jan 1 of Start year to start of requested year & month
days = calcDaysSoFar(year, month);
int firstDayOfWeek = days % 7;
cout << "First day of " << months[month-1] << " " << year << " is a " << dayNames[firstDayOfWeek] << endl;
system("pause");
}
//This function calculates the number of days passed from some start point year
int calcDaysSoFar( int year, int month)
{
int days;
//calculates basic number of days passed
days = (year - firstYear) * 365;
days += dayOffset;
days += daysPassed[month-1];
//add on the extra leapdays for past years
for (int count = firstLeapYear; count < year ; count +=4)
{
if (isLeapYear(count) )
{
days++;
}
}
//add leapday for this year if requested month is greater than Feb
if( month > 2 && isLeapYear(year) )
{
days++;
}
return days;
}
//This function checks whether a particular year is a leap year
bool isLeapYear( int year)
{
return (!(year%4)&&(year%100))||!(year%400);
}
so this is what I have , but i can't get the chart, the numbers inside the chart, and the days to show on the screen. and I don't know where to put the setw spaces.
int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);
char Calendar (const char[]chart);
int main ()
{
char chart;
int year, month;
int days;
cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
cout<< "Please leave a space in between the month and year when you enter them: \n";
cin>>month>>year;
days = calcDaysSoFar(year, month);
int firstDayOfWeek = days % 7;
cout << "First day of " << months[month-1] << " " << year << " is a " << dayNames[firstDayOfWeek] << endl;
Calendar (chart);
cout<<chart<<endl;
system("pause");
}
int calcDaysSoFar( int year, int month)
{
int days;
days = (year - firstYear) * 365;
days += dayOffset;
days += daysPassed[month-1];
for (int count = firstLeapYear; count < year ; count +=4)
{
if (isLeapYear(count) )
{
days++;
}
}
if( month > 2 && isLeapYear(year) )
{
days++;
}
return days;
}
bool isLeapYear( int year)
{
return (!(year%4)&&(year%100))||!(year%400);
}
Are you absolutely sure that the requirement is to draw all those ascii grid characters. Maybe they just want the calendar dispalyed in a "grid-like" pattern.
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>
usingnamespace std;
//helper function declarations
int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);
void printCalendar(int month /*1 = January*/, int year, int firstDay /* 0 = Sunday*/);
//Globals
constint firstYear = 1800; //This is our start point
constint dayOffset = 3; //The first day of our start year may not be a Sunday ( in 1800 it was Wednesday)
constint firstLeapYear = 1804; //help to reduce how many leap years we have to check
string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
string months[] = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
int daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31} ; //standard year
int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //per month per year
int main ()
{
int year, month;
int days;
cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
cout<< "Please leave a space in between the month and year when you enter them: \n";
cin>>month>>year;
//Calculate how many days have passed since Jan 1 of Start year to start of requested year & month
days = calcDaysSoFar(year, month);
int firstDayOfWeek = days % 7;
cout << "First day of " << months[month-1] << " " << year << " is a " << dayNames[firstDayOfWeek] << endl;
printCalendar(month, year, firstDayOfWeek);
system("pause");
}
//This function calculates the number of days passed from some start point year
int calcDaysSoFar( int year, int month)
{
int days;
//calculates basic number of days passed
days = (year - firstYear) * 365;
days += dayOffset;
days += daysPassed[month-1];
//add on the extra leapdays for past years
for (int count = firstLeapYear; count < year ; count +=4)
{
if (isLeapYear(count) )
{
days++;
}
}
//add leapday for this year if requested month is greater than Feb
if( month > 2 && isLeapYear(year) )
{
days++;
}
return days;
}
//This function checks whether a particular year is a leap year
bool isLeapYear( int year)
{
return (!(year%4)&&(year%100))||!(year%400);
}
//Prints Month and Year
//followed by the day headings
//followed by the numbers
void printCalendar(int month, int year, int firstDay)
{
cout << '\n';
cout << months[month-1] << " " << year << '\n';
cout << "Sun " << "Mon " << "Tue " << "Wed " << "Thu " << "Fri " << "Sat "<< '\n';
int count,offset;
offset= 1 - firstDay;
count = daysInMonth[month-1]; //get the number of days in this month
//adjust if dealing with February and year is leap year
if(isLeapYear(year) && (month==2) )
{
count++;
}
for (int x = offset; x <= count; x +=7)
{
for(int column= x; (column < x+7) && (column <= count); column++)
{
if (column <=0)
{
cout << setw(4) << left << setprecision(3) << " ";
}
else
{
cout << setw(4) << left << setprecision(3) << setfill(' ') << column;
}
}
cout << '\n';
}
cout << endl;
}
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>
usingnamespace std;
//helper function declarations
int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);
void printCalendar(int month /*1 = January*/, int year, int firstDay /* 0 = Sunday*/);
//Globals
constint firstYear = 1800; //This is our start point
constint dayOffset = 3; //The first day of our start year may not be a Sunday ( in 1800 it was Wednesday)
constint firstLeapYear = 1804; //help to reduce how many leap years we have to check
string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
string months[] = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
int daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31} ; //standard year
int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //per month per year
int main ()
{
int year, month = 0;
int days;
cout << "Enter year to view calendar: ";
cin >> year;
do{
//Calculate how many days have passed since Jan 1 of Start year to start of requested year & month
days = calcDaysSoFar(year, month);
int firstDayOfWeek = days % 7;
printCalendar(month, year, firstDayOfWeek);
month++;
}while(month < 12);
system("pause");
}
//This function calculates the number of days passed from some start point year
int calcDaysSoFar( int year, int month)
{
int days;
//calculates basic number of days passed
days = (year - firstYear) * 365;
days += dayOffset;
days += daysPassed[month-1];
//add on the extra leapdays for past years
for (int count = firstLeapYear; count < year ; count +=4)
{
if (isLeapYear(count) )
{
days++;
}
}
//add leapday for this year if requested month is greater than Feb
if( month > 2 && isLeapYear(year) )
{
days++;
}
return days;
}
//This function checks whether a particular year is a leap year
bool isLeapYear( int year)
{
return (!(year%4)&&(year%100))||!(year%400);
}
//Prints Month and Year
//followed by the day headings
//followed by the numbers
void printCalendar(int month, int year, int firstDay)
{
cout << months[month-1] << "\t" << year << '\n';
cout << "Sun " << "Mon " << "Tue " << "Wed " << "Thu " << "Fri " << "Sat "<< '\n';
int count,offset;
offset= 1 - firstDay;
count = daysInMonth[month-1]; //get the number of days in this month
//adjust if dealing with February and year is leap year
if(isLeapYear(year) && (month==2) )
{
count++;
}
for (int x = offset; x <= count; x +=7)
{
for(int column= x; (column < x+7) && (column <= count); column++)
{
if (column <=0)
{
cout << setw(4) << left << setprecision(3) << " ";
}
else
{
cout << setw(4) << left << setprecision(3) << setfill(' ') << column;
}
}
cout << '\n';
}
cout << endl;
}
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string>
#include <sstream>
usingnamespace std;
//helper function declarations
int calcDaysSoFar( int year, int month );
bool isLeapYear( int year);
void printCalendar(int month /*1 = January*/, int year, int firstDay /* 0 = Sunday*/);
void printGridTop(int innerWidth);
void repeatChar(char ch, int numChar);
void drawDivider(int innerWidth);
void printGridBottom(int innerWidth);
//Globals
constint firstYear = 1800; //This is our start point
constint dayOffset = 3; //The first day of our start year may not be a Sunday ( in 1800 it was Wednesday)
constint firstLeapYear = 1804; //help to reduce how many leap years we have to check
string dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
string months[] = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
int daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31} ; //standard year
int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //per month per year
int main ()
{
int year, month;
int days;
cout<<"Please enter the number of the month and year to access calendar\n"<<endl;
cout<< "Please leave a space in between the month and year when you enter them: \n";
cin>>month>>year;
//do{
//Calculate how many days have passed since Jan 1 of Start year to start of requested year & month
days = calcDaysSoFar(year, month);
int firstDayOfWeek = days % 7;
printCalendar(month, year, firstDayOfWeek);
//month++;
//}while(month < 13); //need to be 13 because months are numbered 1 - 12
system("pause");
}
//This function calculates the number of days passed from some start point year
int calcDaysSoFar( int year, int month)
{
int days;
//calculates basic number of days passed
days = (year - firstYear) * 365;
days += dayOffset;
days += daysPassed[month-1];
//add on the extra leapdays for past years
for (int count = firstLeapYear; count < year ; count +=4)
{
if (isLeapYear(count) )
{
days++;
}
}
//add leapday for this year if requested month is greater than Feb
if( month > 2 && isLeapYear(year) )
{
days++;
}
return days;
}
//This function checks whether a particular year is a leap year
bool isLeapYear( int year)
{
return (!(year%4)&&(year%100))||!(year%400);
}
//Prints Month and Year
//followed by the day headings
//followed by the numbers
void printCalendar(int month, int year, int firstDay)
{
string dayHeader = string("Sun ") + char(186) + "Mon " + char(186) + "Tue " + char(186) +
"Wed " + char(186) + "Thu " + char(186) + "Fri " + char(186) + "Sat ";
int innerWidth = dayHeader.length();
stringstream sstream; //for converting the year to a string.
string tempStr;
cout << '\n';
printGridTop(innerWidth);
//print the month/year line
sstream << year;
tempStr = string(" ") + months[month-1] + string(" ") + sstream.str();
cout << char(186);
cout << tempStr;
repeatChar(' ', innerWidth - tempStr.length());
cout << char (186);
//Draw a dividing line
cout << '\n';
cout << char(204);
for (int count =0; count < 7; count ++)
{
repeatChar(char(205),4);
if( count != 6)
{
cout << char(203);
}
}
cout << char(185);
cout << '\n';
//print the day header line
cout << char(186);
cout << dayHeader;
//cout << "Sun " << "Mon " << "Tue " << "Wed " << "Thu " << "Fri " << "Sat ";//<< '\n';
cout << char(186) << '\n';
drawDivider(innerWidth); //draw a dividing horizontal line
int count,offset;
offset= 1 - firstDay;
count = daysInMonth[month-1]; //get the number of days in this month
//adjust if dealing with February and year is leap year
if(isLeapYear(year) && (month==2) )
{
count++;
}
//loop to fill in rows
int dayNum;
for (int x = offset; x <= count; x +=7)
{
cout << char(186);
for(dayNum= x; (dayNum < x+7) /*&& (column <= count)*/; dayNum++)
{
if ( dayNum <=0 || dayNum > count)
{
cout << setw(4) << left << setprecision(3) << " " << char(186);;
}
else
{
cout << setw(4) << left << setprecision(3) << setfill(' ') << dayNum << char(186);
}
}
cout << '\n';
if(dayNum <= count)
{
//draw a divider line after each row except the last
drawDivider(innerWidth);
}
}
printGridBottom(innerWidth);
}
//draws the top of the drid
void printGridTop(int innerWidth)
{
cout << char (201) ;
repeatChar((char)205,innerWidth);
cout << char (187);
cout << '\n';
}
//this just repeats a char a nomber of times
void repeatChar(char ch, int numChar)
{
string aStr(numChar, ch);
cout << aStr;
}
//draws a line between rows
void drawDivider(int innerWidth)
{
cout << char(204);
for (int count =0; count < 7; count ++)
{
repeatChar(char(205),4);
if( count != 6)
{
cout << char(206);
}
}
cout << char(185);
cout << '\n';
}
//draws the bottom line of the grid
void printGridBottom(int innerWidth)
{
cout << char(200);
for (int count =0; count < 7; count ++)
{
repeatChar(char(205),4);
if( count != 6)
{
cout << char(202);
}
}
cout << char(188);
cout << '\n';
}
If you want the loop back in then uncomment lines 48, 54 and 55.
The loop will print the calendar from the requested month up to the end of the year.
So if user enters - 4 2008 - it will print April up to December 2008.
Note that you should put in some checks to validate the month and year values entered by the user.