Exception Issue

I am having an exception Issue and I can't seem to find it and how to fix it.


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
#include "myDate.h"
#include <string>
#include <iostream>
using namespace std;

int day; 
int month;
int year;

int Greg2Julian(int month, int day, int year) {
	int JD = day - 32075 + 1461 * (year + 4800 + (month - 14) / 12) / 4 + 367
		* (month - 2 - (month - 14) / 12 * 12) / 12 - 3
		* ((year + 4900 + (month - 14) / 12) / 100) / 4;
	return JD;
}

void Julian2Greg(int JD, int & month, int & day, int & year) {
	int L = JD + 68569;
	int N = 4 * L / 146097;
	L = L - (146097 * N + 3) / 4;
	int I = 4000 * (L + 1) / 1461001;
	L = L - 1461 * I / 4 + 31;
	int J = 80 * L / 2447;
	int K = L - 2447 * J / 80;
	L = J / 11;
	J = J + 2 - 12 * L;
	I = 100 * (N - 49) + I + L;

	year = I;
	month = J;
	day = K;
}

myDate::myDate() {
	month = 5;
	day = 11;
	year = 1959;
}

myDate::myDate(int M, int D, int Y) {
	int jd = Greg2Julian(M, D, Y);
	int m1, d1, y1;
	Julian2Greg(jd, m1, d1, y1);
	if (M == m1 && D == d1 && Y == y1) {
		month = M;
		day = D;
		year = Y;
	}
	else {
		month = 5;
		day = 11;
		year = 1959;
	}
}


void myDate::display() {
	string months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
	cout << months[month - 1] << " " << day << ", " << year;
}


void myDate::increaseDate(int n) {
	int jd = Greg2Julian(month, day, year);
	jd += n;
	Julian2Greg(jd, month, day, year);
}

void myDate::decreaseDate(int n) {
	int jd = Greg2Julian(month, day, year);
	jd -= n;
	Julian2Greg(jd, month, day, year);
}

int myDate::daysBetween(myDate D) {
	int beforeJD = Greg2Julian(month, day, year);
	int afterJD = Greg2Julian(D.getMonth(), D.getDay(), D.getYear());
	int difference = afterJD - beforeJD;
	return difference;
}

int myDate::dayOfYear() {
	int JD = Greg2Julian(1, 1, year);
	int beforeJD = Greg2Julian(month, day, year);
	int difference = beforeJD - JD;
	return difference + 1;
}


string myDate::dayName() {
	string days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
	int jd = Greg2Julian(month, day, year);
	int day = jd % 7;
	string result = days[day + 1];
	return result;
}


int main()
{
	myDate Bday;
	myDate duedate(10, 3, 2018);

	int x;
	int month, day, year;

	Bday.display();
	cout << endl;
	duedate.display();
	cout << endl;
	x = Bday.daysBetween(duedate);
	cout << "Master Gold is " << x << " days old today";
	cout << endl;

	Bday.increaseDate(x);

	cout << "Now these 2 dates should be the same:";
	Bday.display();
	cout << '\t';
	duedate.display();
	cout << "\n\n";

	Bday.decreaseDate(x); // setting Bday back to original value

	month = duedate.getMonth();
	day = duedate.getDay();
	year = duedate.getYear();

	duedate.display();
	cout << " is also equal to " << month << "/" << day << "/" << year << endl;

	myDate July4(7, 4, 2018);
	cout << "This year the 4th of July will happen " << (July4.dayOfYear() - 1) << "days after New Years\n\n";

	myDate bogus(23, 12, 2007);
	cout << "The value of the bogus date is:";
	bogus.display();
	cout << endl;

	myDate D5(8, 21, 2017);
	x = D5.dayOfYear();
	D5.decreaseDate(x - 1);
	cout << "Happy Newyear 2017 happened on:";
	D5.display();
	cout << endl;

	cout << "Program is due on " << duedate.dayName() << endl;
	cout << "Master Gold was born on " << Bday.dayName() << endl;

	myDate today = duedate;
	cout << "\nHere are the dates for the next 2  weeks:\n";
	for (int i = 0; i<14; i++)
	{
		today.display();
		cout << ":" << today.dayName() << endl;
		today.increaseDate(1);
	}

	// find all the leap years since 1400
	int counter = 1;
	int leapSum = 0;
	cout << "\n\nLeap Years from 1400 to 2018\n\n";
	for (int y = 1400; y <= 2018; y++)
	{

		myDate leapYear = myDate(12, 31, y);
		leapYear.display();
		cout << ": ";
		leapYear.dayOfYear();
		cout << endl;
		//leapYear.display();
		//cout << ": ";
		//leapYear.dayOfYear();
		//cout << endl;
		if (leapYear.dayOfYear() == 366)
		{
			cout << y << ", ";
			leapSum++;
			if (counter++ % 12 == 0)
			{
				cout << endl;
			}
		}
	}
	cout << "\b\b ";  // get rid of the last comma
	cout << "\n\nHere's the number of the above leapyears:" << leapSum << endl;
	cout << "\n\nPress enter to continue";
	getchar();
	return 0;
	
	
	
}
Last edited on
cool.

If you want us to do your homework tell us more about it, like what did your debugger tell you.

also do this:
http://www.cplusplus.com/articles/jEywvCM9/
when i run it, it tells me

1
2
3
4
5
6
7
8
9
10
11
12
'Date Project.exe' (Win32): Loaded 'C:\Users\mhern\Desktop\Date Project\Release\Date Project.exe'. Symbols loaded.
'Date Project.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded.
'Date Project.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'Date Project.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded.
'Date Project.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbase.dll'. Symbols loaded.
'Date Project.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140.dll'. Symbols loaded.
'Date Project.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140.dll'. Symbols loaded.
'Date Project.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140.dll'. Symbols loaded.
'Date Project.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\vcruntime140.dll'
Exception thrown at 0x73C8DEAE (vcruntime140.dll) in Date Project.exe: 0xC0000005: Access violation reading location 0x3C1CE514.

Unhandled exception at 0x73C8DEAE (vcruntime140.dll) in Date Project.exe: 0xC0000005: Access violation reading location 0x3C1CE514.
Post the contents of the file myDate.h
Here goes my header file.


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
#ifndef MYDATE_H
#define MYDATE_H
#include <string>
using namespace std;

class myDate {
private:
	int year;
	int month;
	int day;
public:
	myDate();
	myDate(int m, int d, int y);
	void display();
	void increaseDate(int n);
	void decreaseDate(int n);
	int daysBetween(myDate D);
	int getMonth()
	{return month;}
	int getDay()
	{return day;}
	int getYear()
	{return year;}
	int dayOfYear();
	string dayName();
};
#endif 
Last edited on
There is a potential out of bounds access to the array in this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string myDate::dayName() {
	string days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
	int jd = Greg2Julian(month, day, year);

        if( jd < 0 ) { // *** sanity check added
            std::cout << "sanity check failed\n" ;        
            return "invalid date / logical error" ;
        }

	int day = jd % 7; // the max value of day is 6 (min value is zero)
 
	// string result = days[day + 1]; // **** out of bounds access when day == 6
        string result = days[day]; // 0 == "Sunday", ... 6 == "Saturday"
        return result;
}
Thank you so much, one more thing, is there a way to just display only the leap years at the end and not the every year.

ex. 1404,1408,1412, 1416,1420,1424,1428
1432,1436,1440 and so on

i just want to list the leap years and not the whole dates.
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
#include <iostream>

bool is_leap_year( int year )
{ return year%4 == 0 && ( year%100 != 0 || year%400 == 0 ) ; }

int main()
{
    // find all the leap years since start_year
    int start_year = 1400 ;
    while( start_year%4 != 0 ) ++start_year ; // get to a multiple of 4
    // we can now increment in steps of 4

    int num_leap_years = 0 ;
    for( int year = start_year ; year <= 2018 ; year += 4 )
    {
        if( is_leap_year(year) )
        {
            ++num_leap_years ;
            std::cout << year << ' ' ;
            if( num_leap_years%16 == 0 ) std::cout << '\n' ;
        }
    }

    std::cout << "\n\nnumber of leap years: " << num_leap_years << '\n' ;
}

http://coliru.stacked-crooked.com/a/b47fa5fe7eb8bca5
im having an issue on my output, when it shows the dates for the next 2 weeks, my first date is one day ahead. I cant seem to fix it

May 11, 1959
October 3, 2018
Master Gold is 21695 days old today
Now these 2 dates should be the same:October 3, 2018    October 3, 2018

October 3, 2018 is also equal to 10/3/2018
This year the 4th of July will happen 184days after New Years

The value of the bogus date is:May 11, 1959
Happy Newyear 2017 happened on:January 1, 2017
Program is due on Tuesday
Master Gold was born on Sunday

Here are the dates for the next 2  weeks:
October 3, 2018:Tuesday
October 4, 2018:Wednesday
October 5, 2018:Thursday
October 6, 2018:Friday
October 7, 2018:Saturday
October 8, 2018:Sunday
October 9, 2018:Monday
October 10, 2018:Tuesday
October 11, 2018:Wednesday
October 12, 2018:Thursday
October 13, 2018:Friday
October 14, 2018:Saturday
October 15, 2018:Sunday
October 16, 2018:Monday


Leap Years from 1400 to 2018

1404, 1408, 1412, 1416, 1420, 1424, 1428, 1432, 1436, 1440, 1444, 1448,
1452, 1456, 1460, 1464, 1468, 1472, 1476, 1480, 1484, 1488, 1492, 1496,
1504, 1508, 1512, 1516, 1520, 1524, 1528, 1532, 1536, 1540, 1544, 1548,
1552, 1556, 1560, 1564, 1568, 1572, 1576, 1580, 1584, 1588, 1592, 1596,
1600, 1604, 1608, 1612, 1616, 1620, 1624, 1628, 1632, 1636, 1640, 1644,
1648, 1652, 1656, 1660, 1664, 1668, 1672, 1676, 1680, 1684, 1688, 1692,
1696, 1704, 1708, 1712, 1716, 1720, 1724, 1728, 1732, 1736, 1740, 1744,
1748, 1752, 1756, 1760, 1764, 1768, 1772, 1776, 1780, 1784, 1788, 1792,
1796, 1804, 1808, 1812, 1816, 1820, 1824, 1828, 1832, 1836, 1840, 1844,
1848, 1852, 1856, 1860, 1864, 1868, 1872, 1876, 1880, 1884, 1888, 1892,
1896, 1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944,
1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992,
1996, 2000, 2004, 2008, 2012, 2016

Here's the number of the above leapyears:150


Press enter to continue



Just add 1 to jd then (in parenthesis before the modulus operator).

Everything is off by 1, Master Gold should be on monday, not sunday, and everything else has the +1 error on the day string. Perhaps it is because of a 1-indexing flaw in the calculation.
Last edited on
Topic archived. No new replies allowed.