Help With Date Calculator?

Hello, I'm trying to create a date calculator in C++, though am running into quite a few issues, with my braces and primary expressions with the arrays in the parameters. Can anyone assist?

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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <iostream>
#include <string>
#include <array>

using namespace std;

//arrays needed
string months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
int monthCodes[]={        6,         2,      2,      5,    0,     3,     5,       1,          4,        6,         2,         4};

string days[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int dayCode[]={       0,       1,        2,          3,         4,       5,         6};

//public variables

int monthNum;
int dayNum;
int yearNum;
int dayCodeReal;
//functions

bool isValidMonth(int);
bool isValidDay(int, int, int);
bool isLeapYear(int, int[]);
int getDayCode(int);
int getMonthCode(int);
int getYearCode(int);
int getDayCalc(int, int, int,int[]);
string getDay(int);
string getMonth(int,int[], string[]);
void resetValues(int[]);

int main()
{
	cout<<"Enter your month number. (1, 2, 3, etc.)"<<endl;
	cin>>monthNum;
	isValidMonth(monthNum);
	
	cout<<"Enter the day of the month."<<endl;
	cin>>dayNum;
	
	cout<<"Enter the year."<<endl;
	cin>>yearNum;
	
	isLeapYear(yearNum, monthCodes[]);
	
	//passes the month, day and year provided by user to the method to test if the day and month match to make a valid day.
	isValidDay(monthNum,dayNum, yearNum);
	
	//sets the return values of these functions to new variables
	int dayCode = getDayCode(dayNum);
	int monthCode = getMonthCode(monthNum);
	int yearCode = getYearCode(yearNum);
	
	//I'm not sure why I added these, I could just give the variables I declared above instead of calling the function again, lol.
	cout<<"Day code is: "<<getDayCode(dayNum)<<endl;
	cout<<"Month code is: "<<getMonthCode(monthNum)<<endl;
	cout<<"Year code is: "<<getYearCode(yearNum)<<endl;
	
	//makes a variable that actually GETS the real day code, and passes the following parameters to that function.
	dayCodeReal = getDayCalc(monthCode, dayCode, yearCode, dayCode[]);
	
	//prints out the results. Needs work. I keep getting primary-expression errors before the ] token. What does this mean?
	cout<<"Your date is: "<<getMonth(monthCode, monthCodes[],months[])<<" "<<dayNum<<", "<<yearNum<<" and the day is: "<<getDay(dayCodeReal)<<endl;
	
	resetValues(monthCodes[]);
	
	system("Pause");
	return 0;
}

//tests to see if the month is actually valid. Simple enough.
bool isValidMonth(int month)
{
	if(month < 1 || month > 12)
		return false;
	else
		return true;
}

//tests to see if the day, year and month together all work to create a valid date that actually exists.
bool isValidDay(int month, int day, int year)
{
	//if the month code is 1, this is February.
	if(month == monthCodes[1])
	{
		//tests to see if the day is less than 1 or greater than 28. False if so OR if the month is February and is NOT a leap year, then is also false.
		else if((day < 1 || day > 28) || ((month == monthCodes[1]) && (year%4 != 0)))
		{
				return false;
			//if the month is february, and the year is a leap year, the days can be up to 29. Returns false if otherwise.
			else
			{
				if((month == monthCodes[1]) && (year%4 == 0)) //defines a leap year
				{
					else if(day < 1 || day > 29)
					{
							return false;
						else
							return true;
					}
				}
			}
		}
	}
}
	//above is the algorithm for if the month is only February.
	
	//tests to see if the day entered is less than 1 or greater than 31. returns false. else, continues
	else if(day < 1 || day > 31)
	{
			return false;
		else
		{
			//I am getting a ton of errors with my braces here, can someone clean them up?
			//Anyway, if the month only has 30 days, this is the test for it.
			else if(month % 2 == 0)
			{
				else if(day < 1 || day > 30)
				{
						return false;
					else
					{
						//With the exception of this, which MAY need to be placed above
						//the day tester because it will fall out of the test if  the day is less
						//than 30 but the month is July.(?) This is complicating me XD
						else if(month == monthCode[6] || month == monthCode[7])
						{
							else if(day < 1 || day > 31)
							{
									return false;
								else 
									return true;
							}
						}
					}
				}
			}	
		}
	}
}
//returns the day code
int getDayCode(int day)
{
	return day%7;
}

