Program Crashing!

I have been trying to make this program for a long time, but I keep getting this error.

When it gets to the Print() function, the program crashes (specifically at line 75).

Why is this?

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
// Giveaway Drawing Generator.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <Windows.h>

std::string strVersion = "0.1";

int nNumberOfContestants  = 0;
int nCycles = 0;

int *nContestants = new int[nNumberOfContestants];

int NumberOfContestants()
{
	using namespace std;

	cout << "Enter number of contestants: ";
	cin >> nNumberOfContestants;

	return 0;
}

int Cycles()
{
	using namespace std;

	cout << "Enter number of cycles: ";
	cin >> nCycles;

	return 0;
}

int SetContestants()
{
	for(int iii = 0; nNumberOfContestants >= iii; iii++)
	{
		nContestants[iii] = 0;
	}

	return 0;

}

int GenerateResults()
{
	std::cout << "Randomizing -- Please wait..." << std::endl << std::endl;

	for(int iii = 0; iii <= nCycles; iii++)
	{
		srand((unsigned int)time(0));

		int nRandom = rand() % nNumberOfContestants + 1;

		nContestants[nRandom]++;

		Sleep(50);
	}

	return 0;
}

int Print()
{
	using namespace std;

	cout << "Results:" << endl;

	for(int iii = 0; iii <= nNumberOfContestants; iii++)
	{
		cout << "Contestant " << iii << " = " << nContestants[iii] << endl;
	}

	return 0;
}

int main()
{
	using namespace std;

	cout << "Giveaway Drawing Generator" << endl;
	cout << "Version " << strVersion << endl << endl;

	NumberOfContestants();
	Cycles();
	SetContestants();
	GenerateResults();
	Print();
}
Last edited on
 
for(int iii = 0; iii <= nNumberOfContestants; iii++)


should be:
 
for(int iii = 0; iii < nNumberOfContestants; iii++)


The use of global variables is not appropriate here.

1
2
3
4
5
int nNumberOfContestants  = 0;
int *nContestants = new int[nNumberOfContestants];
// The above means that you actually do:
int *nContestants = new int [ 0 ];
// And that is an empty array. 

But do not worry. That is not all. Even if the size of array would be equal to the number given by the user, the <= in loops will still lead to out of range, i.e. an error.
You do this in the global scope:
int *nContestants = new int[nNumberOfContestants];

I wasn't aware that this was legal, but it would be done before any code. Since, at this time, nNumberOfContestants is 0, you have an array of size 0. What you should do is move this line down to line 24 (right after you write to: nNumberOfContestants).

The second issue is that you do this:
for(int iii = 0; iii <= nNumberOfContestants; iii++)
but it should be
for(int iii = 0; iii < nNumberOfContestants; iii++)


Let's say you have an array of 4. You can access m_array[0], m_array[1], m_array[2], m_array[3]. If you try and access m_array[4], then you're accessing memory which is not meant for you. This will often even cause the crashes.
Topic archived. No new replies allowed.