classes

This is a discussion on classes within the C++ Programming forums, part of the General Programming Boards category; ive got a problem why does 'the_number' always = 1 ? Code: #include <iostream> #include <ctime> #include <cstring> using namespace ...

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    classes

    ive got a problem why does 'the_number' always = 1 ?
    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
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    because you haven't seeded the random number generator

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Initialize the random generator.

    srand(time(NULL));

    Kuphryn

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    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.
    none...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21