Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include alongside the any other libraries (such as iostream). The cstdlib contains a function called 'rand' for getting a random integer between 0 and the maximum allowed integer. The ctime is for 'seeding' the random number generator. Seeding the Library Before we can use the rand() function we need to 'seed' the random library. The seed is used to get a unique series of numbers. If you don't 'seed' the library, it will always give you the same set of numbers each time you run the program. So the first line inside main (after variable declarations) should be a call to srand() to seed the library. Dont call srand more than once during the running of the program as it will keep resetting the seed value and the program might end up generating the same numbers most of the time.

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions