Trying to make a hit counter with a random number generator

Hello fellow programmers, I decided to make a hit counter. Basically its an array that loops through the first and last index. In that loop I have a random number generator which will increase the value at that index by 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  /*HitCounter.cpp By JacobAmaral*/

#include "stdafx.h"
#include "iostream"
#include "stdlib.h"

using std::cout; using std::cin; using std::endl;using std::rand;
/*Main method*/
void _tmain(int argc, _TCHAR* argv[])
{
	int randNum;
	int hitCounter[100];
	

	for (auto i = 0; i != 100; i++)
	{
		hitCounter[rand() % 100]++;
	}
	cout << "Position\t Hits" << endl;
	for (int n = 0; n != 100; n++)
	{
		cout << n << "\t\t" << hitCounter[n] << endl;
	}



I made this program in Java but obviously C++ has differences. The problem is I am getting very odd values (-8789655), some of the value are different and some are the same. Its not counting hits the way I want it to be (positive).

My results: http://i62.tinypic.com/2ih7dc4.png"
Last edited on
My friend fixed it, I had to initialize the array hitCounter[100]={0};
Topic archived. No new replies allowed.