lottery checker program!
Dec 7, 2016 at 9:00am UTC
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 164 165 166 167 168 169 170 171 172
#include<iostream>
#include<string>
#include<cstdlib>
#include<time.h>
using namespace std;
//function prototypes
void genwinNumbers(int []);
void displayNumbers(int []);
void displayMatches(int [], int []);
void getlottoPicks(int []);
int main()
{
int choice[7] = { 0 };
int matcingNum[7] = { 0 };
int num[7] = { 0 };
string name;
char ans;
int userTicket = 0;
do
{
cout << "\nLITTLETOWN CITY LOTTO MODEL: " << endl;
cout << "------------------------------------" << endl;
cout << "1) Play Lotto " << endl;
cout << "q)Quit Program" << endl;
cout << "Please make a selection:" << endl;
cin >> ans;
cin.ignore();
if (ans == '1' )
{
cout << "Please enter your name." << endl;
cin >> name;
//get choices
int *userTicket;
getlottoPicks(userTicket);
genwinNumbers(num);
//call function to display winningNumbers
cout << name << "'s LOTTO RESULTS" << endl;
cout << "-----------------------------" << endl;
cout << "WINNING TICKET NUMBERS: " ;
displayNumbers(num);
//call function to display user's choices
cout << "\t" << name << "'s TICKET: " ;
displayNumbers(choice);
//call fuction to display number of correct choices
cout << "RESULTS: \n ------------" << endl;
displayMatches(choice, num);
}
else if (ans == 'q' || ans == 'Q' )
{
cout << "YOu have chosen to quit the program. Thank you for using!" << endl;
}
} while ((ans != '1' ) && (ans != 'q' ) && (ans != 'Q' ));
system("pause" );
return 0;
//end of main function
}
//function definition
void genwinNumbers(int winningNums[])
{
bool check[41] = { false };
bool again = true ;
int genwinNumbers = 0;
srand(static_cast <int >(time(0)));
for (int x = 0; x < 7; ++x)
{
do
{
genwinNumbers = 1 + rand() % (40 - 1 + 1);
if (check[genwinNumbers] == false )
{
winningNums[x] = genwinNumbers;
check[genwinNumbers] = true ;
again = false ;
}
} while (again == true );
again = true ;
}
}
void displayNumbers(int num[])
{
for (int x = 0; x < 7; ++x)
{
cout << num[x] << endl;
}
}
void getlottoPicks(int userTicket[])
{
bool check[41] = { false };
int pick = 0;
for (int x = 0; x < 7; ++x)
{
cout << "Please enter number " << x + 1<< ": " ;
cin >> pick;
if (check[pick] == false )
{
userTicket[x] = pick;
check[pick] = true ;
}
else
{
cout << "The number must be between 1 and 40 and no duplicates are excepted."
<< "Please enter another number. " << endl;
--x;
}
}
}
void displayMatches(int pick[], int winningNUmbers[])
{
int match = 0;
cout << "Number matches: " << endl;
for (int x = 0; x < 7; ++x)
{
for (int y = 0; x < 7; ++y)
{
if (pick[x] == winningNUmbers[y])
{
++match;
}
}
}
cout << "Winnings:" ;
if (match == 7)
{
cout << "You win a million dollar prize!" << endl;
}
else if (match == 6)
{
cout << " You win $100,000!" << endl;
}
else if (match == 5)
{
cout << "You win $5,000!" << endl;
}
else if (match == 4)
{
cout << "You win $100!" << endl;
}
else if (match == 3)
{
cout << "You win a free ticket! " << endl;
}
else if (match <= 2)
{
cout << "SORRY NOTHING" << endl;
}
}
can somebody help me identify what's wrong with my program? whenever i run it, it keeps crashing after it prompt's the user's name;(
Last edited on Dec 7, 2016 at 9:01am UTC
Dec 7, 2016 at 4:04pm UTC
Line 38: userTicket
is an uninitialized pointer. When you pass it to getlottoPicks(...) the program will crash because the function expects an array with 7 fields.
Topic archived. No new replies allowed.