DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Generate 6 Random Numbers (http://www.daniweb.com/forums/thread157349.html)

techgenie Nov 14th, 2008 11:52 am
Generate 6 Random Numbers
 
Hi guys
I am working on a Lottery program to generate 6 unique numbers. So far I have the following:

CLASS LOOKS LIKE THIS:
#include "stdafx.h"
#include <string>
#include <ctime>
#pragma once
#define WIN32_LEAN_AND_MEAN                // Exclude rarely-used stuff from Windows headers

class Lottery // Lottery class definition
{
public:
        Lottery(); // constructor that initializes a Lottery object
        void setLottery(int []); // function that gets lottery numbers
        int getLottery(); // function that retrieve the lottery numbers
        bool checkLottery(int, int []); // function that evaluates the lottery numbers
        void displayLottery(int []); // function that displays the lottery numbers

private:
        int lotNumbers[6]; // lottery number
}; // end of class Lottery
#include "stdafx.h"
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <string>
#include <ctime>
using std::string;

#include "lottery.h" // include definition of class lottery

// constructor for the Lottery
Lottery::Lottery()
{

} // end of Lottery constructor

bool Lottery::checkLottery(int lotnum, int lottery_numbers[6])
{
        for (int i=0; i < 6; i++)
        {
                if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0)
                {
                        return false;
                }
        }
        return true;
}
void Lottery::setLottery(int lottery_numbers[6])
{
        lotNumbers[6] = lottery_numbers[6];
}

int Lottery::getLottery()
{
        return lotNumbers[6];
    int lot_count = 0; // counter for the number of valid lottery numbers found
        while (lot_count < 6)
        {
                time_t = t;
                srand(unsigned (time(&t)));
                int lotnum = (rand() * 53);
                if (checkLottery(lotnum, lotnumbers))
                {
                        lotnumbers[lot_count] = lotnum;
                        lot_count++;
                }
        }
}

void Lottery::displayLottery()
{
        getLottery();
        for (int j = 0; j < 6; j++)
        {
                cout << getLottery() << " ";
        }
        getche();
}
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
#include <ctime>

#include "lottery.h" // iinclude definition of class lottery

// function main begins program execution

int main()
{
        int lottery_numbers[6];

        Lottery lotteryDraw; //create Lottery object

        lotteryDraw.displayLottery();

        return 0; // indicate successful termination
       
} // end main function
I am getting the following errors and I do not understand where to go from here.
wrig_9.cpp
c:\users\jeannie\documents\visual studio 2005\projects\wrig_9\wrig_9\wrig_9.cpp(25) : error C2660: 'Lottery::displayLottery' : function does not take 0 arguments
lottery.cpp

c:\users\jeannie\documents\visual studio 2005\projects\wrig_9\wrig_9\lottery.cpp(47) : error C2513: '__time64_t' : no variable declared before '='
c:\users\jeannie\documents\visual studio 2005\projects\wrig_9\wrig_9\lottery.cpp(47) : error C2065: 't' : undeclared identifier
c:\users\jeannie\documents\visual studio 2005\projects\wrig_9\wrig_9\lottery.cpp(48) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Generating Code...
Build log was saved at "file://c:\Users\Jeannie\Documents\Visual Studio 2005\Projects\wrig_9\wrig_9\Debug\BuildLog.htm"
wrig_9 - 4 error(s), 0 warning(s)
====================================
I think the arrays are not declared properly and the calls are inaccurate.
Any help is appreciated. Please let me know what I am doing wrong.

Thanks so much
techgenie

===============

Narue Nov 14th, 2008 12:14 pm
Re: Generate 6 Random Numbers
 
>error C2660: 'Lottery::displayLottery' : function does not take 0 arguments
>void displayLottery(int []);
Looks like it takes one argument to me. You can't lie to your compiler, it doesn't work.

>error C2513: '__time64_t' : no variable declared before '='
>time_t = t;
Yep, this error is pretty straightforward too. You need to name your variables.

skatamatic Nov 14th, 2008 1:21 pm
Re: Generate 6 Random Numbers
 
Why are you passing an array to displayLottery (in the declaration), it's not used in the method? Also, when you set the lottery you are only setting index # 6. And if you had properly passed your array from main it would be an out of range index (which will throw an exception).

Also, when you generate random numbers, use a mod (%) operator, not a * operator. I may be wrong, but I think it's possible for this to generate a int so big that it's bigger than an int's range.
rand() % (MAX + 1)
will generate a number between 0 and MAX, inclusive.

