PDA

View Full Version : C++ Lottery Program


mscags
11-15-2005, 09:00 PM
So I'm in an intro class this semester and I have to write a lottery program. I was wondering if any of you guys that are programers or what not would mind taking a look at the code and see if you can find any glaring mistakes. A couple of things I'm not sure about 1) I need to randomize the numbers in the lottery array. I'm not sure if the code for that is right. 2) I need the user to be able to repeat the program if desired. Is my way alright or should I do something different? I was supposed to use functions for this assignment (hence all of the functions) Anyway, thanks in advance.

Mike

//This is a lottery program. It will ask the user in imput five numbers and will then store
//them as an array. The program will then generate 5 numbers and will check to see if they match.
//If they do, the user will be proclaimed the grand winner!

#include <iostream>
#include <cstdlib>
using namespace std;

void lottery() //Gets the random numbers.
{
unsigned seed;
cout << "Enter a seed value."
cin >> seed;
srand(seed);
y = 1 + rand() % 36;
cout << rand(LotteryNumber[0];
cout << rand(LotteryNumber[1];
cout << rand(LotteryNumber[2];
cout << rand(LotteryNumber[3];
cout << rand(LotteryNumber[4];
cout << rand(LotteryNumber[5];

return 0;
}

void numbercompare() //Compares to see if the user won the lottery.
{
if (LotteryNumber[0] == UserArray[0] && LotteryNumber[1] == UserArray[1] && LotteryNumber[2] == UserArray[2] &&
LotteryNumber[3] == UserArray[3] && LotteryNumber[4] == UserArary[4] && LotteryNumber[5] == UserArray[5])
cout << "CONGRATULATIONS!!! YOU'RE THE GRAND WINNER!!!";
else
cout << "Sorry you didn't win the grand prize. Please try again!";

return 0;
}


void UserNumbers() //Asks the user to imput five numbers to use for the lottery program.
{
const int Numbers = 5 //Amount of numbers needed for the lottery program.
int LotteryNumbers[5], UserArray[Numbers];
int count;

cout << "Enter the five numbers that you want to use for the lottery program.";
for (count = 0; count < Numbers count++)
cin >> UserArrary[count];
cout << "Here are the numbers that you entered.";
for (int index = 0; index < Numbers; index++;
cout << UserArray[index] << endl;

return 0;

}

int main()
{
char decision;

cout << This is the beginning of the lottery program.";
UserNumbers();
lottery();
numbercompare();
cout << "Would you like to repeat the program? Y or N";
cin >> decision;
if (decision == 'Y' or 'y')
UserNumbers();
else
cout << "Thanks for playing the lottery program!";
return 0;
}

TheIrishThug
11-15-2005, 11:43 PM
u r missing a few )'s in places.
if its a void function, u don't need a return.
shouldn't u be passing the arrays to the functions.
u never use/initialize y.


i'm not sure how u have ur lottery set up. do u want it to pull 1-36, and then have the next number be 1-36 or do u not want to allow for repeated numbers.
if u r not allowing repeated numbers ur program will say 2 4 6 8 10 is wrong if the numbers come 10 8 6 4 2.

mscags
11-15-2005, 11:46 PM
[ QUOTE ]
u r missing a few )'s in places.
if its a void function, u don't need a return.
shouldn't u be passing the arrays to the functions.
u never use/initialize y.


i'm not sure how u have ur lottery set up. do u want it to pull 1-36, and then have the next number be 1-36 or do u not want to allow for repeated numbers.
if u r not allowing repeated numbers ur program will say 2 4 6 8 10 is wrong if the numbers come 10 8 6 4 2.

[/ QUOTE ]

It doesn't matter if the numbers are repeated or not. I think I fixed most of my )'s problems. Am I storing the random numbers into the lottery array correctly? I don't think so, I keep getting some errors. Thanks for the help.

Mike

TheIrishThug
11-16-2005, 12:31 AM
if u r gonna use fuctions (which, in this program, only make the program easier to read) u need to pass the arrays to the functions. initialize them in the main, then have:
void FunctionName ( int ArrayName[], int ArrayTwoIfNeeded [])

mscags
11-16-2005, 12:44 AM
IS this more what you are meaning? Thanks for the help, clearly I suck at this /images/graemlins/frown.gif

void lottery(LotteryNumbers[5]) //Gets the random numbers.
{
srand((unsigned)time(0));
int random_integer;
int LotteryNumber[5];
random_integer = (rand()%36)+1;
cout << rand();
cout << rand(LotteryNumber[0]);
cout << rand(LotteryNumber[1]);
cout << rand(LotteryNumber[2]);
cout << rand(LotteryNumber[3]);
cout << rand(LotteryNumber[4]);
cout << rand(LotteryNumber[5]);

}

void numbercompare() //Compares to see if the user won the lottery.
{

if (LotteryNumber[0] == UserArray[0] && LotteryNumber[1] == UserArray[1] && LotteryNumber[2] == UserArray[2] &&
LotteryNumber[3] == UserArray[3] && LotteryNumber[4] == UserArary[4] && LotteryNumber[5] == UserArray[5])
cout << "CONGRATULATIONS!!! YOU'RE THE GRAND WINNER!!!";
else
cout << "Sorry you didn't win the grand prize. Please try again!";

}


void UserNumbers(UserArray[Numbers]) //Asks the user to imput five numbers to use for the lottery program.
{

cout << "Enter the five numbers that you want to use for the lottery program.";
for (count = 0; count < Numbers count++)
cin >> UserArrary[count];
cout << "Here are the numbers that you entered.";

for (int index = 0; index < Numbers; index++)
cout << UserArray[index] << endl;

}

int main()
{
char decision;
const int Numbers = 5 //Amount of numbers needed for the lottery program.
int LotteryNumbers[5], UserArray[Numbers];
int count;
cout << This is the beginning of the lottery program.";
UserNumbers();
lottery();
numbercompare();
cout << "Would you like to repeat the program? Y or N";
cin >> decision;
if (decision == 'Y' or 'y')
UserNumbers();
else
cout << "Thanks for playing the lottery program!";
return 0;
}

mscags
11-16-2005, 01:08 AM
I fixed a few other things and I think this was more along the lines of what you were talking about. I'm still getting a bunch of undeclared identifier errors. Any thoughts?

//This is a lottery program. It will ask the user in imput five numbers and will then store
//them as an array. The program will then generate 5 numbers and will check to see if they match.
//If they do, the user will be proclaimed the grand winner!

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//Function Declarations
void lottery(int);
void numbercompare();
void UserNumbers(int);

void lottery(LotteryNumbers[5]) //Gets the random numbers.
{
srand((unsigned)time(0));
int random_integer;
int LotteryNumber[5];
random_integer = (rand()%36)+1;
cout << rand();
cout << rand(LotteryNumber[0]);
cout << rand(LotteryNumber[1]);
cout << rand(LotteryNumber[2]);
cout << rand(LotteryNumber[3]);
cout << rand(LotteryNumber[4]);
cout << rand(LotteryNumber[5]);

}

void numbercompare() //Compares to see if the user won the lottery.
{

if (LotteryNumber[0] == UserArray[0] && LotteryNumber[1] == UserArray[1] && LotteryNumber[2] == UserArray[2] &&
LotteryNumber[3] == UserArray[3] && LotteryNumber[4] == UserArary[4] && LotteryNumber[5] == UserArray[5])
cout << "CONGRATULATIONS!!! YOU'RE THE GRAND WINNER!!!";
else
cout << "Sorry you didn't win the grand prize. Please try again!";

}


void UserNumbers(UserArray[Numbers]) //Asks the user to imput five numbers to use for the lottery program.
{

cout << "Enter the five numbers that you want to use for the lottery program.";
for (count = 0; count < Numbers count++)
cin >> UserArrary[count];
cout << "Here are the numbers that you entered.";

for (int index = 0; index < Numbers; index++)
cout << UserArray[index] << endl;

}

int main()
{
char decision;
const int Numbers = 5 //Amount of numbers needed for the lottery program.
int LotteryNumbers[5], UserArray[Numbers];
int count;
cout << This is the beginning of the lottery program.";
UserNumbers();
lottery();
numbercompare();
cout << "Would you like to repeat the program? Y or N";
cin >> decision;
if (decision == 'Y' or 'y')
UserNumbers();
else
cout << "Thanks for playing the lottery program!";
return 0;
}

StevieG
11-16-2005, 01:54 AM
[ QUOTE ]

i'm not sure how u have ur lottery set up. do u want it to pull 1-36, and then have the next number be 1-36 or do u not want to allow for repeated numbers.
if u r not allowing repeated numbers ur program will say 2 4 6 8 10 is wrong if the numbers come 10 8 6 4 2.

[/ QUOTE ]

Yeah, I think an array is the wrong way to go here for the user input and the drawing.

Why not use bitwise operations? 5 bytes gets you your 36 "slots", initialize to all 0s. Store the picks and the drawing by toggling the Nth entry if that number is selected.

Then you can AND when picking the random numbers to see if the number was already picked and do it again.

For checking winning, you just check if all bits are the same.

TheIrishThug
11-16-2005, 02:08 AM
ok, i got it to work, u should check the logic to make sure it works. i just got rid of the bugs. u had a lot of spelling errors and it looks like u really need to study the language a little more.

code:
//This is a lottery program. It will ask the user in imput five numbers and will then store
//them as an array. The program will then generate 5 numbers and will check to see if they match.
//If they do, the user will be proclaimed the grand winner!

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//Function Declarations
void lottery(int LotteryNumbers[]);
void numbercompare(int LotteryNumber[], int UserArray[]);
void UserNumbers(int UserArray[], int Numbers);

void lottery(int LotteryNumbers[5]) //Gets the random numbers.
{
srand(time(0));
int random_integer;
cout<<"The winning numbers are:"<<endl;
for (int x; x<5; x++)
{
LotteryNumbers[x]=((rand()%36)+1);
cout<<LotteryNumbers[x]<<endl;
}
}

void numbercompare(int LotteryNumber[], int UserArray[]) //Compares to see if the user won the lottery.
{

if (LotteryNumber[0] == UserArray[0] && LotteryNumber[1] == UserArray[1] && LotteryNumber[2] == UserArray[2] &&
LotteryNumber[3] == UserArray[3] && LotteryNumber[4] == UserArray[4])
cout << "CONGRATULATIONS!!! YOU'RE THE GRAND WINNER!!!"<<endl;
else
cout << "Sorry you didn't win the grand prize. Please try again!"<<endl;

}


void UserNumbers(int UserArray[5], int Numbers) //Asks the user to imput five numbers to use for the lottery program.
{

cout << "Enter the five numbers that you want to use for the lottery program."<<endl;
for (int count = 0; count < Numbers; count++)
cin >> UserArray[count];
cout << "Here are the numbers that you entered."<<endl;

for (int index = 0; index < Numbers; index++)
cout << UserArray[index] << endl;

}

int main()
{
char decision;
const int Numbers = 5; //Amount of numbers needed for the lottery program.
int LotteryNumbers[5], UserArray[5];
int count;
do {
cout << "This is the beginning of the lottery program."<<endl;
UserNumbers(UserArray, Numbers);
lottery(LotteryNumbers);
numbercompare(LotteryNumbers, UserArray);
cout << "Would you like to repeat the program? Y or N"<<endl;
cin >> decision;
} while (decision == 'Y' || decision == 'y');
cout << "Thanks for playing the lottery program!";

return 0;
}

mscags
11-16-2005, 02:18 AM
[ QUOTE ]
[ QUOTE ]

i'm not sure how u have ur lottery set up. do u want it to pull 1-36, and then have the next number be 1-36 or do u not want to allow for repeated numbers.
if u r not allowing repeated numbers ur program will say 2 4 6 8 10 is wrong if the numbers come 10 8 6 4 2.

[/ QUOTE ]

Yeah, I think an array is the wrong way to go here for the user input and the drawing.

Why not use bitwise operations? 5 bytes gets you your 36 "slots", initialize to all 0s. Store the picks and the drawing by toggling the Nth entry if that number is selected.

Then you can AND when picking the random numbers to see if the number was already picked and do it again.

For checking winning, you just check if all bits are the same.

[/ QUOTE ]

My prof made it a requirement that we use arrays in this program for some weird reason /images/graemlins/confused.gif

Nomad84
11-16-2005, 02:20 AM
Disregard. Looks like the above poster knows much more about this than I do. I just glanced at his code, but it looks pretty solid to me.

mscags
11-16-2005, 02:44 AM
This looks great. Thanks for all of your help. I really apprecaite it /images/graemlins/smile.gif Yes I think I do need to learn the language a little better.

StevieG
11-16-2005, 12:45 PM
[ QUOTE ]
ok, i got it to work, u should check the logic to make sure it works.

[/ QUOTE ]

Yeah, I think you got it to compile, but there are still some serious flaws.

* As implemented, it is possible for the same number to come up more than once in the drawing.

* There is no input checking to verify that these are even integers, let alone that there are no repeats in the picks.

* There is no sorting of either the drawing or the picks, and so the comparison could fail to find a winning selection.