Question

In: Computer Science

Using C++ and using a boolean variable with local scope to control the looping structure: How...

Using C++ and using a boolean variable with local scope to control the looping structure:

How do I create a program that will simulate a game of craps with the below qualification:

Containing two functions -an int rollDice() function that will simulate the rolling of two dice and the main function. When called the roll Dice() function will generate two random numbers between 1 and 6 inclusive, add them, and return the sum as the integer variable “point”. The function will also output the value of the “roll” of each digital die and the value of “point” to cout. Selection and looping structures in the int main() function will determine the impact of each roll (win, lose, or roll again) on the game, print the results, and continue the game if necessary.

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>

using namespace std;

int rollDice() {
    int a = rand() % 6 + 1;
    int b = rand() % 6 + 1;

    cout << "Dice 1: " << a << ", Dice 2: " << b << endl;
    return a + b;
}

int main() {
    srand(time(NULL));
    int sum = rollDice();
    if (sum == 7 || sum == 11) {
        cout << "you won!" << endl;
    }
    else if (sum == 2 || sum == 3 || sum == 12) {
        cout << "you lose!" << endl;
    }
    else {
        int point = sum;

        do {
            cout << "Dice sum: " << sum << endl;
            cout << "Rolling again." << endl;
            sum = rollDice();
        } while (sum != 7 && sum != point);

        if (sum == 7) {
            cout << "you lose!" << endl;
        }
        else if (sum == point) {
            cout << "you won!" << endl;
        }
    }
}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

C# Programming Explain in your own words how using the control selection ( decision) structure is...
C# Programming Explain in your own words how using the control selection ( decision) structure is useful in programming in general. Provide Example
Demonstrate the following: Usage of Eclipse Variable Assignment Looping (for, while etc) ArrayList (using object) Object...
Demonstrate the following: Usage of Eclipse Variable Assignment Looping (for, while etc) ArrayList (using object) Object based programming User Interaction with multiple options Implementing Methods in relevant classes Instructions Start with below java files: Item.java Pages //add package statement here public class Item { private int id; private double price; public Item(int id, double price){ this.id = id; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double...
Create your own data type using a C++ structure. Assign values to a variable of that...
Create your own data type using a C++ structure. Assign values to a variable of that data type. Retrieve values from the variable of that data type. Description: In this lab you will choose the statements to create a program that: Creates a brand new data type called "Monster". Once created, creates a variable of type Monster and assign values to the variable and then display the contents. This lab will again take the form of a guided multiple-choice quiz...
C++ - De Morgan’s laws commonly apply to text searching using Boolean operators AND, OR, and...
C++ - De Morgan’s laws commonly apply to text searching using Boolean operators AND, OR, and NOT. Consider a set of documents containing the words “cars” and “trucks”. De Morgan’s laws hold that these two searches will return the same set of documents: Search A: NOT (cars OR trucks) Search B: (NOT cars) AND (NOT trucks) The corpus of documents containing “cars” or “trucks” can be represented by four documents: Document 1: Contains only the word “cars”. Document 2: Contains...
Write a C program that has a local and a global variable. The program uses a...
Write a C program that has a local and a global variable. The program uses a fork to create a child process. The parent process modifies both variables to be 10 and 20 and prints out the values. Then the child process modifies both variables to be 100 and 200 and prints out the values? Explain the program output?
Please write with structure, not class! Add comments for clear understanding. Write code without using scope...
Please write with structure, not class! Add comments for clear understanding. Write code without using scope resolution operators. Write an ADT to represent a doctor's medical registration number (a non-negative integer), number of patients (a non-negative integer), and income (non-negative, in euros). A doctor's information should be represented by a structure with 3 fields: medical_reg and num_patients of type int, and income of type float. Your ADT should define four interface functions to carry out the following operations: · Construct...
If you would define a rule for creating a variable in C using regular expression, how...
If you would define a rule for creating a variable in C using regular expression, how would you write it? If you need to skip statements in for or while loops and go to the next iteration, what statement will you use? In respect to users and processes, how would you define Linux OS?
3. Simplify the following expressions using the properties of boolean algebra : 3A)    S(A,B,C) = A'B'C...
3. Simplify the following expressions using the properties of boolean algebra : 3A)    S(A,B,C) = A'B'C + A'BC + AB'C + ABC 3B) F(A,B,C) = A'B'C' + A'B'C + AB'C' + AB'C + ABC' + ABC
How to transfer the below two global varibale into local variable where this two variable are...
How to transfer the below two global varibale into local variable where this two variable are used. PLEASE HELP // Read in the user's height int customerAge = 0; double customerHeight = 0; // ******************************************* // Functions // ******************************************* void readHeight(); void printCustomerInfo(); bool askToDoAnother(); int main() { bool keepReading = 1; while (keepReading) { // Read in the customer info: // ... Read the height readHeight(); // ... Read Age cout << "Enter their age: "; cin >> customerAge;...
14) Fill in the three blanks in the following such that two processes executing the code cannot enter the critical section at the same time. key is a local Boolean variable.
Operating System Questions14) Fill in the three blanks in the following such that two processes executing the code cannot enter the critical section at the same time. key is a local Boolean variable. The swap function swaps the contents of the two arguments.do {key = ______________;while (key == FALSE) Swap (&lock, &key);/* critical section */lock = _____________;/* remainder section */ } while (TRUE);lock is a shared Boolean variable initialized to ___________.15) signal(mutex) without a corresponding wait(mutex) on a binary semaphore...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT