You are to create to create a program in C++ to simulate a game using the concep
ID: 3678079 • Letter: Y
Question
You are to create to create a program in C++ to simulate a game using the concepts of the lottery problem. Initially the user will enter the amount of money with which he/she can play (bet). Each time the user chooses to play the lottery it will cost $100. Each time the user plays, he/she will be prompted to enter from zero to 99 (including zero and 99) If the user’s number matches randomly generated number exactly, the user wins $5000.00. If the user’s guess contains the same digits as the randomly generated number, but they are in reverse order (e.g. 67 and 76), the user will win $2000.00. If one digit of the user’s guess is in the randomly generated number, the user should win $500.00. If the randomly generated number or the user’s guess is less than 10 treat the number as if it has 0 as the first digit (9 should be treated as 09). They user can continue playing as long as he/she still has enough money to play again ($100) and wants to continue playing. Your program should employ a while loop to confirm that the user’s initial amount of money is at least $100 and that each guess is greater than or equal to zero and less than 100. Then your program should employ a do-while loop to allow the user to keep playing as long as he/she wants and has at least $100. The guessed number and the randomly generated number should be stored as an integer. The first and second digit of both the user’s guess and the randomly generated number should be determined using a minimum number of arithmetic operators. (At no time should the user’s guess or the randomly generated be stored as an array or string). Your program should use an if-else if to determine how much the use wins each time he/she plays. During each play the randomly generated number and the user’s guess should be output. Hint to test your program use a specific number rather than a randomly generated number to compare the user’s guess. Then generate the number randomly after you have confirmed the determined your if-else if works correctly.
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
const int lotteryDigits = 10;
const int SIZE = 5;
int lottery(int[], int, int);
int Bet(int[], int);
int Match_count(int[], int[], int);
void dispaly(int[], int[]);
void win_lose(int);
using namespace std;
int main()
{
int lottery[5] = {0, 0, 0, 0, 0};
int user[5] = {0, 0, 0, 0, 0};
int matches = 0;
lottery(lottery, SIZE, lotteryDigits);
Bet(user, SIZE);
Match_count(lottery, user, matches);
dispaly(lottery, user);
win_lose(matches);
system("pause");
return 0;
}
int lottery(int lottery[], int, int)
{
unsigned seed = time(0);
srand(seed);
for (int y=0; y<SIZE; y++)
{
lottery[y] = rand() % lotteryDigits;
}
return lottery[0], lottery[1], lottery[2], lottery[3], lottery[4];
}
int Bet(int user[], int)
{
cout << "This program will simulate a lottery. ";
for (int y=0; y<SIZE; y++)
{
cout << "Please enter between 0 and 9 ";
cin >> user[y];
while (user[y]<0 || user[y]>9)
{
cout << "Error!!";
cin >> user[y];
}
}
return user[0], user[1], user[2], user[3], user[4];
}
int Match_count(int lotto[], int input[], int)
{
int match = 0;
for (int x = 0; x < SIZE; ++x)
{
if (lotto[x] == input[x])
match = match + 1;
}
return match;
}
void dispaly(int lottery[], int user[])
{
cout << " The winning lottery numbers are: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl;
cout << "Your lottery numbers are: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl;
}
void win_lose(int matches)
{
cout << "You matched " << matches << " numbers";
if (matches != SIZE)
cout << " you lose. Better luck next time. ";
else
cout << "you WIN!!!! ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.