Question about Array,functions, and random- Lotto Numbers
Nov 18, 2013 at 3:50am UTC
The code works up to the input of the 7th number and after that it stops.
I then tried switching the order of the functions being called and noticed it was the GetWinNum function that is causing trouble. Any help in fixing it, I have tried moving the srand from main to inside the function.
The program needs 7 numbers from the user and the GetWinNum generates 7 numbers from 1 - 40 and checks that there are no repeated numbers. then compares both arrays to see if any numbers match and checks if you are a winner.
Thank you for your time any help is appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#include <iostream>
#include <string>
#include<string>
#include<cctype>
#include <cstdlib>
#include <ctime>
using namespace std;
const int YOURNUM = 7;
const int WINGNUM = 7;
void getLottoPicks(int nums[],int size);
void GenWinNum(int nums [], int size);
int main()
{
char selection;
string name;
int UserTicket [YOURNUM];
int winningNums [WINGNUM];
int matching = 0;
/*srand ((unsigned int)time(NULL));*/
do
{
cout <<"\nLITTLETON CITY LOTTO MODEL:" <<endl
<<"---------------------------" <<endl
<<"1) Play Lotto" <<endl
<<"q) Quit Program" <<endl
<<"Please make a selection" <<endl;
cin >> selection;
cin.ignore();
while (selection != '1' && selection != 'q' && selection != 'Q' )
{
cout <<"\nInvalid Selection" <<endl
<<"LITTLETON CITY LOTTO MODEL:" <<endl
<<"---------------------------" <<endl
<<"1) Play Lotto" <<endl
<<"q) Quit Program" <<endl
<<"Please make a selection" <<endl;
cin >> selection;
cin.ignore();
}
if (selection == '1' )
{
cout <<"Please enter your name." <<endl;
getline(cin,name);
getLottoPicks(UserTicket,YOURNUM);
GenWinNum(UserTicket,YOURNUM);
for (int index = 0; index < YOURNUM; index++)
{
int temp = UserTicket[index];
for (int index = 0; index < WINGNUM; index++)
{
if (temp == winningNums[index])
{
matching++;
}
}
}
cout <<"\n" << name <<"'s Lotto Results" <<endl
<<"----------------------" <<endl
<<"Winning Ticket Numbers :" ;
for (int index = 0; index < WINGNUM; index++)
{
cout <<" " << winningNums[index];
}
cout <<"\n" <<name <<"'s Ticket :" ;
for (int index = 0; index < YOURNUM; index++)
{
cout <<" " << UserTicket[index];
}
cout << "RESULTS :" <<endl
<< "--------" <<endl
<< "Number Matches:" <<matching
<< "Winnings :" ;
if (matching <= 2)
{
cout <<"SORRY NOTHING" <<endl;
}
else if (matching == 3)
{
cout << "FREE TICKET" <<endl;
}
else if (matching == 4)
{
cout << "NOT BAD - $100" <<endl;
}
else if (matching == 5)
{
cout << "LUCKY YOU! - $5,000" <<endl;
}
else if (matching == 6)
{
cout << "GREAT! - $100,000" <<endl;
}
else
{
cout << "JACKPOT - 1 MILLION" <<endl;
}
}
}while (selection != 'q' && selection !='Q' );
cout << "You have chosen to quit the program. Thank you for using!" <<endl;
system("pause" );
return 0;
}
void getLottoPicks(int numbers[],int size)
{
int yourticket = numbers[size];
cout << "Please enter your 7 lotto number picks between 1 and 40: " << endl;
for (int i = 0; i < size; i++)
{
cout << "selection #" << i + 1 << endl;
cin >> numbers[i];
cin.ignore();
while (numbers[i] < 1 || numbers[i] > 40)
{
cout << "Please choose a number between 1 and 40: " << endl;
cin >> numbers[i];
}
for (int j = 0; j < i; j++)
{
while (numbers[i] == numbers[j])
{
cout << "You have already picked that number. Enter a different one: " << endl;
cin >> numbers[i];
cin.ignore();
}
}
}
}
void GenWinNum (int nums [], int size)
{
srand ((unsigned int )time(NULL));
int winning = rand () % 40 + 1;
for (int count = 0; count < size; count++)
{
nums[count] = winning;
for (int j = 0; j < count; j++)
{
while (nums[count] == nums[j])
{
nums[count] = winning;
}
}
}
}
Last edited on Nov 18, 2013 at 3:51am UTC
Topic archived. No new replies allowed.