I've been searching for hours on how to fix this but I'm stumped. It's the simply lottery program, where 5 digits are randomly generated and compared to the user's 5. It's supposed to count how many are the same, but every time it gives either 1 or 0, which is usually incorrect. I would greatly appreciate any help on fixing this issue.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
constint number = 5;
int winningDigits[number];
constint value = 5;
int player[value];
int numMatches = 0;
void generateNumbers()
{
//random number generator
srand(time(NULL));
//Generates the 5 winning numbers
for (int i = 0; i < number; i++)
{
winningDigits[0] = rand() % 10;
winningDigits[1] = rand() % 10;
winningDigits[2] = rand() % 10;
winningDigits[3] = rand() % 10;
winningDigits[4] = rand() % 10;
}
}
//user's numbers
void playerInput()
{
cout << "Enter your five numbers:\n";
cin >> player[0];
cin >> player[1];
cin >> player[2];
cin >> player[3];
cin >> player[4];
}
//outputs the two sets of numbers
void displayValues()
{
cout << "Winning Digits:\n";
for (int i = 0; i < number; i++)
{
cout << " " << winningDigits[i];
cout << endl;
}
cout << "Your numbers:\n";
for (int i = 0; i < value; i++)
{
cout << " " << player[i];
cout << endl;
}
}
int main()
{
int i = 0;
//compare and count
generateNumbers();
playerInput();
for (int i = 0; i < 5; i++)
{
if (winningDigits[i] == player[i])
numMatches++;
}
displayValues();
//Says if they won or not, if they didn't how many were the same
if (numMatches != 5)
{
cout << numMatches << " of your numbers matched.\n";
}
if (numMatches == 5)
{
cout << "Congratulations! You won the lottery!\n";
}
system("pause");
return 0;
}
Not particularly, but I'm just counting how many repeat and outputting that, not necessarily showing the user the exact digits. I'm not sure exactly what you were referring to but I hope I answered it.
Imagine the user enters 1 3 5 7 9
Imagine lottery numbers 5 3 2 4 1
You only compare 1 and 5, 3 and 3, 5 and 2 and so on.
Do you see the problem now?
Easiest solution would be to sort the two arrays first.
Another option would be to create a table and count the occurences of each number.
Each number that appears twice or more is a winner.
There are 2 things you could possibly use for your program.
You could use a linear search or a binary search.
I wrote a similar program for my c++ class a week or 2 ago.
For the linear search you can use (or your own version):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int linearSearch(int list[], int max, int win)
{
int i = 0;
int position = -1;
bool found = false;
while(i < max && !found)
{
if(list[i] == win) //value found
{
found = true;
position = i;
}
i++;
}
return position;
}
int binarySearch(int list[], int max, int win)
{
int first = 0;
int last = max - 1;
int middle;
int position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if(list[middle] == win) // middle
{
found = true;
position = middle;
}
elseif (list[middle] > win) // Lower half
{
last = middle - 1;
}
else
{
first = middle + 1;
}
}
return position;
}