//Can someone simpify this...? It seems like there's an easier way to do this. Codes 
//were provided online. Simply returns the code of the month when the day is provided.
int getMonthCode(int month)
{
	if(monthNum == 1)
		return 6;
	else if(monthNum == 2)
		return 2;
	else if(monthNum == 3)
		return 2;
	else if(monthNum == 4)
		return 5;
	else if(monthNum == 5)
		return 0;
	else if(monthNum == 6)
		return 3;
	else if(monthNum == 7)
		return 5;
	else if(monthNum == 8)
		return 1;
	else if(monthNum == 9)
		return 4;
	else if(monthNum == 10)
		return 6;
	else if(monthNum == 11)
		return 2;
	else if(monthNum == 12)
		return 4;
}
//Returns year code whether the year is leap year, or years 1, 2 or 3 past it.
int getYearCode(int year)
{
	if(year%4 == 0)
		return 0;
	else if(year %4 == 1)
		return 1;
	else if(year %4 == 2)
		return 2;
	else if(year %4 == 3)
		return 3;
	else
		return -1;
}
//Tests to see the day Code. Calculation was provided online, but may not be fully functional.
int getDayCalc(int monthCode, int dayCode, int yearCode, int dayCode[])
{
	int dayNumber = monthCode + dayCode + yearCode;
	dayNumber -= 7;
	for(int i = 0; i<=7; i++)
		if(dayNumber == dayCode[i])
			return dayCode[i];
}
//Again, seems like there's an easier way to do this.
//Simply returns the day given from the array.
string getDay(int realDay)
{
	switch(realDay)
	case 0: return "Sunday";
	case 1: return "Monday";
	case 2: return "Tuesday";
	case 3: return "Wednesday";
	case 4: return "Thursday";
	case 5: return "Friday";
	case 6: return "Saturday";
	default: return -1;
}

//returns the actual month from the array above. Are Parallel arrays allowed in C++?
string getMonth(int monthCode, int monthCodes[],string months[])
{
	for(int i = 0; i<13; i++)
	{
		if(monthCode == monthCodes[i])
			return months[i];
	}
}

bool isLeapYear(int year, int monthCodes[])
{
	if(year %4 == 0)
	{
		monthCodes[0] = 5;
		monthCodes[1] = 1;
		return true;
	}
	else
		return false;
}

void resetValues(int monthCodes[])
{
	monthCodes[0] = 6;
	monthCodes[1] = 2;
}
Last edited on
Can you be more specific on what kind of problems your having?
Yeah, in lines 45,61,64,66, I'm getting an expected primary-expression before ']' token error.

In lines 88,92,96 and 99 I'm getting the expected '}' before 'else' error.

And at 103, I'm getting expected declaration before '}' token.
Can anyone help...?
Are you wanting it to skip the rest of the if statements if the first is true?



1
2
3
4
5
6
7
//tests to see if the day, year and month together all work to create a valid date that actually exists.
bool isValidDay(int month, int day, int year)
{
	//if the month code is 1, this is February.
	if(month == monthCodes[1])
	{    /* What happens here if month is = to feb. */
             /*I can only assume you want it to test the day if it is true? */

Last edited on
Im not sure but Im curious; are you trying to say


1
2
3
4

if(month ==month[1]){
   // test for the day
    }
If the month is february, I need the test to continue. If it's not February, I need it to jump down to the else if statement below it. else if(day < 1 || day > 31)
To start I want to be forward with you. Im having a hard time all the if and else if ...Im just not use to coding with so many in order like that so it keeps throwing me off. Sorry.


But one thing I think is causing the ']' it seems to looking for a value in the brackets. Maybe an "Array size" I might be wrong but Im pretty sure that is the problem there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if(month == monthCodes[1]) /*if this is true it skips all of the following for sure */
	{

		//tests to see if the day is less than 1 or greater than 28. False if so OR if the month is February and is NOT a leap year, then is also false.
		else if((day < 1 || day > 28) || ((month == monthCodes[1]) && (year%4 != 0)))
		{
				return false;
			//if the month is february, and the year is a leap year, the days can be up to 29. Returns false if otherwise.
			else
			{
				if((month == monthCodes[1]) && (year%4 == 0)) //defines a leap year
				{
					else if(day < 1 || day > 29)
					{
							return false;
						else
							return true;
					}
				}
			}
		}
	}
Last edited on
Some of your if statements seem to repeat themselves.
1
2
3
4
5
6
7
8
9
10
if(month == monthCodes[1]) 
else if((day < 1 || day > 28) || ((month == monthCodes[1]) && (year%4 != 0)))
if((month == monthCodes[1]) && (year%4 == 0)) 

//You should be eliminating some of the conditions on your way down, or at least that is what I would do. 
//For instance :

if(month == monthCodes[1]) //if this is true you can put any, if, statements or, else if, statements in it (the first if statement) brackets
//and you shouldn't have to retest for that again. Does that make since?
 

I don't think the sequence of" if, else, if else " will work the way you have it.



1
2
3
4
5
6
7
8
else if((day < 1 || day > 28) || ((month == monthCodes[1]) && (year%4 != 0)))
		{
				return false;
			//if the month is february, and the year is a leap year, the days can be up to 29. Returns false if otherwise.
			else
			{
				if((month == monthCodes[1]) && (year%4 == 0)) //defines a leap year
				{


For now I would recommend trying to eliminate some of your condition statements..

1
2
3
4
5
6
7
8
9
10
if((month == monthCodes[1]) && (year%4 == 0))&&(day < 1 || day > 29) //defines a leap year
				{ return false;}
else if(month == monthCodes[1])&&(day < 1 && day > 28)  && (year%4 != 0)){  
      return false; }

else if( condition ){
    blah blah blah
}
else
do this
Last edited on
else should always be the final statement if your going to use an else. Another way to say that is you choose to use else don't use it until the end, never in the middle.


But one thing I think is causing this error ']' would be the compiler looking for an an "Array size" I might be wrong but Im pretty sure that is the problem there.

Had to correct my comment from earlier.. Sorry for all the typos and multi comments. I keep looking at it and see different things that stand out to me.
Last edited on
closed account (E0p9LyTq)
You already have arrays set up, in two functions (getMonthCode and getDay) there is no need for all that if testing. One if test is all that is required, to test if the input is proper.

1
2
3
4
5
6
7
8
int getMonthCode(int month)
{
   if (month < 1 || month > 12)
   {
      return -1; // error!
   }
   return monthCodes[month - 1];
}


FYI, look at your getMonthCode again. You have month as an input variable, but you are testing against monthNum!

1
2
3
4
5
6
7
8
9
10
11
string getDay(int realDay)
{
   if (realDay < 0 || realDay > 6)
   {
      return "error";  // return a string, not a numeric -1!
   }
   else
   {
      return days[realDay];
   }
}
Last edited on
closed account (E0p9LyTq)
Something else about your 3 functions, getMonth, isLeapYear and resetValues. You seem to pass in your global variables and they get copied to new, local scope variables. Are you wanting to work with your global variables or copies of your globals?

If you want to work with your globals then the functions could read:

1
2
3
4
5
string getMonth(int monthCode)

bool isLeapYear(int year)

void resetValues()


Your function prototypes will need to be updated if you make this change.

Ooops, I originally made a typo in resetValues, corrected.
Last edited on
Topic archived. No new replies allowed.