Ok we have been creating this calendar in our intro to programming class for about a week or so, and right now I am stuck on getting the start day of each month. I was thinking it should be total days (days in month + total days from 1800 - year), plus 3 (the day that january 1800 started on), mod 7, but i cant get it to work for the life of me, any help would be dearly appreciated.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
bool isLeapYear(int yr);
int getYear();
int getMonth();
int getNoOfDaysInMonth(int mo, int yr);
int getTotalNoOfDays(int mo, int yr);
string getMonthName(int mo);
string getStartDay(int TotDays);

int main()
{
    int mo;
    int numOfDays;
    int yr;
    int TotDays;
    string moName;
    string dname;
    // processing area
    mo=getMonth();
    yr=getYear();
    //leapYear = isLeapYear(yr);
    numOfDays = getNoOfDaysInMonth( mo, yr);
    moName = getMonthName(mo);
    TotDays = getTotalNoOfDays(mo,yr);
    dname = getStartDay(TotDays);
    
    cout <<"Name of month is = " << moName << '\n'; 
    cout <<"Number of Days = " << numOfDays << '\n';
    cout <<"Total number of days = " << TotDays <<'\n';
    cout <<"The first day in the month of " << moName << " is " << dname << " . " << '\n' ;
    char c;
    cout << "Please press any key to continue." << '\n';
    cin >> c;

return 0;
}
bool isLeapYear(int yr)
{
    bool leapYear;
    leapYear = ((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0);
    return leapYear;
}

int getNoOfDaysInMonth(int mo, int yr)
{
    if (mo==1||mo==3||mo==5||mo==7|| 
        mo==8||mo==10||mo==12)
        return 31;
    if (mo==9||mo==4||mo==6||mo==11)
        return 30;
    if (isLeapYear(yr))
        return 29;
    else
        return 28;
}
int getYear()
{
    int yr;
    cout << "Plese enter a year." << '\n';
    cin >> yr;
    return yr;
}
int getMonth()
{
    int mo;
    cout << "Please enter a month." << '\n';
    cin >> mo;
    return mo;
}

int getTotalNoOfDays(int mo, int yr)
{
    int j;
    int totDays=0;
    for (j=1800; j <= yr; j++)
    {
        if (isLeapYear(yr))
            totDays = totDays + 366;
        else
            totDays = totDays + 365;
    }
    for (j=1; j <= mo; j++)
    {
        totDays = totDays + getNoOfDaysInMonth(mo,yr);
    }
    return totDays; 
}
string getMonthName(int mo)
{
    switch (mo)
    {
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
    }
    return "You must enter a number between 1 and 12.";
}
string getStartDay(int TotDays)
{
    int wday;
    string dname;

    wday = (TotDays + 3) % 7;
    if (wday == 2)
    {
        dname = "Sunday";
    }
    if (wday == 3)
    {
        dname = "Monday";
    }
    if (wday == 4)
    {
        dname = "Tuesday";
    }
    if (wday == 5)
    {
        dname = "Wednesday";
    }
    if (wday == 6)
    {
        dname = "Thursday";
    }
    if (wday == 0)
    {
        dname = "Friday";
    }
    if (wday == 1)
    {
        dname = "Saturday";
    }

    return dname ;
}

You add an extra year to the result, plus the number of days in the selected month. January 1800 comes out as 396, or 365+31.

ok, so do I have to check if the year is a leap year? when i add a year to the result?

I actually didn't test the leap year code. What's happening is you're adding the last year before it's over, skewing the result by that number of days. Ditto with the month. If you change the <= in getTotalNoOfDays to just a < it'll fix that problem. There's another problem in that you call getNoOfDaysInMonth with mo each time. That'll just call it with the same month over and over; you should be using j instead. It's like adding the days in April several times when you want to add January, February, and March instead.

How would I write it so that it doesnt keep calling the same month? would that be another loop?

Nope, you've got the loop. Remember, j is going from 1 to the current month. So if you pass j, it'll look up the days in the 1st, 2nd, 3rd, ... nth month. :icon_wink:

ok so i tried changing it to:

int getTotalNoOfDays(int mo, int yr)
{
    int j;
    int totDays=0;
    for (j=1800; j < yr; j++)
    {
        if (isLeapYear(yr))
            totDays = totDays + 366;
        else
            totDays = totDays + 365;
    }
    for (j=1; j < mo; j ++)
    {
        totDays = totDays + getNoOfDaysInMonth(mo=j,yr);
    }
    return totDays; 
}

still cant get it to start on the right day though

Is it getting the day counts right though? Maybe if you just rearrange the values for the days it'll straighten out...

I dont think so, because right now I am checking it with the calendar in my phone. It is getting May 2007 right, but when i try another value, like Sep 1986, it is off by one day. Here is the total program as it is now, pretty much finished after I get it to get the startday right.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
bool isLeapYear(int yr);
int getYear();
int getMonth();
int getNoOfDaysInMonth(int mo, int yr);
int getTotalNoOfDays(int mo, int yr);
string getMonthName(int mo);
int getStartDay(int TotDays, int mo, int yr);
void pntMonthName (int mo, int yr);
void printMonth (int wday, int mo, int yr);
string dayName (int wday);

int main()
{
    int mo;
    int numOfDays;
    int yr;
    int TotDays;
    int wday;
    string moName;
    string dname;
    // processing area
    mo=getMonth();
    yr=getYear();
    //leapYear = isLeapYear(yr);
    numOfDays = getNoOfDaysInMonth( mo, yr);
    moName = getMonthName(mo);
    TotDays = getTotalNoOfDays(mo,yr);
    wday = getStartDay(TotDays, yr, mo);
    dname = dayName(wday);
    pntMonthName(mo, yr);
    printMonth (wday, mo, yr);
    
    cout <<"Name of month is = " << moName << '\n'; 
    cout <<"Number of Days = " << numOfDays << '\n';
    cout <<"Total number of days = " << TotDays <<'\n';
    cout <<"The first day in the month of " << moName << " is " << dname << " . " << '\n' ;
    char c;
    cout << "Please press any key to continue." << '\n';
    cin >> c;

return 0;
}
bool isLeapYear(int yr)
{
    bool leapYear;
    leapYear = ((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0);
    return leapYear;
}

int getNoOfDaysInMonth(int mo, int yr)
{
    if (mo==1||mo==3||mo==5||mo==7|| 
        mo==8||mo==10||mo==12)
        return 31;
    if (mo==9||mo==4||mo==6||mo==11)
        return 30;
    if (isLeapYear(yr))
        return 29;
    else
        return 28;
}
int getYear()
{
    int yr;
    cout << "Plese enter a year." << '\n';
    cin >> yr;
    return yr;
}
int getMonth()
{
    int mo;
    cout << "Please enter a month." << '\n';
    cin >> mo;
    return mo;
}

int getTotalNoOfDays(int mo, int yr)
{
    int j;
    int totDays=0;
    for (j=1800; j < yr; j++)
    {
        if (isLeapYear(yr))
            totDays = totDays + 366;
        else
            totDays = totDays + 365;
    }
    for (j=1; j < mo; j ++)
    {
        totDays = totDays + getNoOfDaysInMonth(mo=j,yr);
    }
    return totDays; 
}
string getMonthName(int mo)
{
    switch (mo)
    {
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
    }
    return "You must enter a number between 1 and 12.";
}

string dayName (int wday)
{
    string dname;

        if (wday == 0)
    {
        dname = "Sunday";
    }
    if (wday == 1)
    {
        dname = "Monday";
    }
    if (wday == 2)
    {
        dname = "Tuesday";
    }
    if (wday == 3)
    {
        dname = "Wednesday";
    }
    if (wday == 4)
    {
        dname = "Thursday";
    }
    if (wday == 5)
    {
        dname = "Friday";
    }
    if (wday == 6)
    {
        dname = "Saturday";
    }
    return dname;
}
int getStartDay(int TotDays, int mo, int yr)
{
    int wday ;
    
    
    wday = TotDays + 2 + getNoOfDaysInMonth (mo,yr);


    wday = wday % 7;

    
    return wday ;

}

void pntMonthName (int mo, int yr)
{
    cout << "\t \t \t " << getMonthName(mo) << " , " << yr << '\n' ;

}

void printMonth (int wday, int mo, int yr)

{
int skipDay, dayCount;

cout<<"Sun Mon Tue Wed Thu Fri Sat\n";
cout<<"--- --- --- --- --- --- ---\n";

for(skipDay = 0;skipDay < wday; skipDay++)
cout<<"    ";

int weekDay = wday;
for (dayCount =1; dayCount <= getNoOfDaysInMonth(mo, yr); dayCount++)
{
if (weekDay>6)
{
cout<<endl;
weekDay =1;
}

else
weekDay++;
cout<<setw(3) <<dayCount<<" ";
}

cout<<"\n--- --- --- --- --- --- ---\n";

}

Ok, finally made a few tweaks and get everything working correctly. Heres the working code.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
bool isLeapYear(int yr);
int getYear();
int getMonth();
int getNoOfDaysInMonth(int mo, int yr);
int getTotalNoOfDays(int mo, int yr);
string getMonthName(int mo);
int getStartDay(int totDays, int mo, int yr);
void pntMonthName (int mo, int yr);
void printMonth (int wday, int mo, int yr);
string dayName (int wday);
int main() // Calls functions to create calendar for specified month and year.
{
int mo, numOfDays, yr, totDays, wday;
string moName, dname;

mo=getMonth();
yr=getYear();
numOfDays = getNoOfDaysInMonth( mo, yr);
moName = getMonthName(mo);
totDays = getTotalNoOfDays(mo,yr);
wday = getStartDay(totDays, yr, mo);
dname = dayName(wday);
pntMonthName(mo, yr);
printMonth (wday, mo, yr);

cout <<"The name of the month is " << moName << '\n'; 
cout <<"The number of days in the month = " << numOfDays << '\n';
cout <<"Total number of days are " << totDays <<'\n';
cout <<"The first day in the month of " << moName << " is " << dname << " . " << '\n' ;
char c;
cout << "Please press any key to continue." << '\n';
cin >> c;
return 0;
}
bool isLeapYear(int yr) // Determines if the year is a leap year or not
{
bool leapYear;
leapYear = ((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0);
return leapYear;
}
int getNoOfDaysInMonth(int mo, int yr)// Calculates the number of days in each month
{
if (mo==1||mo==3||mo==5||mo==7|| 
mo==8||mo==10||mo==12)
return 31;
if (mo==9||mo==4||mo==6||mo==11)
return 30;
if (isLeapYear(yr)) 
return 29;
else
return 28;
}
int getYear() // Gets input for the year of the calendar.
{
int yr;
cout << "Please enter a year. (ex. 2001) " << '\n';
cin >> yr;
return yr;
}
int getMonth() // Gets input for month for the calendar.
{
int mo;
cout << "Please enter a month from 1 - 12." << '\n';
cin >> mo;
return mo;
}
int getTotalNoOfDays(int mo, int yr) // Gets total number of days from specified year, and days from beginning of year to specified month.
{
int totDays=0;

for (int d=1800; d < yr; d++)
{
if (isLeapYear(d))
totDays = totDays + 366;
else
totDays = totDays + 365;

}

for (int d=1; d < mo; d++)
{
totDays = totDays + getNoOfDaysInMonth(d, yr);
}
return totDays; 
}
string getMonthName(int mo) // Changes number of month to english name of month
{
switch (mo)
{
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
}
return "You must enter a number between 1 and 12.";
}
string dayName (int wday) // Gets english name of the day of the week
{
string dname;
if (wday == 0)
{
dname = "Sunday";
}
if (wday == 1)
{
dname = "Monday";
}
if (wday == 2)
{
dname = "Tuesday";
}
if (wday == 3)
{
dname = "Wednesday";
}
if (wday == 4)
{
dname = "Thursday";
}
if (wday == 5)
{
dname = "Friday";
}
if (wday == 6)
{
dname = "Saturday";
}
return dname;
}
int getStartDay(int totDays, int mo, int yr) // Determines start day for calendar
{
int wday ;


wday = totDays + 3 ;
 
wday = wday % 7;

return wday ;
}
void pntMonthName (int mo, int yr) // Prints month and year of calendar (month,year)
{
cout << "\t \t \t " << getMonthName(mo) << " , " << yr << '\n' ;
}
void printMonth (int wday, int mo, int yr) // Prints calendar
{
int skipDay, dayCount;
cout<<"Sun Mon Tue Wed Thu Fri Sat\n";
cout<<"--- --- --- --- --- --- ---\n";
for(skipDay = 0;skipDay < wday; skipDay++)
cout<<" ";
int weekDay = wday;
for (dayCount =1; dayCount <= getNoOfDaysInMonth(mo, yr); dayCount++)
{
if (weekDay>6)
{
cout<<endl;
weekDay =1;
}
else
weekDay++;
cout<<setw(3) <<dayCount<<" ";
}
cout<<"\n--- --- --- --- --- --- ---\n";
}

how do I create a calender program using c++?

Antwane.....welcome aboard.I reccommend taking a look at the Rules & FAQ for the daniweb forum. Please do not re-open old threads....i'm pretty sure you could get some help from the 2 previous pages. Again ,welcome. If you are doing this for a school assignment, If you have specific problems when trying to do a program...please post the source code on your own thread along with what you think may be wrong. Also post the errors you get, so we can take a look and help you. Additionally, we don't do homework for people...we provide assistance.

how do I do that , that is submit whatnot on my on thread ? My calender is almost finished, I h=just have a few bugs in my code will submit my source code(.cpp) but again how do I proceed doing so and must warn you my code is Visual Express edition of C++

how do I create a calender program using c++?

If you were "smarter than a 5th grader" then you could just simply "copy" from the previous poster. But be aware that if he is also wrong you will have to post "I Antwane am NOT smarter than a 5th grader." and flunk out of school.

[edit]For those not in USA "Are You Smarter Than A 5th Grader" is a popular current TV game show where the contestent can win up to $1,000,000.00 for answering ten 1st through 5th grade questions.[/edit]

how do I do that , that is submit whatnot on my on thread ?

Just hit the Start New Thread button here.

maybe this web page can help you with your formula
http://www.jimloy.com/math/day-week.htm

Ok we have been creating this calendar in our intro to programming class for about a week or so, and right now I am stuck on getting the start day of each month. I was thinking it should be total days (days in month + total days from 1800 - year), plus 3 (the day that january 1800 started on), mod 7, but i cant get it to work for the life of me, any help would be dearly appreciated.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
bool isLeapYear(int yr);
int getYear();
int getMonth();
int getNoOfDaysInMonth(int mo, int yr);
int getTotalNoOfDays(int mo, int yr);
string getMonthName(int mo);
string getStartDay(int TotDays);

int main()
{
    int mo;
    int numOfDays;
    int yr;
    int TotDays;
    string moName;
    string dname;
    // processing area
    mo=getMonth();
    yr=getYear();
    //leapYear = isLeapYear(yr);
    numOfDays = getNoOfDaysInMonth( mo, yr);
    moName = getMonthName(mo);
    TotDays = getTotalNoOfDays(mo,yr);
    dname = getStartDay(TotDays);
    
    cout <<"Name of month is = " << moName << '\n'; 
    cout <<"Number of Days = " << numOfDays << '\n';
    cout <<"Total number of days = " << TotDays <<'\n';
    cout <<"The first day in the month of " << moName << " is " << dname << " . " << '\n' ;
    char c;
    cout << "Please press any key to continue." << '\n';
    cin >> c;

return 0;
}
bool isLeapYear(int yr)
{
    bool leapYear;
    leapYear = ((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0);
    return leapYear;
}

int getNoOfDaysInMonth(int mo, int yr)
{
    if (mo==1||mo==3||mo==5||mo==7|| 
        mo==8||mo==10||mo==12)
        return 31;
    if (mo==9||mo==4||mo==6||mo==11)
        return 30;
    if (isLeapYear(yr))
        return 29;
    else
        return 28;
}
int getYear()
{
    int yr;
    cout << "Plese enter a year." << '\n';
    cin >> yr;
    return yr;
}
int getMonth()
{
    int mo;
    cout << "Please enter a month." << '\n';
    cin >> mo;
    return mo;
}

int getTotalNoOfDays(int mo, int yr)
{
    int j;
    int totDays=0;
    for (j=1800; j <= yr; j++)
    {
        if (isLeapYear(yr))
            totDays = totDays + 366;
        else
            totDays = totDays + 365;
    }
    for (j=1; j <= mo; j++)
    {
        totDays = totDays + getNoOfDaysInMonth(mo,yr);
    }
    return totDays; 
}
string getMonthName(int mo)
{
    switch (mo)
    {
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
    }
    return "You must enter a number between 1 and 12.";
}
string getStartDay(int TotDays)
{
    int wday;
    string dname;

    wday = (TotDays + 3) % 7;
    if (wday == 2)
    {
        dname = "Sunday";
    }
    if (wday == 3)
    {
        dname = "Monday";
    }
    if (wday == 4)
    {
        dname = "Tuesday";
    }
    if (wday == 5)
    {
        dname = "Wednesday";
    }
    if (wday == 6)
    {
        dname = "Thursday";
    }
    if (wday == 0)
    {
        dname = "Friday";
    }
    if (wday == 1)
    {
        dname = "Saturday";
    }

    return dname ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.