I am not sure what is making the code skip a crucial step in the program.
Here's how it should work.
The program asks the user to pick 5 numbers from 0 to 9. Then after the array of numbers is collected a simulation is run to generate random lottery numbers in the same range (tests are run so no repeating numbers reappear). After showing the lottery numbers the program shows the numbers in common between both array until the lottery and user numbers match up, showing the number of tries it took.
I left the comments to help whoever is helping find the issue faster.
UPDATE: Further updated the code. Asks user for data but is now stuck after receiving the information (used to skip altogether and display garbage lottery and user numbers).
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
bool sameNumbers(int totnums, int lot1, int lot2, int lot3, int lot4, int lot5, int us1, int us2, int us3, int us4, int us5)
{
//this checks how many numbers in each array are similar
//returns the value that will break out of the loop and determine when all the numbers are correct
int samenums = 0;
int usr[]={us1, us2, us3, us4, us5};
int lot[]={lot1, lot2, lot3, lot4, lot5};
for(int x=0; x<totnums; x++) //checks each array in user input (usr)
{
for(int y=0; y<totnums; y++) //checks each array in lottery (lot)
{
if(usr[x]==lot[y])
samenums++;
}
}
cout << "There are " << samenums << " numbers in common between the user guess and the lottery numbers" << endl;
const int numbers = 5;
int lottery[numbers];
int user[numbers];
bool match = false;
int tries = 0;
cout << "To play the lottery you'll have to input five numbers from 0 to 9. None of them should repeat." << endl;
for(int x=0; x<numbers; x++) //inputs a number in each of the arrays
{
cout << "Input a number from 0 to 9." << endl;
cin >> user[x];
}
while(match == false)
{
for(int x=0; x<numbers; x++) //the number of the array
{
bool same = false; //sets value same as a boolean that always starts off false
while(same == false) // keeps creating a new value while the array is false
{
int num = rand()%10; //random number from 0 to 9
same=true; // if the value isn't found false in the while loop it will exit the for loop
lottery[x]=num; // assigns value to lottery array, will keep getting recreated if repeat number
for(int check=0; check<numbers; check++) //checks if the randomly generated number is the same in another array value
{
if(lottery[check]==num)
same=false;
}
}
}
cout << "Your numbers: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl;
cout << "The lottery numbers: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl;
tries++; //adds to the numbers of times the program ran or "tries" until the numbers were right
//returns false or true value, true means all the numbers match
match = sameNumbers(numbers, lottery[0], lottery[1], lottery[2], lottery[3], lottery[4], user[0], user[1], user[2], user[3], user[4]);
}
cout << "You finally won the lottery! It took you " << tries << " tries." << endl; //shows number of turns needed to get numbers correct
while (match == false)
{
for (int x = 0; x<numbers; x++) //the number of the array
{
bool same = false;
while (same == false)
{
int num = rand() % 10;
same = true;
lottery[x] = num; // <= set lottery[x] to num.
for (int check = 0; check<numbers; check++)
{
if (lottery[check] == num) // at some point check will be x and lottery[x] will be num.
same = false; // So this will always be set to false...
}
} // And you'll do it all over again.
}
Final version
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
bool sameNumbers(int totnums, int lot1, int lot2, int lot3, int lot4, int lot5, int us1, int us2, int us3, int us4, int us5)
{
//this checks how many numbers in each array are similar
//returns the value that will break out of the loop and determine when all the numbers are correct
int samenums = 0;
int usr[]={us1, us2, us3, us4, us5};
int lot[]={lot1, lot2, lot3, lot4, lot5};
for(int x=0; x<totnums; x++) //checks each array in user input (usr)
{
for(int y=0; y<totnums; y++) //checks each array in lottery (lot)
{
if(usr[x]==lot[y])
samenums++;
}
}
cout << "There are " << samenums << " numbers in common between the user guess and the lottery numbers" << endl;
const int numbers = 5;
int lottery[numbers];
int user[numbers];
bool match = false;
int tries = 0;
cout << "To play the lottery you'll have to input five numbers from 0 to 9. None of them should repeat." << endl;
for(int x=0; x<numbers; x++) //inputs a number in each of the arrays
{
cout << "Input a number from 0 to 9." << endl;
cin >> user[x];
}
while(match == false)
{
for(int x=0; x<numbers; x++) //the number of the array
{
bool same = false; //sets value same as a boolean that always starts off false
while(same == false) // keeps creating a new value while the array is false
{
int num = rand()%10; //random number from 0 to 9
same=true; // if the value isn't found false in the while loop it will exit the for loop
lottery[x]=num; // assigns value to lottery array, will keep getting recreated if repeat number
for(int check=0; check<x; check++) //checks if the randomly generated number is the same in another array value
{
if(lottery[check]==num)
same=false;
}
}
}
cout << "Your numbers: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl;
cout << "The lottery numbers: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl;
tries++; //adds to the numbers of times the program ran or "tries" until the numbers were right
//returns false or true value, true means all the numbers match
match = sameNumbers(numbers, lottery[0], lottery[1], lottery[2], lottery[3], lottery[4], user[0], user[1], user[2], user[3], user[4]);
}
cout << "You finally won the lottery! It took you " << tries << " tries." << endl; //shows number of turns needed to get numbers correct