I am creating a lottery game in C++.
I created a function called GetLottoPicks which asks the user for 7 numbers.
I have another function called GenWinNums that generates 7 random numbers.
The problem is for the GenWinNums it keeps generating this 7 times: -858993460
I believe I made inputs into the argument int WinningNums[] bu apparently not?
Thank you.
int main()
{
string name;
int choice;
int user_picked[7];
int chosen_lotto[7];
cout << "LITTLETON CITY LOTTO MODEL:" << endl;
cout << "---------------------------" << endl;
cout << " 1) Play Lotto " << endl;
cout << " Q) Quit Program " << endl;
cout << " Please make a selection: ";
cin >> choice;
if (choice == 1)
{
cout << "\n Please enter your name: ";
cin >> name;
getLottoPicks(user_picked);
GenWinNums(chosen_lotto);
for(int i = 0; i < 7; i++)
{
cout << chosen_lotto[i];
}
for(int i = 0; i < 7; i++)
{
cout << user_picked[i];
}
}
else if (choice == 'Q' || 'q')
{
exit(0);
}
else
{
cout << "That is not a valid choice." << endl;
}
system("PAUSE");
return 0;
}
int getLottoPicks(int UserTicket[])
{
const int amount = 7;
UserTicket[amount];
int ticket = UserTicket[amount];
for (int i = 0; i < amount; i++)
{
cout << "\n Please enter number " << i + 1 << ":";
cin >> UserTicket[i];
while (UserTicket[i] < 1 || UserTicket[i] > 40)
{
cout << "Your selection cannot be less than 1 and/or greater than 40. Try Again." << endl;
cin >> UserTicket[i];
}
if(i > 0)
{
for(int check = 0; check < i; check++)
{
while(UserTicket[i] == UserTicket[check])
{
cout << "You cannot use the same lotto number twice. Try again: " << endl;
cin >> UserTicket[i];
}
}
}
}
return ticket;
}
int GenWinNums(int WinningNums[])
{
const int amount = 7;
int numbers[amount];
int ticket = WinningNums[amount];
for(int i = 0; i < amount; i++)
{
numbers[i] = (rand() % 40) + 1;
while(numbers[i] < 1 || numbers[i] > 40)
{
numbers[i] = (rand() % 10) + 1;
}
if(i > 0)
{
for(int check = 0; check < i; check++)
{
while(numbers[i] == numbers[check])
{
numbers[i] = (rand() % 10) + 1;
}
}
}
}
return ticket;
}
Looks like you are never assigning any values to the
WinningNumsarray in your routine, that might explain why you get the same number all the time.I’d suggest removing the
numbersarray and working directly withWinningNumsto generate your random numbers.