techgenie Nov 14th, 2008 2:33 pm
Re: Generate 6 Random Numbers
 
Thank you skatamatic for that suggestion on the modulus (%). Narue I realized I had omitted the <ctime> and <cstdlib> libraries. After adding them it made it easier for me to identify the other errors.

As the program stands right now; it has finally compiled but I do not get random numbers between 1 and 53, as it should. I am getting an extremely large number out of range
(- 858993460). I get 6 of the same numbers.
Any suggestions?

The modified function definitions are:
#include "stdafx.h"
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

#include "lottery.h" // include definition of class lottery

// constructor for the Lottery
Lottery::Lottery()
{

} // end of Lottery constructor

bool Lottery::checkLottery(int lotnum, int lottery_numbers[6])
{
       
        for (int i=0; i < 6; i++)
        {
                lottery_numbers[i] = i * i; //store lottery numbers in the index
                if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0)
                {
                        return false;
                }
        }
        return true;
}
void Lottery::setLottery(int lottery_numbers[6])
{
        lotNumbers[6] = lottery_numbers[6];
}

int Lottery::getLottery()
{
        return lotNumbers[6];
    int lot_count = 0; // counter for the number of valid lottery numbers found
        while (lot_count < 6)
        {
        time_t t;
                srand(unsigned (time(&t)));
                int lotnum = 1 + rand() % 53;
                if (checkLottery(lotnum, lotNumbers))
                {
                        lotNumbers[lot_count] = lotnum;
                        lot_count++;
                }
        }
}

void Lottery::displayLottery(int [])
{
        getLottery();
        for (int j = 0; j < 6; j++)
        {
                cout << getLottery() << " " << endl;
        }
}

Narue Nov 14th, 2008 2:35 pm
Re: Generate 6 Random Numbers
 
Adding code tags to other people's posts gets old very quickly. Please start using code tags when you post code.

techgenie Nov 14th, 2008 4:54 pm
Re: Generate 6 Random Numbers
 
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

#include "lottery.h" // include definition of class lottery

// constructor for the Lottery
Lottery::Lottery()
{

} // end of Lottery constructor

bool Lottery::checkLottery(int lotnum, int lottery_numbers[6])
{
       
        for (int i=0; i < 6; i++)
        {
                lottery_numbers[i] = i * i; //store lottery numbers in the index
                if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0)
                {
                        return false;
                }
        }
        return true;
}
void Lottery::setLottery(int lottery_numbers[6])
{
        lotNumbers[6] = lottery_numbers[6];
}

int Lottery::getLottery()
{
        return lotNumbers[6];
    int lot_count = 0; // counter for the number of valid lottery numbers found
        while (lot_count < 6)
        {
        time_t t;
                srand(unsigned (time(&t)));
                int lotnum = 1 + rand() % 53;
                if (checkLottery(lotnum, lotNumbers))
                {
                        lotNumbers[lot_count] = lotnum;
                        lot_count++;
                }
        }
}

void Lottery::displayLottery(int [])
{
        getLottery();
        for (int j = 0; j < 6; j++)
        {
                cout << getLottery() << " " << endl;
        }
}

Hope this helps!

skatamatic Nov 14th, 2008 6:11 pm
Re: Generate 6 Random Numbers
 
Why are you returning the 6th lottery number (again, out of range) in getLottery()? You are using arrays incorrectly. Each entry in the array needs to be accessed seperately. And what's with this line?
lottery_numbers[i] = i * i; //store lottery numbers in the
???
This program is pretty erronous and confusing. Fix up your syntax issues with your class methods, array passing, and loop techniques. Use google for syntax issues.

techgenie Nov 16th, 2008 12:15 am
Re: Generate 6 Random Numbers
 
I have been working on this program for quite some time and I have gotten it to compile and execute but I am getting a Debug error - Runtime Check Failure #2 Stack around the variable 'fllottery_numbers was corrupted.
the only function that has 'fllottery_numbers is the function below.
Can anyone help with resolving this issue.
Thanks for your time and patience.

void Lottery::displayLottery()
{
        int fllottery_numbers[6];
        flLotteryDraw(fllottery_numbers);
       
        for (int j = 0; j < 6; j++)
        {
                cout << fllottery_numbers[j] << " " << endl;
        }
}

skatamatic Nov 16th, 2008 4:24 pm
Re: Generate 6 Random Numbers
 
What is flLotterDraw doing? It's not in the previous code. Whatever it's doing, it's corrupting your array. Post that function.


All times are GMT -4. The time now is 9:33 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC