How to Format a Calendar Program

I'm having some trouble with my Calendar program. Everything for the most part is working correctly, except I'm not sure how to make it so the calendar isn't so spaced out when it is outputted to the user. I'm currently using \t to space it all out correctly but it doesn't look exactly like how my professor would like it to look...

Just as a disclaimer: I'm relatively new to programming so a lot of things that I've done, probably don't make any sense, or my professor wants it to be that way.
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Calendar
/* This program stores a calendar month of dates (numbers starting at 1) in a two-dimensional array.
Some months have 31 days, others 30, and February has 28 (29 if it’s a leap year, which is a year divisible by 4). */

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

#define TRUE    1
#define FALSE   0


const int SIZE = 12;
const int col = 7;
int days_in_month[SIZE]={31,0,31,30,31,30,31,31,30,31,30,31};

const int rows = 5; 

int getDaysInMonth(string, string [], int []);
void determineLeapYear(int);
void showCal(string, int, int [][col], int, int [], string [], string);
void validateDay(string, string []);
void validateMonth(string, string []);


int month[rows][col] = 
	{
	{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, 0, 0, 0, 0}
	};
string dayOfWeek[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
	string monthName[SIZE] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	
	int year[4] = {2016, 2017, 2018, 2019};
	int dayNumber[7] = {1, 2, 3, 4, 5, 6, 7};
int main()

{
	string choice;
	string GET_DAY;
	int GET_YEAR;

	cout << "Enter the name of a Month> ";
	getline(cin, choice);

	validateMonth(choice, monthName);

    while (choice != "Stop" && choice != "stop")
    {
		cout << "\nEnter the year> "; 
		cin >> GET_YEAR;
		cout << endl;
		determineLeapYear(GET_YEAR);
	
		cout << "\nEnter the day the month starts> "; 
		cin >> GET_DAY;
		validateDay(GET_DAY, dayOfWeek);
		cout << "\n";
		showCal(choice, GET_YEAR, month, rows, dayNumber, monthName, GET_DAY);

		cout << "\nEnter the name of a month or enter stop to quit>";
        cin.ignore();
        getline(cin, choice);
		validateMonth(choice, monthName);
	
	}
	     
	system("pause");
	return 0; 
}

//*************************************************************************************
// Definition of the function validateMonth which test the input of the user for GET_DAY*
//*************************************************************************************
void validateMonth(string choice, string monthName []){

	int found = 0;
		int a = 0;
    for (a = 0; a < 7 && !found; a++)
    {
        if (monthName[a] == choice || choice == "stop" || choice == "Stop")
        {
            found = 1;            
        }
    }   // end for loop
    if (found)
    {
        cout << endl;
    }
    else
    {
        cout << "ERROR: The day " << choice << " was not found...please again!" << endl << endl;
    }


}

//*************************************************************************************
// Definition of the function validateDay which test the input of the user for GET_DAY*
//*************************************************************************************
void validateDay(string GET_DAY, string dayOfWeek []){

	int found = 0;
		int a = 0;
    for (a = 0; a < 7 && !found; a++)
    {
        if (dayOfWeek[a] == GET_DAY)
        {
            found = 1;            
        }
    }   // end for loop
    if (found)
    {
        cout << endl;
    }
    else
    {
        cout << "ERROR: The day " << GET_DAY << " was not found...please again!" << endl << endl;
    }


}
//***********************************************************************************
// Definition of the function determineLeapYear which determines if it is leap year * 
// or not in order to provide the right number of days for February.                *
//***********************************************************************************


void determineLeapYear(int GET_YEAR)
{

	if(GET_YEAR% 4 == FALSE && GET_YEAR%100 != FALSE ||GET_YEAR%400 == FALSE)
	{
		days_in_month[1] = 29;
	}
	else
	{
		days_in_month[1] = 28;
		
	}
}





//***********************************************************************************
// Definition of the function showCal which formats the calendar of each month     *
// depending on which day the month starts on.                                      *
//***********************************************************************************


void showCal(string choice, int GET_YEAR, int month[][col], int rows, int dayNumber[], string monthName [], string GET_DAY)
{
int days;
	int numDays = 0;
	int start_day = 0;
	cout << "\t" << choice << "\t" << GET_YEAR << endl;
	for (int count = 0; count < SIZE; count++)
	{
		if (choice == monthName[count])
		{
			numDays += days_in_month[count];
		}
	}
	for (int c = 0; c < 7; c++)
	{
		if (GET_DAY == dayOfWeek[c])
		{start_day += dayNumber[c];}	
	}
	//this loop sets the position for the starting day
	
      for (int i = 0; i < start_day; i++) 
	  { 
		  cout << "\t"; 
      }
      //This loop displays the days 
      for (month[rows][col] = (1+start_day); month[rows][col] <= (numDays+start_day); month[rows][col]++) 
      { 
         cout << (month[rows][col]-start_day) << "\t"; 
		 if( month[rows][col]%7 == 0){
            cout << endl; 
			
         } 
      }

	  cout << endl;

	}
Last edited on
it doesn't look exactly like how my professor would like it to look

Are we expected to know what that means? Do you think we know who your professor is, and how he wants your output look?
I'm sorry mikeyboy you don't have to get so irritated. I simply would like an alternative to using \t....sorry I didn't ask that question exactly up to your standards.
Hey,

Sounds like you could use <iomanip>, setw() seems to be a viable alternative to \t if I understand it correctly: http://www.cplusplus.com/reference/iomanip/setw/
I'm sorry mikeyboy you don't have to get so irritated. I simply would like an alternative to using \t....sorry I didn't ask that question exactly up to your standards.

*Shrug* we can't read your mind. We can't possibly know what it is you're actually looking for if you don't tell us. Believe it or not, I was helping you, by trying to elicit more information from you about what you're looking for,

And hey, guess what - the moment you actually did that, someone was able to help you get what you want. Amazing how that works, huh?

So, this has been a productive thread for you - that's two things you've learned, for the price of one :)
Use std::setw() to set the width? http://en.cppreference.com/w/cpp/io/manip/setw

