I Need C++ Help

closed account (967L1hU5)
I have another problem with a game I'm making, here's the part of the code where it goes funny:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (age >= 18)
                {
                    //cin.ignore();
                    cout << "*ask if playre really wants to continue*" << endl;
                    getline (cin, yn);
                    if (yn == "no")
                    {
                        cout << "*no action*" << endl;
                    }
                    if (yn == "yes")
                    {
                        actionchance = rand() % 0 + 6;
                        cout << "*action*" << endl;
                    }
                }

I have cin.ignore commented because it worked with a similar problem, but not this time. If the string "yn" is answered with "yes", the console will freeze up and Windows (I'm on Windows 7) will give me an error message. If I click on more detail, I'm presented with:

Problem signature:
Problem Event Name: APPCRASH
Application Name: Actual Entity.exe
Application Version: 0.0.0.0
Application Timestamp: 4e540663
Fault Module Name: Actual Entity.exe
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 4e540663
Exception Code: c0000094
Exception Offset: 00006d78
OS Version: 6.1.7600.2.0.0.256.1
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

When I exit out of that window, the console says:

Process returned -1073741676 (0xC0000094)

Than I'll press a button and the console will exit. Does anyone know what's happening or how to fix/ get around it?
actionchance = rand() % 0 + 6;


You are trying to divide by zero.

% gives you the remainder after division. When you say rand() % x, you are saying, "divide whatever number rand gives by x, and give me the remainder". Since x is 0, you have a division by 0, which causes the world to explode.
closed account (967L1hU5)
When I read
You are trying to divide by zero.
I could not help but laugh, very loud. So will changing 0 to 1 fix it?
That will stop the crash, yes. But I doubt that will do what you want.

If you do this:
actionchance = rand() % 1 + 6;

Then actionchance will always be 6 (x % 1 is always 0, since the remainder of division by 1 is zero). What exactly are you trying to accomplish with that line?
Last edited on
closed account (967L1hU5)
The chance for pregnancy. "Action" was my little euphemism for sex, and I didn't want my retardery to draw people away from my problem, so I cleaned it up.
Never forget BODMAS when writing formulae :)

Operators are applied in this order:

1st. Brackets
2nd. Order (aka indices)
3rd. Division
4th. Multiplication
5th. Addition
6th. Subtraction

And since the modulus sign (%) counts as division, "rand() % 0" will be calculated before the addition of 6 is applied (as explained by Disch), hence the division by zero.
closed account (967L1hU5)
Oh, well than how can I generate a random number between 1-6? I used the same formula, except it went to 50, and the names generate different every time.
for a random number between any two numbers a and b given that a < b

rand()%(b-a+1) + a

I always try never to give an answer straight up...maybe I could've just given it for a question that simple.
Last edited on
% gives you the remainder after division.

If you want a number within a certain range, you can use % to get that range. x % y will always be < y because you can't have a remainder larger than the number you are dividing by.

Therefore:

1
2
3
4
5
actionchance = rand() % 6; // remainder gives you [0..5]
actionchance += 1;  // add 1 to make it [1..6]

// or this if you want it all on 1 line:
actionchance = rand() % 6 + 1;
Last edited on
closed account (967L1hU5)
Oh yeah, big before small. I'm fairly new to C++, started to get interested around March this year, but never got far passed "Hello World!", but around the start of summer, I tried again, and have made huge progress. So I tend to forget the small things, and semi-colons, alot.
Oh yeah, big before small.


big and small has nothing to do with it. rand() % 6 + 50; is perfectly valid, it just gives you a different range: [50..55]

You should really try to understand what the code is doing, rather than memorizing bits like "big before small" which aren't necessarily true.

rand() % X + Y;
Here, the % makes sure the number is no larger than X. This limits the random numbers to a specific range: [0..X-1]. Adding Y shifts that range so the minimum value is something other than zero: [Y..X+Y-1]

If you must memorize something, memorize one of these:

 
rand() % range + minimum;


or

 
rand() % (maximum - minimum + 1) + minimum
closed account (967L1hU5)
Thanks for the advice, I'm fairly new to the rand() scene, and was just going off what I already knew.
Fair enough. =)

It's more of a math thing than a rand thing. rand gives you a random number. The math that follows it just transforms that number so it is within a certain range.

But yeah I know how it is when you're starting out. =)
closed account (967L1hU5)
And have only been practicing for about 3 months.
Topic archived. No new replies allowed.