-
classes
ive got a problem why does 'the_number' always = 1 ? :confused:
Code:
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
class game
{
public:
int the_number;
int guess;
};
int main()
{
game guess_game;
guess_game.the_number = rand() %10;
cout <<"the number is "<<guess_game.the_number<<endl;
cin.get();
return 0; // exit success
}
-
because you haven't seeded the random number generator
-
Initialize the random generator.
srand(time(NULL));
Kuphryn
-
cgod,
This means that the problem isn't in the class ( as your thread title implies ), the same thing would happen to a normal variable even if it's not in a class.
-
What's the point of using a class here if you aren't going to have it do anything? A struct would be better here, because it's less typing in that you don't have to make anything implicitly public.
Quzah.