Question

In: Computer Science

Write a C++ die roller program. We need you to write a program that will roll...

  • Write a C++ die roller program.
  • We need you to write a program that will roll dice for them, but not just six-sided dice.
  • Your program needs to be able to take an input in for form nDx or ndx where the d is the letter d or D.
  • n may or not be present. If it is, it represents the number of dice. If not, assume it is a 1.
  • x may or may not be present. If it is, it represents the number of sides on the die. If it is absent, assume it is a 6.
  • If n is absent, x must be present. If x is absent, n must be present.
  • Remember, you need to roll each die individually and then add up the values. Also show the individual dice if n > 1.
  • For example: 2d means roll 2 six-sided dice (2d6). d20 means roll one 20-sided die (1d20)
  • Assume for the time being that you will only receive valid input.

Solutions

Expert Solution

Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation.

Please provide your feedback
Thanks and Happy learning!

#include <iostream>
#include <string>
#include <sstream>
#include <time.h>

int main()
{
    std::string strInput;
    int diceCount = 1;
    int diceSideCount = 6;

    std::cout << "Enter the input in the format nDx or ndx : ";
    std::getline(std::cin, strInput);

    if (!strInput.empty())
    {
        size_t foundPos = 0;
        if ((foundPos = strInput.find_first_of('d')) == std::string::npos && (foundPos = strInput.find_first_of('D')) == std::string::npos)
        {
            std::cout << "ERROR: Invalid input format." << std::endl;
            return 1;
        }

        std::string strCount = strInput.substr(0, foundPos);
        std::string strSideCount = strInput.substr(foundPos + 1);

        //If n is absent, x must be present. If x is absent, n must be present.
        if (strCount.empty() && strSideCount.empty())
        {
            std::cout << "ERROR: Invalid input." << std::endl;
            return 1;
        }

        std::stringstream ss;
        ss.clear();
        if (!strCount.empty())
        {
            ss << strCount;
            ss >> diceCount;
        }

        ss.clear();
        ss.flush();
        if (!strSideCount.empty())
        {
            ss << strSideCount;
            ss >> diceSideCount;
        }

        srand(time(NULL));
        int totalDiceReading = 0;
        for (int i = 1; i <= diceCount; ++i)
        {
            //roll the dice;
            //Generate a random value between 1 and diceSideCount
            int diceReading = std::rand() % diceSideCount + 1;
            //show the individual dice if n > 1.
            if (diceCount > 1)
            {
                std::cout << "Dice " << i << " reading: " << diceReading << std::endl;
            }
            totalDiceReading += diceReading;
        }

        std::cout << "Total dice reading : " << totalDiceReading << std::endl;
    }
    else
    {
        std::cout << "ERROR: Invalid input." << std::endl;
    }

    return 0;
}

Related Solutions

PROJECT C Roll a 6-sided die 800 times. If you do not desire to roll a...
PROJECT C Roll a 6-sided die 800 times. If you do not desire to roll a die manually, STATDISK can be used to simulate the process. To use STATDISK, go to “Data” at the top of the STATDISK window, and then choose “Dice Generator”. The “Dice Roll Random Sample Generator” window will appear. Then in that window, put 800 in for “Sample Size”, put 1 in for “Num Dice”, and put 6 in for “Num Sides”. Please disregard the “Random...
You roll a balanced die two times. Is the second roll independent of the first roll?
You roll a balanced die two times. Is the second roll independent of the first roll?
Suppose that you roll a die and your score is the number shown on the die....
Suppose that you roll a die and your score is the number shown on the die. On the other hand, suppose that your friend rolls five dice and his score is the number of 6’s shown out of five rollings. Compute the probability (a) that the two scores are equal. (b) that your friend’s score is strictly smaller than yours.
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
in c++ you need to write a program that maintains the gradebook for a specific course....
in c++ you need to write a program that maintains the gradebook for a specific course. For each student registered in this course, the program keeps track of the student’s name, identification number (id), four exam grades, and final grade. The program performs several functionalities such as: enrolling a student in the course, dropping a student from the course, uploading student’s exam grades, displaying the grades of a specific student, displaying the grades of all students in the course, and...
Coding in C++: For this verse, you need to write a program that asks the user...
Coding in C++: For this verse, you need to write a program that asks the user to input the inventory id number (integer) and the price (float) for 5 inventory items. Once the 5 inventory items are input from the user, output the results to the screen. Please ensure you use meaningful names for your variables. If your variables are not named meaningfully, points will be deducted.
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
17#13 Suppose we roll a fair six-sided die and sum the values obtained on each roll,...
17#13 Suppose we roll a fair six-sided die and sum the values obtained on each roll, stopping once our sum exceeds 289. Approximate the probability that at least 76 rolls are needed to get this sum.
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT