Getting a Weird error: Unhandled Exception Access vilation writing location

Hey guys. I am trying to write a program that takes the input from a user in the form of MM/DD/YYYY and figures out a date. I am building off a program I already wrote that individually accepts the month day and year and verifies it is a valid date (including leap year). I have tried a lot today and this is the closest I've come without syntax errors but now I have an error I don't know how to solve. It reads:

Unhandled exception at 0x00D374D6 in RMcAndrews_Week2_CISP232.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.

Here is my header:

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
//***************************************
//*CISP232: C++ Programming I		    *
//*Programming Exercises Chapter 11		*
//*Instructor: Dr. Loay Alnaji          *
//*Created By: Rob McAndrews            *
//*Date Created: Mar 10, 2013           *
//*										*
//*This class is for the program that	*
//*sets the date.  It checks that the	*
//*input is a valid date including Feb	*
//*29 being only valid on leap years.	*
//***************************************

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;


class dateType
{
public:
	bool isLeapYear(int year);
		//Function to check if year entered is leap year
		//returns 1 if it is, 0 if not.

	void setDate(int month, int day, int year);
		//Function to set the date.
		//The member variables dMonth, dDay, and dYear are set
		//according to the parameters.
		//Postcondition: dMonth = month; dDay = day;
		//				 dYear = year

	int getDay() const;
		//Function to return the day.
		//Postcondition: The value of dDay is returned.

	int getMonth() const;
		//Function to return the month.
		//Postcondition: The value of dMonth is returned.

	int getYear() const;
		//Function to return the year.
		//Postcondition: The value of dYear is returned.
		
	void printDate() const;
		//Function to output the date in the form mm-dd-yyyy.

	int findMonth(string date[9]);
		//function to get month out of date

	int findDay(string date[9]);
		//function to get day out of date

	int	findYear(string date[9]);
		//function to get year out of date
	
	
private:
	int dMonth;		//variable to store the month
	int dDay;		//variable to store the day
	int dYear;		//variable to store the year
	bool leapYear;	//variable to store leap year true/false
};

//this function finds the information entered by the user and returns the month
int dateType::findMonth(string date[9])
{
	string fMonth;
	int mm;
	fMonth = date[0] + date[1];
	cout << fMonth;
	mm = atoi(fMonth.c_str()); 
	return mm;
}

int dateType::findDay(string date[9])
{
	string fDay;
	int dd;
	fDay = date[3] + date[4];
	cout << fDay;
	dd = atoi(fDay.c_str());
	return dd;
}

int dateType::findYear(string date[9])
{
	string fYear;
	int yy;
	fYear = date[6] + date[7] + date[8] + date[9];
	cout << fYear;
	yy = atoi(fYear.c_str());
	return yy;
}

//this uses a formula to check for leap year and returns true (1) or false (0)
bool dateType::isLeapYear(int year)
{
	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
		leapYear = 1;
	else
		leapYear = 0;
	
	return leapYear;
}

void dateType::setDate(int month, int day, int year)
{
	//this checks to see if month is between 1 and 12
	if (month < 1 || month > 12)
	{	
		cout << "The month is invalid.  Please enter a number between 1 - 12: ";
		cin >> month;
	}

	dMonth = month;
	//this checks for February and applies special rules
	if (month == 2)
	{
		
		if (leapYear == 0)
		{
			while (day > 28)
			{
				cout << "The day is invalid for this month and year." << endl;
				cout << "Please enter a number between 1 - 28: ";
				cin >> day;
				cout << endl;
			}
		
		}
		if (leapYear == 1)
		{
			while (day > 29)
			{
				cout << "The day is invalid for this month.  Please enter a number between 1 - 29: ";
				cin >> day;
				cout << endl;
			}
		}
	}

	// this checks for months containing 31 days
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 
		|| month == 10 || month == 12)
	{
		while (day > 31)
		{
			cout << "The day is invalid.  Please enter a number between 1 - 31: ";
			cin >> day;
			cout << endl;
		}
	}	
	
	//this checks for months with 30 days
	if (month == 4 || month == 6 || month == 9 || month == 11)
	{
		while (day > 30)
		{
			cout << "The day is invalid for this month.  Please enter a number between 1 - 30: ";
			cin >> day;
			cout << endl;
		}
	}

	dDay = day;

	//this checks the year is greater than 1900
	if (year < 1900)
	{
		cout << "Please enter a year that is greater than 1900: ";
		cin >> year;
		cout << endl;
	}
	else
	{
		dYear = year;
	}
}

//this sets the day
int dateType::getDay() const
{
	return dDay;
}

//this sets the month
int dateType::getMonth() const
{
	return dMonth;
}

//this sets the year
int dateType::getYear() const
{
	return dYear;
}

//this prints the entered date
void dateType::printDate() const
{
	cout << dMonth << "-" << dDay << "-" << dYear;
}




Here is my main:

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
//***************************************
//*CISP232: C++ Programming I		    *
//*Programming Exercises Chapter 11		*
//*Instructor: Dr. Loay Alnaji          *
//*Created By: Rob McAndrews            *
//*Date Created: Mar 10, 2013           *
//*										*
//*This program prompts the user to		*
//*set the date.  It checks that the	*
//*input is a valid date including Feb	*
//*29 being only valid on leap years.	*
//***************************************

//header files
#include <iostream>
#include "dateType.h"
#include <string>

using namespace std;

//main program
int main()
{
	//declare variables
	dateType today;
	dateType cMonth;
	dateType cDay;
	dateType cYear;

	int month;
	int day;
	int year;
	string date[9];


	//prompt user to enter month
	cout << "This program simulates setting the date." << endl;
	cout << "Please enter a date in the format MM/DD/YYYY: ";
	cin >> date[9];

	cout << date;
	
	month = cMonth.findMonth(date);
	day = cDay.findDay(date);
	year = cYear.findYear(date);

	//calls function to check for leap year
	today.isLeapYear(year);
	//calls function to set the date
	today.setDate(month, day, year);
	
	//outputs date after validation and calls function for print date
	cout << endl;
	cout << "Today's date is: ";
	today.printDate();
	cout << endl;
	cout << endl;
		
	system ("pause");
	return 0;
}


Please help!
By the way, compiler is showing an error in std::char_traits<char>
This is from one of the default included headers?
I think cin>>date[9] is giving this error;

why have you used string date[9] everywhere? I guess it should be char date[9].
I was getting errors from using char so I thought maybe since the input contained '/' it wasn't a valid input for char so I switched to string. My understanding was char was only good for integers between -125 and 125 and 03/17/2013 seems to be out of bounds. Am I misunderstanding something?
Thanks eklavya sharma 2. The error is now gone. I stopped using [n] on my string (you're right this made no sense). It was left over from trying to use char and not having any success.

Now, I still have a problem. I added cout for the functions (fMonth, mm, fDay, dd, fYear, yy) and the f numbers are ascii (as expected) but mm, dd, and yy all report as 0. I have not used atoi before and am not sure if I am using it correctly. Any suggestions? Is using string incorrect and I need to find a way to make char work?
instead of using
fDay = date[3] + date[4]
use
1
2
fDay.push_back(date[3]);
fDay.push_back(date[4]);

Similar changes for fMonth and fYear

It seems your basics are very unclear.
http://www.cplusplus.com/doc/tutorial/
Yeah, I'm very new at this still. I've had about 9 weeks in an online course. I wish I had taken this in seat but I didn't have time to with working full time and kids. I feel very self taught in this class.

Your solution worked flawlessly. Now I can move on to adding and subtracting days. That should be a breeze. Thanks again for your help! We haven't really covered push back yet. I know I was handling it like a char array but I wasn't sure how to parse the string any other way.
Well push_back was just a quick fix but it is also possible to make this whole program only with char arrays.

I suggest you learn at a comfortable pace as learning very fast will make you forget things and you'll have to learn those things again. It is very important to be fluent in the basics before moving on.

http://norvig.com/21-days.html
Last edited on
I think I know what my issue was as I was browsing char arrays in the forums. I was using
char date[9];
as my variable. Well, date has 10 characters:

0 1 2 3 4 5 6 7 8 9 10
0 3 / 1 8 / 2 0 1 3 /n

I forgot about the return character so my input was being read as incomplete which was giving me a goofy output. If I switch back to
char date[10];
and use my original idea of
fMonth = date[0] + date[1];
will it read right? I know, I have to play with it myself but after 8 hours of frustration with it not working, I don't know if I want to go back. I still have two more steps to complete, which I know how I'm going to write those but this is due Wednesday night and I still have work and parenting.

Thanks for the advice about practicing. This class is a requirement for my degree. I am not getting into programming per se (networking security major) but I need to have a basic understanding in case I come across this stuff.
Last edited on
I tried, it doesn't work. I saw someone use something like this on a forum:

fMonth = ((char[0]-0)*10)+(char[1]-0);

but that didn't make any sense to me at all. The arithmetic made sense but I didn't think you could just use math with char arrays. And why subtract 0?
adding 2 char variables or constants returns a new char whose ASCII value is the sum of ASCII values of the 2 chars.

http://en.wikipedia.org/wiki/ASCII

'0' has ASCII value 48
'1' has ASCII value 49 and so on

adding '0' and '1' will return 97 (i.e. 48+49) which is the ascii value of 'a'.
fmonth is a string and assigning a char to a std::string makes no sense.
Topic archived. No new replies allowed.