having trouble Implementing classes

I have to use the classes i created to roll the die 12000 and update the histogram and the list. I keep getting an error and cannot figure out how to fix it.

the error I am getting: Unhandled exception at 0x76972F71 in Dice.exe: Microsoft C++ exception: std::out_of_range at memory location 0x001DF430.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef ADIE_H
#define ADIE_H
#include <iostream>


class aDie
{
public:
	aDie(); // default constructor
	void Roll();
	
private:
	int die;
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "aDie.h"
using namespace std;

aDie::aDie()
{
	die = 0;
	return;
} //default constructor 

	void aDie::Roll() //generating a random number from 1-6
	{
		
		die = 0;
		die = ((rand() % 6) + 1);
		
	}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef AHISTOGRAM_H
#define AHISTOGRAM_H
#include <iostream>
#include <vector>
using namespace std;

class aHistogram
{
public:
	aHistogram(); // default constructor
	void update(int face); // update histogram counts
	void display(int maxLengthOfLine); // displays histogram
	void clear(); // clears counts
	vector<int> graph;
	
private:
	int face;
	int maxLengthOfLine;
};
#endif 


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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "aDie.h"
#include "aHistogram.h"
using namespace std;

aHistogram::aHistogram()
{
	face = ((rand() % 6) + 1);
	maxLengthOfLine = 60;
}
void aHistogram::update(int face) // updates roll count
{
	graph.at(face++);
}
void aHistogram::display(int maxLengthOfLine)
{
	int i;

	cout << "Face Appearances" << endl; // displays the appearances of each face
	cout << endl;
	for (i = 0; i < graph.size(); ++i)
	{
		cout << i + 1 << "---" << graph.at(i) << endl;
	}
	cout << endl;

	cout << "Histogram" << endl; // displays the histogram count
	cout << endl;
	for (i = 0; i < graph.size(); ++i)
	{
		cout << i + 1 << " - ";
		while (graph.at(i) > maxLengthOfLine)
		{
			cout << "X";
			graph.at(i) -= maxLengthOfLine;
		}
		cout << endl;
	}
}

void aHistogram::clear() // clears the counts for the appearances of each face 
{
	face = 0;
	maxLengthOfLine = 0;

}


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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "aDie.h"
#include "aHistogram.h"
using namespace std;

int main()
{
	int i;
	srand(time(0)); // seeding RNG

	aDie Die1;
	aHistogram Die2;
	
	for (i = 0; i < 12000; ++i)
	{
		Die1.Roll();
	}

	Die2.update(6);
	Die2.display(60);

	cin.ignore();

	return 0;
}


Any guidance would be appreciated.
I think its because your graph is have an empty size, but you tried to access it by grapgh.at()
Try to resize the graph first before using it, or use push_back()
When I tried to give it a size it brought errors up where I used graph

I would size like this: vecctor<int>graph(6) right?
Or try this graph.resze(6)
and I should put that in the histogram class?
The constructor should be a good home for it :D
It is still bringing up that same error :(
In update function what did graph.at(face++); do ?

If face is 6 then it'll try to acess graph.at(7)
It is supposed to update each number of appearances of each face of the die.
You mean like this graph.at(face)++
Okay so now it looks like this

1
2
3
4
5
6
7
8
9
10
aHistogram::aHistogram()
{
	face = ((rand() % 6) + 1);
	maxLengthOfLine = 60;
	graph.resize(6);
}
void aHistogram::update(int face) // updates roll count
{
	graph.at(face)++;
}


but it still brought up that error, i feel like it is something that is so simple that I am just missing.
Topic archived. No new replies allowed.