Jump to content
  • Advertisement
Sign in to follow this  
Assassin7257

Generating Random Number OOP

This topic is 2487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

class slot
{
public:
int m_slotpicture; // determines if slot winner

int& generateSlot(int slotNumber); //generate what slot picture is shown

};


int& slot::generateSlot(int slotNumber)
{
for(int i = 0;i = slotNumber; ++i)
{
srand(time(0));
int slot = rand();
int pull = ( (slot % 3 ) + 1);
m_slotpicture = pull;
return pull;
}
}

void checkWinner(int& slot1, int& slot2, int& slot3)
{
if(slot1 = slot2 = slot3)
{
cout << "Congradulations ! You've WON !" << endl;
}

else
{
cout << "You didn't win..." << endl;
}
}

int main()
{
slot slot1;
slot slot2;
slot slot3;

int firstSlot = slot1.generateSlot(1);
int secondSlot = slot2.generateSlot(2);
int thirdSlot = slot3.generateSlot(3);

cout << "The first slot was " << slot1.m_slotpicture << endl;
cout << "The second slot was " << slot2.m_slotpicture << endl;
cout << "The third slot waas " << slot3.m_slotpicture << endl;

checkWinner(firstSlot, secondSlot, thirdSlot);

return 0;
}


I want to try making a slot machine game this time using objects but everytime I render I get the same number how would I do it that I get a different number everytime ?

Share this post


Link to post
Share on other sites
Advertisement

it crashes when i switch the for statement

No, it doesn't.

I have tested out.
int& slot::generateSlot(int slotNumber)
{
for(int i = 0;i = slotNumber; ++i)
{
int slot = rand();
int pull = ( (slot % 3 ) + 6);
m_slotpicture = pull;
return pull;
}
}


Don't "combine/contrive"(?) more, man.

Share this post


Link to post
Share on other sites
Just to explain the previous post: '=' is assignment but, (in)conveniently also returns the assigned value which can then be converted to bool. '==' is comparison. You also don't want to return a reference in generateSlot - that variable is local to the loop, and goes out of scope at the end of the curly bracket. Its value needn't persist between iterations of the loop, nor after the function returns. In practice, it might seem to work for now, but it can, does, and will break.

You seem to use references in a lot of places you don't need to, actually. You might be trying to optimize, but for built-in types the difference is likely to be either none at all or negligible. You want to use const reference for that, anyway - non-const reference lets you directly modify the argument, which is poor documentation for anyone using or reading your code, apart from anything else. If you need a copy, just pass by value.

That for loop, though, is the real problem. Now that you've 'fixed' the condition to be '==', it will first set i to 0, then check if i and slotNumber are equal. If slotNumber is 0, it'll execute the first iteration of the loop and return immediately (i.e. without actually looping). If slotNumber isn't 0, the function will return without actually returning a value. Except that the calling code doesn't know this, so it'll look for the return value and take whatever garbage value it finds there. Since generateNumber returns a reference, this probably means it later tries to read from an address that it doesn't have access to, and crashes with an access violation or segmentation fault. (When you say 'it crashes', you need to give us more detail about how, and what information you get when it does so - but I'm assuming it's one of these two.)

I think you want to delete the for loop entirely, and remove the slotNumber parameter which seems completely superfluous. Removing srand will fix the 'same number every time' problem.

Share this post


Link to post
Share on other sites
Removing srand will fix the 'same number every time' problem.[/quote]
smile.png .

It can be done using a lot of ways.

Share this post


Link to post
Share on other sites

int slot::generateSlot()
{
int pull = ( (rand() % 3 ) + 1);
m_slotpicture = pull;
return pull;
}


I changed it to this and now the numbers 3 3 2 keep showing everytime i Run it

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!