using microsoft notepad, create a file call win_numbers.txt that contains the fo
ID: 3694029 • Letter: U
Question
using microsoft notepad, create a file call win_numbers.txt that contains the following numbers: 13579 26791 26792 33445 55555 62483 77777 79422 85647 93121 A lottery ticket buyer purchase 10 tickets a week, always playing the same 10 5-digit "lucky" combinations. write a program that will read the file above and initializes an array with these numbers and then lets the player enter this week's winning 5-digit number. The program should perform a linear search through the list of the player's number and report whether or not one of the tickets is a winner this week. I just need coding part thanks.
Explanation / Answer
//User Libraries
#include <iostream>
#include <cstdlib>
using namespace std;
//User Libraries
//Global Constant
//Function Prototype
//Execution starts here!
const int LUCKY_NUMS=10; //"lucky" combinations as given by book
// Function prototype that searches winning ticket number, linear search method
int ticketSearch(const int [], int, int);
int main(int argc, char** argv)
{
//User continues playing the lottery
char again;
const char QUIT='N';
//Determines if player's ticket is a winner
int winNum;
//5 digit "lucky" ticket number entered by user
int luckyNum;
//Holds winning ticket number
int ticket;
//Array holding the winning tickets for each week
int lotoNum[LUCKY_NUMS]={13579, 26791, 26792, 33445, 55555,
62483, 77777, 79422, 85647, 93121};
//Player decides if they want to continue playing
for (int week=0; week<10; week++)
{
//Winning lotto ticket for each week (10 weeks total)
ticket=lotoNum[week];
cout<<"Enter your 'lucky' combination; "<<(week + 1)<<":"<<endl;
//Player's ticket number
cin>>luckyNum;
//Calls linear search for winning lotto ticket
winNum=ticketSearch(lotoNum, LUCKY_NUMS, luckyNum);
//Error message if user's "lucky" number is not the winning ticket
if ((winNum==-1)||luckyNum!=ticket)
{
cout<<"Sorry you did not win! ";
cout<<"Try Again?? (Y/N)";
cin>>again;
}
//Player wins the lottery
else if (luckyNum==ticket)
{
cout<<"CONGRATULATIONS! YOU WIN THE LOTTERY!";
cout<<"Play again? (Y/N)";
cin>>again;
}
if ((again!='Y')&&(again!='y'))
{
//exit message
cout<<"Have a nice day! =)";
//exits the lottery program
exit(0);
}
}
//Exit stage right
return 0;
}
//linear search method, to search for the winning lottery numbers
int ticketSearch(const int ticketList[], int numTickets, int winNum)
{
int index=0;
int position=-1;
bool found=false;
while ((index<numTickets)&&!found)
{
if (ticketList[index]==winNum)
{
found=true;
position=index;
}
index++;
}
return position;
}
sample output
Enter your 'lucky' combination; 1:
93121
Sorry you did not win! Try Again?? (Y/N)n
Have a nice day! =)sh-4.3$
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.