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
|
#include <cstdlib>
#include <ctime>
#include <iostream>
constexpr int ARRAY_SIZE = 10;
void fillArrayWithRN(int array_wins[], size_t size);
bool checkArrayAgainstUserInput(int array_wins[], size_t size, int guess);
int askForInt();
int askForGuesses(int* lottery, int* tickets, size_t size);
void printout(int array_wins[], int tries[], size_t size);
int main()
{
std::srand(std::time(nullptr));
std::cout << "This program picks 10 random numbers from 1 to 50.\n"
"You have a chance to win if you pick the matching number.\n"
"Good luck!\n\n";
int array_wins[ARRAY_SIZE] = {0}; // all fields initialized to 0
fillArrayWithRN(array_wins, ARRAY_SIZE);
// Record 10 user's numbers and check them against array_wins
int usersnum[ARRAY_SIZE] {};
int matches = askForGuesses(array_wins, usersnum, ARRAY_SIZE);
std::cout << "You guessed " << matches << " numbers.\n";
printout(array_wins, usersnum, ARRAY_SIZE);
std::cout << '\n';
return 0;
}
// draws 10 random numbers in array and outputs results
void fillArrayWithRN(int array_wins[], size_t size)
{
for(size_t i{}; i< size; ++i) {
array_wins[i] = rand() % 50 + 1;
}
}
int askForGuesses(int* lottery, int* tickets, size_t size)
{
int matches {};
for(size_t i{}; i<size; ++i) {
// Step 1: ask user for input:
tickets[i] = askForInt();
if(checkArrayAgainstUserInput(lottery, size, tickets[i])) { matches++; }
}
return matches;
}
// checks generated numbers with user number
bool checkArrayAgainstUserInput(int array_wins[], size_t size, int guess)
{
for (size_t i = 0; i < size; ++i) {
if (guess == array_wins[i]) { return true; }
}
return false;
}
// gets user's input from 1 to 50
int askForInt()
{
std::cout << "Please select a number from 1 to 50: ";
int ans {};
std::cin >> ans;
std::cin.ignore();
return ans;
}
// outputs selected lottery numbers
void printout(int array_wins[], int tries[], size_t size)
{
std::cout << "Lotto numbers are: ";
for (size_t i = 0; i < size; ++i) { std::cout << array_wins[i] << ' '; }
std::cout << "\nWhile tried numbers are: ";
for (size_t i = 0; i < size; ++i) { std::cout << tries[i] << ' '; }
std::cout << '\n';
}
|