Question
Write a C++ program for a lottery game. When the program starts, it should ask the user for his/her name. Assume that the name has
Write a C++ program for a lottery game.
When the program starts, it should ask the user for his/her name. Assume that the name has no spaces in it. The program should allow the user to play a game as many times as they want by asking whether they want to play again. If the user says 'y', the game should be played again. For any other response, the program should terminate. The player gets a pot of 100 coins at the start of the program. For each game, the user is asked for a bet amount (must be between 0 and the current size of the pot) and three numbers from 1 to 6. Then the program will generate three numbers at random from a range of 1 to 6. If the users number for a position matches the computers number for that position, the pot is increased by the bet amount. If none of the numbers match, the pot is decreased by the bet amount. If the pot goes to 0 at the end of a game, the program should print out a message "You have no money left!" and terminate the program.
Libraries Needed
You need to get two random numbers between 1 and 6 for each number. First, include two libraries needed for the rand() function. So at the top of your code put #include
int main()
{
// variable declarations go here
srand((unsigned) time(0));
// start of while loop goes here
........
The call to time(0) gets the current number seconds elapsed since Jan 1, 1970. The (unsigned) is needed because srand only takes an unsigned long and time returns a signed long. The use of time(0) is to assure that each time the program is run, the random number generator is seeded with a different number. This then generates a unique set of integers for each run.
Getting Random Numbers in a Range The cstdlib has the rand() function which passes back a random integer between 0 and the maximum possible integer. This way too big a range so you need to cut it down using the modulo operator % The modulo of any number with 6 will produce a number between 0 and 5. To get a range between 1 to 6 you will need to then add 1 to the result:
num1 = (rand() % 6) + 1;
num2 = (rand() % 6) + 1;
num3 = (rand() % 6) + 1;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started