Console crashes upon passing dynamically allocated array as a parameter

Hi, I wrote a program to simulate rolling dice with input number of dice, sides, and rolls, and ask the user if they want to display a histogram of the sum at the end. I am having issues with the histogram, it crashes upon passing into the Histogram function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Dice.h
#include <iostream>
#include <cstdlib>
#include <time.h>

class Dice //Was having trouble with the class from the homework
	       //so I rewrote the class to make it easy for varied sides.
{
    private:
        int faces; 
        int value; 
    public:
        int getFaces() {return faces;} 
        int roll() 
        {
            value = rand() % faces + 1;
            return value;
        }
        Dice() : faces(6) {}; 
        Dice(int size) : faces(size) {};
};

and
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
//roll.cpp
#include <iostream>
#include <cstdlib>
#include <time.h>
#include "Dice.h"

void selectionSort(int a[], int n)  // sort the array a of size n using selectionSort algorithm
{
		int min,temp;

	for (int i=0; i < n-1; i++)
	{
	    min = i;
		for (int j=i+1; j < n; j++)
		{
			if (a[j] < a[min])
			{
           	 	min=j;
			}
		}
        if (min != i)
            {
            	temp = a[i];
                a[i] = a[min];
                a[min] = temp;
            }
	}
}

void histogram(int a[], int n)  // display the histogram of the integer data array a of size n
{
	selectionSort(a, n);
	for(int i=0; i<n;)
	{
		int value = a[i];
		std::cout << a[i] << ":";
		do {
			std::cout << "*";
			i++;
			if(i>=n) 
			break;
			} 
		while(a[i] == value);
			std::cout << std::endl;
	}
}


int main()
{
    srand( time(NULL) );
    int dFaces, numdice,numroll,index;
	char yesno;
	int sum=0,roll;

    std::cout << "How many dice?: ";
    std::cin >> numdice;

	std::cout << "How many faces?: ";
    std::cin >> dFaces;

	std::cout << "How many rolls?: ";
    std::cin >> numroll;

	int *hist;
	hist = new int[numroll];

	Dice* cup = new Dice[numdice];
	for (int j=1; j <numroll+1; j++)
	{
		printf("\nSet #%d:",j);
		for (int i=1; i < numdice+2; i++)
		{
			cup[i] = dFaces;
			roll = cup[i].roll();
			printf("\nRoll on a %d sided die: %d",dFaces,roll);
			sum += roll;
			
		}
		printf("\nTotal: %d\n",sum);
		index = j-1;
		hist[index] = sum;
		sum = 0;
	}
	std::cout << "\nDo you want to see the histogram? ( Y/N): ";
	std::cin >> yesno;
	if (yesno == 'Y')
	{
		histogram(hist,numroll);
	}

    return 0;
}


So after testing, it gets through the call and the sort, but after that crashes somewhere
Last edited on
Doesn't crash for me. What input are you using?
Topic archived. No new replies allowed.