Can't figure out std::out_of_range at memory location error while debugging

The error : "Unhandled exception at 0x75AEC52F in Project5.exe: Microsoft C++ exception: std::out_of_range at memory location 0x003CF370."
occurs in the aHistogram.cpp file where indicated. If anyone can help me understand where the issue is I would greatly appreciate 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
main.cpp 
# include <iostream>
# include <cstdlib>
# include <ctime>
# include <vector>
#include <algorithm>]
#include "aDie.h"
#include "aHistogram.h"

using namespace std;




int main()

{
	int seedNum;
	cout << "enter a seed number" << endl;
	cin >> seedNum; 
	srand(seedNum);
	int numRolls;

	const int maxLengthOfLine = 60;

	cout << "How many rolls? " << endl;

	cin >> numRolls;

	aDie fairDie;

	aHistogram fairHistogram;

	//For Loop rolls the die and updates the histogram vector Histogram.

	for (int i = 0; i < numRolls; i++)

	{

		int face = fairDie.roll();

		fairHistogram.update(face);

	}


	cout << "Histogram: " << endl;


	fairHistogram.display(maxLengthOfLine);
}


aDie.



#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
# include <iostream>

using namespace std;

class aDie

{

public:

	int roll(); //return an integer between 1 and 6 to represent what face appears when the die is rolled.

	aDie(); //Default constructor

	~aDie(); //Destructor

private:
int faces = 6;
};




aDie.cpp------------------------------------------------------------------------


#include "aDie.h"
using namespace std;

int aDie::roll(){
  return ((rand() % faces) + 1); //returns a number 1-6 randomly

}

aDie::aDie(){
    
	cout << "Dice Roll" << endl;
    return;

}

aDie::~aDie(){
   
	return;
}




aHistogram.h------------------------------------------------------------------

#define AHISTOGRAM_H_INCLUDED
#include <algorithm>
#include <stdlib.h>
#include <vector>
# include <iostream>
using namespace std;

class aHistogram{

public:

	void update(int face);

	void display(int maxLengthOfLine);

	int Count(int face);

	void reset();

	aHistogram(); //Constructor

	~aHistogram(); //Destructor

	vector<int> Histogram;

private:

	const int Faces = 6;

	int totalRolls;

	int bigVal = 0;

	double xScaler;

	int face = 0;

	 int maxLengthOfLine = 0;
};



aHistogram.cpp-----------------------------------------------------------------


//Adds a count to each face every time the die lands on said face.
#include "aHistogram.h"

void aHistogram::update(int face){

	Histogram.at(face)++ ;     <<<<<<------------------------- Error is here
    return;

}

//Displays the histogram with X's

//maxLengthOfLine represents the maximum number of x’s to be printed for the largest count.

void aHistogram::display(int maxLengthOfLine)

{

	xScaler = maxLengthOfLine / bigVal;

	for (int i = 1; i <= 6; i++)

	{

		cout << i << " : " << Count(i) << " : ";

		int numXs = xScaler * Histogram.at(i);

		for (int j = 0; j < numXs; j++)

		{

			cout << "X";

		}

	}

}

//To be called AFTER aHistogram::update

//Returns a count of how many times for each face of the die

int aHistogram::Count(int face)

{

	//For Loop determines the largest count

	for (int i = 1; i <= Faces; i++)

	{

		while (Histogram.at(i) >= bigVal)

		{

			bigVal = Histogram.at(i);

		}

	}

	//

	return Histogram.at(face);

}

void aHistogram::reset()

{

	Histogram.clear();

	return;

}

//Defines the DEFAULT CONSTRUCTOR. Sets all elements of the histogram to zero.

aHistogram::aHistogram()

{
	vector<int> Histogram(7);
	
	const int Faces = 6;

	maxLengthOfLine = 60;

}

//Defines the DESTRUCTOR. Clears vector after use.

aHistogram::~aHistogram()

{

	Histogram.clear(); //Clears vector

	return;

}



Last edited on
Please don't delete your post and re-post. Just edit the original. My response was lost posting to a thread that no longer existed.

1
2
3
4
5
6
7
8
9
10
11
12
aHistogram::aHistogram()

{
	vector<int> Histogram(7);    // creates a local-to-the-constructor variable with the same name
                                     // as a member variable, but has no relation to it at all.
	
	const int Faces = 6;   // creates a local-to-the-constructor variable with the same name
                               // as a member variable but has no relation to it at all.

	maxLengthOfLine = 60;

}


Instead:
1
2
3
aHistogram::aHistogram() : Histogram(7), Faces(6), maxLengthOfLine(60)
{
}

Last edited on
Sorry wont happen again, I'm new to the site and I was just trying to make the code more readable.
That change did fix that error however so thank you very much.

A new issue has arisen unfortunately. After getting past the initial error it then was giving me a divide by zero error, as I had forgotten to call the Count() function after the update() function in main.cpp. Once I added the Count() function call it now runs the program but just prompts the user for the seed number, the number of rolls, and then says "Dice Roll". After that nothing happens and the display() function doesn't run. Here's what the main.cpp file looks like now, what is causing it to hang up and not run the display() function?

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


int main()

{
	int seedNum;
	cout << "enter a seed number" << endl;
	cin >> seedNum; 
	srand(seedNum);
	int numRolls;

	const int maxLengthOfLine = 60;

	cout << "How many rolls? " << endl;

	cin >> numRolls;

	aDie fairDie;

	aHistogram fairHistogram;

	//For Loop rolls the die and updates the histogram vector Histogram.

	for (int i = 0; i < numRolls; i++)

	{

		int face = fairDie.roll();

		fairHistogram.update(face);

		fairHistogram.Count(face);

	}




	cout << "Histogram: " << endl;


	fairHistogram.display(maxLengthOfLine);
}




Last edited on
aHistogram::Count contains an infinite loop.
Ah OK, I simply had a >= where it should have been a >. Thank you for all the help It seems to be working fine now.

Topic archived. No new replies allowed.