Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Program crashes but no errors appear... (small prog)

Status
Not open for further replies.

Skywise

Programmer
Apr 11, 2001
9
0
0
US
I have a small program that causes the "This program has performed an illegal operation and will be shut down." box to pop up everytime I run it. Also, the program doesn't show any output so I dont think it even ran. MSVC++ 6.0 does not show any errors or warnings (I have the warning level set to level 3). Here is my program:

Header File (RND.H)
// This seeds the random numbers...
void Startup()
{ srand( (unsigned)time( NULL ) ); }

int Dice (int numDie = 1, int numSides = 6, int numAdd = 0)
{
int Total = 0;
if ((numDie > 0) && (numSides > 0))
{
int x;
for (int i = 0; i < numDie; i++)
{
x = (rand()%numSides) + 1;
Total = Total + x;
}
Total = Total + numAdd;
}
return Total;
}

And this is the RND.CPP file...

#include &quot;stdafx.h&quot;

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

#include &quot;RND.h&quot;


int main(int argc, char* argv[])
{
Startup();
int total[5];
int x;
for (int tyu = 0; tyu < 6; tyu++) total[tyu] = 0;
for (int ctr = 0; ctr < 1001; ctr++)
{
x = Dice();
total[(x+1)]++;
for (int ctr2 = 0; ctr2<6;ctr2++)
cout << &quot;Side &quot; << ctr2 << &quot;: &quot; << total[(ctr2+1)] << &quot; / &quot; << (ctr / total[(ctr2+1)]) << &quot;\t&quot;;
cout << &quot;\n&quot;;
}
return 0;
}

Here is the text in the windows error box (I dont know if this helps or not):

RND caused a divide error in module RND.EXE at 015f:004011c7.
Registers:
EAX=00000000 CS=015f EIP=004011c7 EFLGS=00010297
EBX=00560000 SS=0167 ESP=0066fd84 EBP=0066fdf8
ECX=00000000 DS=0167 ESI=816cb73c FS=342f
EDX=00000000 ES=0167 EDI=0066fdf8 GS=0000
Bytes at CS:EIP:
f7 7c 8d f0 50 68 2c 80 42 00 8b 55 dc 8b 44 95
Stack dump:
00428030 00000000 816cb73c 00560000 cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc

There it all is... I dont have the faintest idea whats wrong with my prog. Thanks in advance for your help.

Skywise
 
Hi,
the machine processor can't divide by 0. Every numbers on total in your program are initialized to 0.
By the way, you have two other bugs where you'll get memory fault.
In the first for, you should put <5 instead <6. In the third is the same thing. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top