Two Plus Two Older Archives  

Go Back   Two Plus Two Older Archives > General Gambling > Computer Technical Help
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 11-15-2005, 09:00 PM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default C++ Lottery Program

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;
}
Reply With Quote
  #2  
Old 11-15-2005, 11:43 PM
TheIrishThug TheIrishThug is offline
Senior Member
 
Join Date: Jan 2005
Location: playing nl omaha h/l stud
Posts: 204
Default Re: C++ Lottery Program

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.
Reply With Quote
  #3  
Old 11-15-2005, 11:46 PM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default Re: C++ Lottery Program

[ 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
Reply With Quote
  #4  
Old 11-16-2005, 12:31 AM
TheIrishThug TheIrishThug is offline
Senior Member
 
Join Date: Jan 2005
Location: playing nl omaha h/l stud
Posts: 204
Default Re: C++ Lottery Program

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 [])
Reply With Quote
  #5  
Old 11-16-2005, 12:44 AM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default Re: C++ Lottery Program

IS this more what you are meaning? Thanks for the help, clearly I suck at this [img]/images/graemlins/frown.gif[/img]

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;
}
Reply With Quote
  #6  
Old 11-16-2005, 01:08 AM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default New Code

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;
}
Reply With Quote
  #7  
Old 11-16-2005, 01:54 AM
StevieG StevieG is offline
Senior Member
 
Join Date: Jan 2003
Location: Baltimore, MD, USA
Posts: 157
Default Re: C++ Lottery Program

[ 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.
Reply With Quote
  #8  
Old 11-16-2005, 02:08 AM
TheIrishThug TheIrishThug is offline
Senior Member
 
Join Date: Jan 2005
Location: playing nl omaha h/l stud
Posts: 204
Default Re: C++ Lottery Program

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;
}
Reply With Quote
  #9  
Old 11-16-2005, 02:18 AM
mscags mscags is offline
Senior Member
 
Join Date: Mar 2005
Location: Between Two Hot Twins
Posts: 713
Default Re: C++ Lottery Program

[ 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 [img]/images/graemlins/confused.gif[/img]
Reply With Quote
  #10  
Old 11-16-2005, 02:20 AM
Nomad84 Nomad84 is offline
Senior Member
 
Join Date: Mar 2005
Posts: 194
Default Re: New Code

[edit] 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. [/edit]
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.