I’m making a dice game in C++, and in my program I have some arrays.
die[5] = { (rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1 };
And then I use the arrays with
cout<<"First die: "<< die[0] <<"\n"
etc
But, when I run the program, the last array will always print 0, is there a way I can fix this?
In your code you have this line:
which is an invalid access as die has only 5 elements thus 0 to 4 are valid indexes.
This is actually “undefined behaviour”. Your program might core-dump / give an access violation but it doesn’t have to. It can instead just output some random number or zero…