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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird problem with rand() and object

Status
Not open for further replies.

Veneficus

Programmer
Mar 7, 2002
7
US
Hello everyone,

I am having a problem with following code. When I try to access the data member "i" in the Numbers class from the main() it prints "-858993460". But in the constructor it assigns and prints a random number without a problem. I am pulling my hair out since I couldn't figure out what I am doing wrong. I appreciate it if you can land me a hand and tell me what's wrong.

Oh by the way I tried with an accessor function and got the same result.

Thanks,

---------------------------------------------------------------------------------
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;


class Numbers {
public:
int i;
Numbers (int i = 0);
};

Numbers::Numbers (int i)
{
i = rand ();
cout << i << endl;
}



int main (void)
{
srand (time (NULL));
Numbers myNumbers;
cout << myNumbers.i << endl;
return 0;
}
---------------------------------------------------------------------------------



Report this post
 
You assign the wrong i.

class Numbers {
public:
int i; // <== This i is the one you print in main, but you never assign it any value
Numbers (int i = 0);
};

Numbers::Numbers (int i) // <=== This is i is assigned...
{
i = rand (); // ...with a random number
cout << i << endl;
}
 
perfnurt is correct but here is a bit more clarification. Because the parameter to Numbers is also called &quot;i&quot;, when you get into the constructor, you are not accessing your member variable i but the local variable to the constructor, declared in its parameters. If you change the variable name, it will work fine. Also, use of the this pointer would work.

ex:

Numbers::Numbers(int local_i)
{
i = rand();
cout<<i<<endl;
}

However, I dont see a reason to have a value in the constructor. I would suggest, unless there is a reason, to do the following

class Numbers
{
public:

// int i; dont call it i... it can get confusing
int m_randomNumber;

Numbers();
Numbers(int rand_value);
};

Numbers::Numbers()
{
m_randomNumber = rand();
cout<<m_randomNumber<<endl;
}

Numbers::Numbers(int rand_value):m_randomNumber(rand_value)
{}

// On a side note your original constructor would work if you did

Numbers::Numbers(int i)
{
this->i = rand();
cout<<this->i<<endl;
}

Matt

 
Thanks a lot Zyrenthian and PerFnurt. I knew I was doing something stupid. :)

Well with the light of your help I've changed the class definition to following:

class Numbers {
public:
int i;
Numbers (void) {i=rand();}
friend void fill_class (Numbers &);
};

Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top