This is my code which determines it a lottery number is a winner or a loser. I have specific numbers that it needs to go by. My code compiles and it congratulates the user if when I type any of the numbers, however, if I type in 12345 or several other numbers it also congratulates the user. If anyone can help me with this I would greatly appreciate it.
//This program uses a binary search and allows a lottery ticket buyer who purchases 10 tickets a week
//to enter 5 numbers to determine if he or she's number's is a winner or loser.
#include<iostream>
using namespace std;
int main()
{
int winningNumbers[]={13579,26791,26792,33445,55555,62483,77777, 79422,85647, 93121}; //"Lucky" lottery numbers
int numbers; //To hold lottery numbers
int last=10, //To hold the last array element
first=0, //To hold the first array element
middle; //To hold the midpoint of the search
do
{
cout<<"Enter 5 numbers and I will tell you if you won: "; //Allows user to enter 5 numbers
cin>> numbers;
if(numbers<00000||numbers>100000)
//Input Validation, lets user know the did not put in enough or too many numbers
cout<<"Input Validation: You need to put in 5 numbers.\n";
}
while (numbers<00000||numbers>100000); //Used to determine if correct numbers were used
last--;
while (first<=last)
{
middle=(first+last)/2;
if(winningNumbers[middle]<numbers)
first = middle + 1;
else
{
if( winningNumbers[middle]>numbers)
last = middle - 1;
else
{
last=-1;
}
}
}
if(last<0)
cout<<"Congratulations, those are the winning numbers!\n"; //Lets the user know the numbers are winners
else
cout<<"Sorry, those are not the winning numbers.\n"; //Lets user know the numbers are not winners
return 0;
}