Archived

This topic is now archived and is closed to further replies.

Da cobra

get a memory error

Recommended Posts

Da cobra    122
can som1 pls try this code or tell me why I get a memory error when I run it?
        

#include <time.h>

#include <stdlib.h>

#include <iostream.h>

void main()
{
	// *** initialisation ***


	int dice[10] ;
	int counter = 0 ;
	int three = 0 ;
	int six = 0 ;

	// *** generate 10 random numbers ***

	for (counter = 1; counter <= 10; counter++)
	{
		srand ((unsigned)time(NULL)) ;
		dice[counter] = rand()%6 + 1;
		if (dice[counter] == 3)
		{
			three = three + 1 ;
		}
		else 
			if (dice[counter] == 6)
			{
				six = six + 1 ;
			}
		
	}
	
	// *** output ***

	system("cls") ;
	for (counter = 1; counter <= 10; counter++)
	{
		cout << "Dice " << counter << " = " << dice[counter] << endl ;
	}
	cout << endl ;
	cout << "thrown a 3 : " << three << endl ;
	cout << "thrown a 6 : " << six << endl ;
	
}

        
thanx in advance for any help [edited by - Da Cobra on March 18, 2003 11:37:41 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Instead of counter <= 10, replace with counter < 10. If you have int dice[10], there is no dice[10]. That defines 10 elements in the array, which would be dice[0..9]. That should fix it.

Share this post


Link to post
Share on other sites