Calendar Code

I am trying to make a calendar at any given year where the user inputs the year they want(where the program will check if it's a leap year) and they would also input what day Jan 1 is. I am having trouble figure out the spacing for the later months. I'm having a hard time on how to come about this. I figure I would create a separate function for the spacing at the beginning of each month but that might mess up the spacing for the months after January.

[]code]


#include "stdafx.h"
#include <iostream>
using namespace std;

int printMonthCalender(int numOfDays, int startingDay);
int leapYearCheck(int year);
void printYearCalender(int year, int startingDay);
int main()
{
int numDays;
int typeDay;
int desiredYear;
int value;
cout << "Please enter the number of days in the month:" << endl;
cin >> numDays;
cout << "Please enter a number for the day of the week were first number starts:" << endl;
cin >> typeDay;
cout << "Please enter year:" << endl;
cin >> desiredYear;

//cout<<printMonthCalender(numDays,typeDay)<<endl;
printYearCalender(desiredYear, typeDay);
cin.ignore(2);
return 0;
}

int printMonthCalender(int numOfDays, int startingDay)
{
int countDays=0;
int startDay;
cout << "Mon" << "\t" << "Tue" << "\t" << "Wed" << "\t" << "Thr" << "\t" << "Fri" << "\t" << "Sat" << "\t" << "Sun" << endl;


//spacing for 1st week
for (startDay = 1; startDay <= startingDay - 1; startDay++)
{
cout << "\t";
}
//days in first week
for (int count = 1; count <= 8 - startingDay; count++)
{
cout << count << "\t";

}
cout << endl;
//loop for every week 6 # in the week ie 5-11, 12-18
for (int count = 1, countDays = 9 - startingDay; count <= numOfDays, countDays <= numOfDays; count++, countDays++)
{
if (count % 7 == 0)
{
cout << endl;
}
cout << countDays << "\t";
}

return countDays;
}

int leapYearCheck(int year)
{
int numOfDays;
//assuming user input is friendly and does not put year before Gregorian calendar
if ((year % 4) == 0)
{
if (year % 100 == 0 && year % 400 != 0)
{
numOfDays = 28;
//input feb as the days
}
else
{
numOfDays = 29;
//add extra to the numOfDays
}
}
else
{
numOfDays = 28;
}

return numOfDays;
}
void printYearCalender(int year, int startingDay)
{
int january;
int feburary;
//march, april, may, june, july, august, september, october, november, december;
int numOfDays;
cout << "January " << year << endl;
january = printMonthCalender(31, startingDay);
cout << endl;

//cant figure out spacing for the beginning of the months after jan

cout << "Feburary " << year << endl;
feburary = printMonthCalender(leapYearCheck(year), startingDay);
cout << endl;
//call function leapYearCheck
// leapYearCheck(year)
cout << "March " << year << endl;
march=printMonthCalender(31,startingDay)
cout << "April " << year << endl;
april=printMonthCalender(30,startingDay)
cout << "May " << year << endl;
may=printMonthCalender(31,startingDay)
cout << "June " << year << endl;
june=printMonthCalender(30,startingDay)
cout << "July " << year << endl;
july=printMonthCalender(31,startingDay)
cout << "August " << year << endl;
august=printMonthCalender(31,startingDay)
cout << "September " << year << endl;
september=printMonthCalender(30,startingDay)
cout << "October " << year << endl;
october=printMonthCalender(31,startingDay)
cout << "November " << year << endl;
november=printMonthCalender(30,startingDay)
cout << "December " << year << endl;
december=printMonthCalender(31,startingDay)
}

[/code
If your problem is only the spacing between columns in output, you could try std::setw() and the other manipulators:
http://en.cppreference.com/w/cpp/io/manip

Anyway, the compiler complains for a number of errors in you code and perhaps you should begin by managing them.
Here’re some hints:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>

int printMonthCalender(int numOfDays, int startingDay);
int leapYearCheck(int year);
void printYearCalender(int year, int startingDay);

int main()
{
    std::cout << "Please enter the number of days in the month: ";
    int numDays;
    std::cin >> numDays;
    std::cout << "Please enter a number for the day of the week were first number starts: ";
    int typeDay;
    std::cin >> typeDay;
    std::cout << "Please enter year: ";
    int desiredYear;
    std::cin >> desiredYear;

    // std::cout << printMonthCalender(numDays,typeDay) << '\n';
    printYearCalender(desiredYear, typeDay);
    std::cin.ignore(2);
    return 0;
}

int printMonthCalender(int numOfDays, int startingDay)
{
    std::cout << "Mon\tTue\tWed\tThr\tFri\tSat\tSun\n";

    // spacing for 1st week
    for (int startDay = 1; startDay < startingDay; startDay++) { std::cout << '\t'; }
    //days in first week
    for (int count = 1; count <= 8 - startingDay; count++) { std::cout << count << '\t'; }
    std::cout << '\n';

    //loop for every week 6 # in the week ie 5-11, 12-18
    int countDays = 9 - startingDay;
    for (int count = 1;
            count <= numOfDays && countDays <= numOfDays;
            count++, countDays++) {
        if (count % 7 == 0) { std::cout << '\n'; }
        std::cout << countDays << '\t';
    }

    return countDays;
}

int leapYearCheck(int year)
{
    int numOfDays;
    // assuming user input is friendly and does not put year before Gregorian calendar
    if ((year % 4) == 0)
    {
        if (year % 100 == 0 && year % 400 != 0) { numOfDays = 28; } // February
        else                                    { numOfDays = 29; }
    }
    else
    {
        numOfDays = 28;
    }

    return numOfDays;
}

void printYearCalender(int year, int startingDay)
{
    // march, april, may, june, july, august, september, october, november, december;
    int numOfDays;
    std::cout << "January " << year << '\n';
    int january = printMonthCalender(31, startingDay);
    std::cout << '\n';

    //can't figure out spacing for the beginning of the months after jan
    std::cout << "Feburary " << year << '\n';
    int feburary = printMonthCalender(leapYearCheck(year), startingDay);
    std::cout << '\n';
    // call function leapYearCheck
    // leapYearCheck(year)
    std::cout << "March " << year << '\n';
    int march = printMonthCalender(31, startingDay);
    std::cout << "April " << year << '\n';
    int april = printMonthCalender(30, startingDay);
    std::cout << "May " << year << '\n';
    int may = printMonthCalender(31, startingDay);
    std::cout << "June " << year << '\n';
    int june = printMonthCalender(30, startingDay);
    std::cout << "July " << year << '\n';
    int july = printMonthCalender(31, startingDay);
    std::cout << "August " << year << '\n';
    int august = printMonthCalender(31, startingDay);
    std::cout << "September " << year << '\n';
    int september = printMonthCalender(30, startingDay);
    std::cout << "October " << year << '\n';
    int october = printMonthCalender(31, startingDay);
    std::cout << "November " << year << '\n';
    int november = printMonthCalender(30, startingDay);
    std::cout << "December " << year << '\n';
    int december = printMonthCalender(31, startingDay);
}

Topic archived. No new replies allowed.