user3055670
user3055670

Reputation:

rand() not working, gives me a number outside of the specified max and minimum

So I'm making a game were you're the boss an your hiring employees, I've used rand before but for some reason it's not working now:

Code generating the random number:

class employee
{

public:

int getSkills(int askingPrice)

    {
        srand((unsigned)time(0));

        switch(askingPrice)
        {
        case 1: //point of this is so if askingPrice is from 1 to 5 it will execute the same code
        case 2:
        case 3:
        case 4:
        case 5:

            skills = rand() % 5 + 1;

        }
        return skills;
    }
    int returnSkills()
    {
        return skills;
    }

    int getPaid(int askingPrice)
    {
        srand((unsigned)time(0));
        switch(askingPrice)
        {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:


            paid = rand() % 5 + 1;

        }
        return paid;
    }
    int returnPaid()
    {
        return paid;
    }

    int getHappy(int askingPrice)
    {
        srand((unsigned)time(0));
        switch(askingPrice)
        {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:


            happy = rand() % 5 + 1;

        }
        return happy;
    }
    int returnHappy()
    {
        return happy;
    }
private:
    int skills, paid, happy;
};

Code displaying the random number:

std::cout <<"Your first employee is: " << one.returnPaid() << " salary, " <<             one.returnSkills() << " skilled @ his job, " << one.returnHappy() << " happy with his job";

and for some reason it displays as this:

Your first employee is: -858993460 salary, 3 killed @ his job, 3 happy with his job

Why am I getting a negative number for the salary but not anything else?

I'm using Visual Studio 2010

Upvotes: 0

Views: 135

Answers (1)

Oswald
Oswald

Reputation: 31685

When you call one.returnPaid(), one.paid is not yet initialized, because one.getPaid() (the only function where one.paid will receive a value) has not been called yet.

Upvotes: 5

Related Questions