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.
main.cpp
# include <iostream>
# include <cstdlib>
# include <ctime>
# include <vector>
#include <algorithm>]
#include "aDie.h"
#include "aHistogram.h"
usingnamespace std;
int main()
{
int seedNum;
cout << "enter a seed number" << endl;
cin >> seedNum;
srand(seedNum);
int numRolls;
constint 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;
}
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.
constint 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;
}
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?