简体   繁体   中英

Why am i getting out-of-range error?

I can't seem to find where my issue is. Its a three file program with aDie class in one file, aHistogram class in another file, and the main.cpp file. It is supposed to print a histogram constructed with X's to show how many times the die landed on each of the six faces. I cant move forward because of the vector error... There may be other issues with the program that i haven't worked out yet, but I just want to know about the vector error. Thank you.

main.cpp:

#include <iostream>
#include <stdlib.h> //srand and rand
#include <time.h> //Time
#include <vector>
#include <algorithm>
#include "aHistogram.h"
#include "aDie.h"

using namespace std;

int main()
{
    srand (time(NULL));
    int numRolls;
    const int maxLengthOfLine = 50;

    cout << "How many rolls? " << endl;
    cin >> numRolls;

    aDie fairDie;
    aHistogram fairHistogram;

    //For Loop rolls the die and updates the histogram vector ~~binHistogram.
    for(int i = 0; i < numRolls; i++)
    {
        int face = fairDie.roll();
        fairHistogram.update(face);
    }

    cout << "*******************" << endl;
    cout << "*****Histogram*****" << endl;
    cout << "*******************" << endl;

    fairHistogram.display(maxLengthOfLine);


}

aDie.h:

#ifndef ADIE_H_INCLUDED
#define ADIE_H_INCLUDED

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

using namespace std;

/********************************************/
/*******Definition of aDie class*************/
/********************************************/
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 numFaces = 6;
};

int aDie::roll()
{
    return ((rand() % numFaces) + 1); //returns a random number between 1 and 6
}

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

aDie::~aDie()
{
    return;
}

#endif // ADIE_H_INCLUDED

aHistogram.h:

#ifndef AHISTOGRAM_H_INCLUDED
#define AHISTOGRAM_H_INCLUDED

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

using namespace std;

/********************************************/
/*******Definition of aHistogram class*******/
/********************************************/

class aHistogram
{
    public:
        void update(int face);
        void display(int maxLengthOfLine);
        int Count(int face);
        void clear();

        aHistogram(); //Constructor
        ~aHistogram(); //Destructor

    private:
        vector<int> binHistogram;
        const int numFaces = 6;
        int totalRolls;
        int largeBin = 0;
        double xScale;
};

//Adds a count to each face every time the die lands on said face.
void aHistogram::update(int face)
{
    binHistogram.at(face) += 1;
    return;
}

//Displays the histogram with X's
//maxLengthOfLine represents the maximum number of x’s to be printed for the largest bin count.
void aHistogram::display(int maxLengthOfLine)
{
    xScale = maxLengthOfLine / largeBin;
    for(int i = 1; i <= 6; i++)
    {
        cout << i << " : " << Count(i) << " : ";
        int numXs = xScale * binHistogram.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 bin count
    for (int i = 1; i < numFaces; i++)
    {
        while (binHistogram[i] >= largeBin)
        {
            largeBin = binHistogram.at(i);
        }
    }

    //
    return binHistogram.at(face);
}

void aHistogram::clear()
{
    binHistogram.clear();
    return;
}

//Defines the DEFAULT CONSTRUCTOR. Sets all elements of the histogram to zero.
aHistogram::aHistogram()
{
    return;
}

//Defines the DESTRUCTOR. Clears vector after use.
aHistogram::~aHistogram()
{
    binHistogram.clear(); //Clears vector
    return;
}



#endif // AHISTOGRAM_H_INCLUDED

I didnt find the place where you initialize the histogram this might be the problem. But even if you fix that, you will hit two other bugs:

for (int i = 1; i < numFaces; i++)
{
    while (binHistogram[i] >= largeBin)
    {
        largeBin = binHistogram.at(i);
    }
}

you are accessing elements 1....6 when probably it should be 0...5 . Same problem in the line where you have

largeBin = binHistogram.at(i);

which is most likely the line that causes the error (the one above wont be so nice to tell you what is the problem but just crash your program).

You never change the size of the vector in the aHistogram class, which means its size will always zero. Any index will be out of bounds.

For things like histograms I would actually recommend you to use std::unorderd_map instead of std::vector , with the "face" being the key and the count being the data. Then you could do eg

binHistogramMap[face] += 1;

without worrying about the element for face not existing (it will be created and initialized to zero if the entry doesn't exist).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM