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!

Small prog crashes with no ERRORS LISTED?!?! Why?

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
 

int Dice (int numDie = 1, int numSides = 6, int numAdd = 0)


x = Dice(); // This is not what is the compiler expect
//according to the function prototype!!!!!

Walid Magd
Engwam@Hotmail.com
 
Ok... I inserted this at the top of the header...

int Dice(int numDie = 1, int numSides = 6, int numAdd = 0);

and changed the function to

int Dice(int numDie = 1, int numSides = 6, int numAdd = 0)

and I am still getting the same error...

How do I change it to make default values?
 
My last post was wrong...
I changed the function part to this:

Dice (int numDie, int numSides, int numAdd)

and not

Dice (int numDie = 1, int numSides = 6, int numAdd = 0)

But, I still get same error.
 
your

Dice (int numDie = 1, int numSides = 6, int numAdd = 0)

is fine. This sets default values and allows for you to call the fuction as

Dice()
Dice(value)
Dice(value,value);
Dice(value,value,value);

Where the problem is, is in the total array;

Dice() // no parameters
returns a value from 1-6 and the array ranges from 0-4

total[(x+1)] will set elements 2 thru 7... not what you are looking for. This is where I think the problem lies.

Matt

 
The first bug:
for (int tyu = 0; tyu < 6; tyu++) total[tyu] = 0;
you should put tyu < 5 because the array has five elements.
second bug:
for (int ctr2 = 0; ctr2<6;ctr2++)
you should write ctr2<5 because of total
third bug:
..... <<(ctr / total[(ctr2+1)]) << &quot;\t&quot;;
all elements of total are initialized to 0. The processor cant divide to 0. John Fill
1c.bmp


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

Part and Inventory Search

Sponsor

Back
Top