Question: Write a complete C + + program. In this program you have to write 2 functions in addition to the main function. 1 . Function

Write a complete C++ program. In this program you have to write 2 functions in addition to the main function.
1. Function #1:
Write function throwDice() that generates a random number between 1 and 6. The function should return the generated number.
int throwDice();
2. Function #2:
Write function play() to perform the following:
1. Declare the required local variables: score, dice1, dice2
2. Initialize variable score to 0.
3. Loop 5 times to do the following:
o Call the function throwDice and store the returned value in variable dice1.
o Call the function throwDice again and store the returned value in vriable dice2.
o Update score by adding to its current value the value of dice1 and the value of dice2.
4. After the loop, check the value of score, if it is above 50 then return true; otherwise retun false.
bool play();
Write a complete program to do the following in the main:
1. Call the function play().
2. Display Win if the returned value of calling the function play() is true, otherwise, display lose.
3. Repeat the above two steps until the user decides to stop playing and exit the program. why is this program not working properly: #include
#include // For random number generation
using namespace std;
// Function to throw a dice and return the result
int throwDice(){
static random_device rd;
static mt19937 gen(rd());
static uniform_int_distribution distribution(1,6);
return distribution(gen);
}
// Function to play the game
bool play(){
int score =0;
int dice1, dice2;
// Loop 5 times to throw the dice and update score
for (int i =0; i <5; ++i){
dice1= throwDice();
dice2= throwDice();
score += dice1+ dice2;
}
// Check if score is above 50
if (score >50)
return true;
else
return false;
}
int main(){
char choice;
do {
cout << "Playing the game..." << endl;
if (play())
cout << "Congratulations! You won." << endl;
else
cout << "Sorry, you lost." << endl;
cout <<"Do you want to play again? (Y/N): ";
cin >> choice;
} while (choice =='Y'|| choice =='y');
cout << "Thanks for playing. Goodbye!" << endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!