Random Number Generator Program...PLEASE HELP!!

This is a discussion on Random Number Generator Program...PLEASE HELP!! within the C++ Programming forums, part of the General Programming Boards category; Hi all, This is my first experience with c++ programming. I am struggling with our first assignment to create a ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    Exclamation Random Number Generator Program...PLEASE HELP!!

    Hi all,

    This is my first experience with c++ programming. I am struggling with our first assignment to create a program that generates 10 random numbers. We are supposed to separate the implementation and the interface, so I have a header file (rng.h) and 2 cpp files (rng.cpp, main. cpp). Can someone please help me out?? I'm not even sure if I'm on the right track and I'm getting 5 errors when I compile.

    Any help is GREATLY appreciated.

    Thanks in advance.

    rng.h
    Code:
    #ifndef RNG_H
    #define RHG_H
    
    //aRandomNumberGenerator class definition
    class aRandomNumberGenerator
    {
    public:
         //Constructor initializes aRandomNumberGenerator
         aRandomNumberGenerator();
         void setSeed(unsigned long);
         double generate();
            
    private:
         unsigned long seed;
    }; //end of class aRandomNumberGenerator
    
    #endif
    rng.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    #include "rng.h"
    
    using namespace std;
    
    void aRandomNumberGenerator::setSeed(unsigned long)
    {
    	seed = 2345;
    	srand(seed);
    }
    
    double aRandomNumberGenerator::generate()
    {
    	return rand();
    }
    main.c
    Code:
    #include <iostream>
    #include <cstdlib>
    
    #include "rng.h"
    #include "rng.cpp"
    
    int main()
    {
    	cout<<"Ten random numbers are: "<<endl;
    
    	for(double i = 0; i<=10; i++)
    	{
    		cout<<generate();
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    main.c , you might need to use namespace std.. as in


    Code:
    using namespace std;
    when you use generate() , you need to make an object of arandomnumbergenerator first, since you made it so its in a class

    Code:
    aRandomNumberGenerator myrng;
    cout << myrng.generate(); // like this

    click here for more help
    Last edited by rodrigorules; 06-14-2010 at 09:15 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by girly_engineer View Post
    I'm getting 5 errors when I compile.
    The exact number of errors you are getting is not as useful for us as the actual errors themselves are. You may not be able to understand them, but we certainly can, so post em!

    I can't speak for everyone but I almost never bother creating a blank project and copy and pasting your code in, and then compiling it. If you're using a different compiler you might get some different errors anyway, so we need to see your compilation errors!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    106
    Code:
    #include <iostream>
    #include <math.h>
    #include <time.h>
    #include "numbergenerator.h"//what i named header file
    
    using namespace std;
    
    int main()
    {
        
        random generate;
        
        
        srand(time(NULL));
        for (int loopNumber = 0; loopNumber < 10; loopNumber++)
        {
            int randomNumber = generate.generateRandomNumber();
            cout << randomNumber << endl; 
        }
        cin.get();
        
    }
    Header file contents:
    Code:
    class random {
          public:
                 int generateRandomNumber()
                 {                  
                     return rand();
                                                            
                 }
    };
    yeah so im a noob so there may be problems with this... all i know is that it works

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Ok these are the errors I am currently getting.

    1>------ Build started: Project: Project 1, Configuration: Debug Win32 ------
    1>Compiling...
    1>rng.cpp
    1>main.cpp
    1>t:\remydoc\logu17\visual studio 2008\projects\project 1\project 1\rng.h(6) : error C2011: 'aRandomNumberGenerator' : 'class' type redefinition
    1> t:\remydoc\logu17\visual studio 2008\projects\project 1\project 1\rng.h(6) : see declaration of 'aRandomNumberGenerator'
    1>t:\remydoc\logu17\visual studio 2008\projects\project 1\project 1\rng.cpp(7) : error C2027: use of undefined type 'aRandomNumberGenerator'
    1> t:\remydoc\logu17\visual studio 2008\projects\project 1\project 1\rng.h(6) : see declaration of 'aRandomNumberGenerator'
    1>t:\remydoc\logu17\visual studio 2008\projects\project 1\project 1\rng.cpp(13) : error C2027: use of undefined type 'aRandomNumberGenerator'
    1> t:\remydoc\logu17\visual studio 2008\projects\project 1\project 1\rng.h(6) : see declaration of 'aRandomNumberGenerator'
    1>Generating Code...
    1>Build log was saved at "file://t:\ReMyDoc\logu17\Visual Studio 2008\Projects\Project 1\Project 1\Debug\BuildLog.htm"
    1>Project 1 - 3 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    These are the files I'm compiling with changes

    rng.h
    Code:
    #ifndef RNG_H
    #define RHG_H
    
    //aRandomNumberGenerator class definition
    class aRandomNumberGenerator
    {
    public:
         //Constructor initializes aRandomNumberGenerator
         aRandomNumberGenerator();
         void setSeed(unsigned long);
         double generate();
            
    private:
         unsigned long seed;
    }; //end of class aRandomNumberGenerator
    
    #endif
    rng.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    #include "rng.h"
    
    using namespace std;
    
    void aRandomNumberGenerator::setSeed(unsigned long)
    {
    	int seed = 2345;
    	srand(seed);
    }
    
    double aRandomNumberGenerator::generate()
    {
    	double r = rand();
    	return r;
    }
    main.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    
    #include "rng.h"
    #include "rng.cpp"
    
    using namespace std;
    
    int main()
    {
    	
    	cout<<"Ten random numbers are: "<<endl;
    
    	for(int i = 0; i<=10; i++)
    	{
    		cout<<generate() ;
    	}
    	return 0;
    }

  7. #7
    Registered User skreaminskull's Avatar
    Join Date
    Jan 2009
    Posts
    7

    Thumbs up

    I think you are getting the first error,
    C2011: 'aRandomNumberGenerator' : 'class' type redefinition

    because you the way main.cpp is included rng.h and rng.cpp. Even though you have the header guard set up in rng.h, I think the compiler is trying to include it twice...once from the include rng.h in main.cpp and coming over with rng.cpp as well. Hope this makes sense.

    Try removing the include for rng.h from main.cpp and see if that will get your further.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Ok so I revised my code and now I'm getting one error:

    1>------ Build started: Project: Project 1, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.cpp
    1>t:\remydoc\logu17\visual studio 2008\projects\project 1\project 1\main.cpp(16) : error C2660: 'aRandomNumberGenerator::setSeed' : function does not take 0 arguments
    1>Build log was saved at "file://t:\ReMyDoc\logu17\Visual Studio 2008\Projects\Project 1\Project 1\Debug\BuildLog.htm"
    1>Project 1 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    rng. h
    Code:
    #ifndef RNG_H
    #define RHG_H
    
    //aRandomNumberGenerator class definition
    class aRandomNumberGenerator
    {
    public:
         //Constructor initializes aRandomNumberGenerator
         aRandomNumberGenerator();
         void setSeed(unsigned long);
         double generate();
            
    private:
         unsigned long seed;
    }; //end of class aRandomNumberGenerator
    
    #endif
    rng.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    #include "rng.h"
    
    using namespace std;
    
    void aRandomNumberGenerator::setSeed(unsigned long)
    {
    	int seed = 2345;
    	srand(seed);
    }
    
    double aRandomNumberGenerator::generate()
    {
    	
    	for(int i = 0; i<=10; i++)
    	{
    		double r = rand();
    		return r;
    		cout<<r ;
    	}
    	
    }
    main.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    
    
    #include "rng.h"
    
    using namespace std;
    
    int main()
    {
    	
    	cout<<"Ten random numbers are: "<<endl;
    	
    	aRandomNumberGenerator rand;
    
    	rand.setSeed();
    	rand.generate();
    	
    }

  9. #9
    Registered User skreaminskull's Avatar
    Join Date
    Jan 2009
    Posts
    7
    You need to pass something to your setSeed() function...you have it defined as
    void aRandomNumberGenerator::setSeed(unsigned long) in rng.cpp. Something like this in main.cpp

    rand.setSeed(1);

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    When I pass a value to setSeed I get this error:

    1>------ Build started: Project: Project 1, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.cpp
    1>Linking...
    1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall aRandomNumberGenerator::aRandomNumberGenerator(voi d)" (??0aRandomNumberGenerator@@QAE@XZ) referenced in function _main
    1>t:\ReMyDoc\logu17\Visual Studio 2008\Projects\Project 1\Debug\Project 1.exe : fatal error LNK1120: 1 unresolved externals
    1>Build log was saved at "file://t:\ReMyDoc\logu17\Visual Studio 2008\Projects\Project 1\Project 1\Debug\BuildLog.htm"
    1>Project 1 - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  11. #11
    Registered User skreaminskull's Avatar
    Join Date
    Jan 2009
    Posts
    7
    Add a constructor call in rng.cpp after
    Code:
    using namespace std;
    
    aRandomNumberGenerator::aRandomNumberGenerator() {
    	
    }

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Ok I'm trying this, but still getting an error:

    Code:
    aRandomNumberGenerator::aRandomNumberGenerator()
    {
    	setSeed();
    }

  13. #13
    Registered User skreaminskull's Avatar
    Join Date
    Jan 2009
    Posts
    7
    I actually got your first source code post to work with a few modifications. First was to add a constructor call to rng.cpp ( aRandomNumberGenerator::aRandomNumberGenerator() ), second was to remove the include of rng.h in main.cpp and I also added an endl to your print out of the random numbers for readability...the following code compiled for me using the g++ compiler on a CentOS 5.4 machine.

    rng.h
    Code:
    #ifndef RNG_H
    #define RHG_H
    
    //aRandomNumberGenerator class definition
    class aRandomNumberGenerator {
    public:
         //Constructor initializes aRandomNumberGenerator
         aRandomNumberGenerator();
         void setSeed(unsigned long);
         double generate();
            
    private:
         unsigned long seed;
    }; //end of class aRandomNumberGenerator
    
    #endif
    rng.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    #include "rng.h"
    
    aRandomNumberGenerator::aRandomNumberGenerator() {
    	
    }
    
    void aRandomNumberGenerator::setSeed(unsigned long)
    {
    	seed = 2345;
    	srand(seed);
    }
    
    double aRandomNumberGenerator::generate()
    {
    	return rand();
    }
    main.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    
    #include "rng.cpp"
    
    using namespace std;
    
    int main(void) {
    
    	aRandomNumberGenerator myrng;
    	
    	cout << "Ten random numbers are: " << endl;
    
    	for(double i = 0; i<=10; i++) {
    		cout << myrng.generate() << endl;
    	}
    
    	return 0;
    }

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    I'm using Visual Studio 2008 and I got errors when I ran the code in the post above.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    So, what are the errors?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  2. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  3. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 07:40 AM

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