Something like this, perhaps (haven;t looked at the rest of the program):

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
void showCal(string choice, int GET_YEAR, int month[][col], int rows, int dayNumber[], string monthName [], string GET_DAY)
{
    int days;
    int numDays = 0;
    int start_day = 0;

    const int width = 4 ; // *** added ****

    // cout << "\t" << choice << "\t" << GET_YEAR << endl;
    cout << setw(width*3) << choice << setw(width*2) << GET_YEAR << "\n\n" ; // ****

    cout << "  Sun Mon Tue Wed Thu Fri Sat\n" ; // **** added

    for (int count = 0; count < SIZE; count++)
    {
        if (choice == monthName[count])
        {
            numDays += days_in_month[count];
        }
    }
    for (int c = 0; c < 7; c++)
    {
        if (GET_DAY == dayOfWeek[c])
        {
            start_day += dayNumber[c];
        }
    }
    //this loop sets the position for the starting day

    for (int i = 0; i < start_day; i++)
    {
        // cout << "\t";
        cout << setw(width) << ' ' ; // *****
    }
    //This loop displays the days
    for (month[rows][col] = (1+start_day); month[rows][col] <= (numDays+start_day); month[rows][col]++)
    {
        // cout << (month[rows][col]-start_day) << "\t";
        cout << setw(width) << month[rows][col]-start_day ; // *****
        if( month[rows][col]%7 == 0)
        {
            cout << endl;

        }
    }

    cout << endl;
}
> I'm sorry mikeyboy you don't have to get so irritated.

This is the internet, you will find all kinds of people. Just ignore the boors.
I got it! I tried using setw() earlier but I was adding it in the wrong place! Haha
Thank you for your help JLBorges!
Topic archived. No new replies allowed.