ok, so i revised my source codes and got most of to work. what is trying to be done here is, i enter how many lotto tickets were bought, then i enter the winning lottory numbers, and then im supposed to enter my "own lottery ticket's number" and a message is supposed to tell me whether i am the winner or not. the dialog that asks me to enter my own "lottery ticket's number" is supposed to continously loop around with my numbers until i enter "0" which quits the program. The problem i have is...
if i enter 3 tickets bought....
it will then asks me to enter 3 winning numbers...
i then enter my own lottery ticket's numbers and it will only prompt me as a "winner" if i enter my own lottery ticket's number in the exact same order and spot i entered previously for the "winning numbers".
how do i get the program to print "winner" every time i enter a winning number as my own lottery ticket's number.
ok if it still sounds a little confusing, i'll give you the outputs i get on my screen.
How many lottery tickets were purchased?: 3
Enter winning lottery ticket's numbers...
Winning Number Lottery ticket#1: 4
Winning Number Lottery ticket#2: 5
Winning Number Lottery ticket#3: 6
Enter your lottery ticket number: 6
Loser!
Enter your lottery ticket number: 5
Winner!
Enter your lottery ticket number: 4
Loser!
_________________________________________
[and so forth......]
as you can tell, only the second number for my lottery ticket number showed up as a winner because "5" is pressed as the 2nd choice for each input. Please help me fix this so as long as my numbers match any of the winning numbers, it will print "Winner!" instead of having it need to be typed according to the same choice. kind of hard to explain but if you test out my codes, you will get the point. anyways, here is my revised codes....
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
|
//This program calculates whether or not a person's lottery numbers
//are the winning numbers.
#include <iostream>
using namespace std;
//function prototypes
int binarySearch(int [], int, int);
const int SIZE=10;
int main()
{
int *numbers;
int N;
int count;
int results;
//Get number of lottery tickets bought
cout<<"How many lottery tickets were purchased?:";
cin>> N;
//allocate an array
numbers= new int[N];
//Get lottery numbers for each ticket
cout<<"\nEnter winning lottery ticket's numbers...\n"<<endl;
for (count=0; count<N; count++)
{
cout<<"Winning Number Lottery ticket #"<<(count+1)<<": ";
cin>>numbers[count];
}
int binarySearch();
for (count=0; count<1000; count++)
{
cout<<"\nEnter your lottery number:";
cin>>results;
if (results == 0)
{
cout<<"Quitting Program\n";
break;}
{
if (results == numbers[count])
{
cout<<"Winner!\n";
}
else
cout<<"Loser!\n";
}
}
//Free allocated memory
delete [] numbers;
numbers = 0;
return 0;
}
//*************************************************
// Binary Search
int binarySearch(int array[], int size, int value)
{
int first= 0,
last=size-1,
middle,
position=-1;
bool found=false;
while (!found && first <=last)
{
middle=(first+last)/2;
if (array[middle]== value)
{
found=true;
position=middle;
}
else if (array[middle]>value)
last=middle-1;
else
first=middle+1;
}
return position;
}
|