Write a program named diceroll.cpp that:
*Implements a function named roll()that simulates a single roll of a weighted die, given its number of sides and weight values for each side. It should return a value between 1 and sides to indicate which side was rolled.
Follow the specifications below:
*Return type: int
*Name:roll
Parameters:
*int sides: number of sides in the die
*double *weights: a pointer to an array of weight values for each side in the die
(weights[sides - 1]: weight value for Side sides)
Presents the user with a menu comprised of four choices:
Enter the sides and weights for a die:
*Prompt the user to enter the number of sides of a die and validate the input (≥2)
*Prompt the user to enter the appropriate number of weights and validate the input (>0)
Specify an output file
*Prompt the user for the name of a file to write all output in text format
Simulate a specified number of rolls of the weighted die
*Prompt the user for the number of rolls to perform and validate the input (≥1)
*Present an error to the user if no output file has been specified (via menu choice 1)
*Present an error to the user if the number of sides and associated weights have not yet
been entered (via menu choice 2)
*All output should be displayed on the console AND written to the specified output file
Exit
*End the program
You must design your file-output code so that previous simulations in the same program session are not over- written (HINT: open the file in append mode).
I have this so far but I am having trouble returning back to the main function and also with the third case in the switch statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <fstream>
using namespace std;
int roll(int sides, double *weights);
int input();
void output();
void quit();
int main()
{
char choice;
cout << "To enter the sides and weights for a die enter A" << "\n";
cout << "To specify an output file enter B" << "\n";
cout << "To simulate a specified number of rolls of the weighted die enter C" << "\n";
cout << "To Exit enter D" << "\n";
cin >> choice;
switch (choice)
{
case 'A':
case 'a':
{
input();
}
case 'B':
case 'b':
{
output();
}
case 'C':
case 'c':
{
roll(side, weight);
}
case 'D':
case 'd':
{
quit();
}
}
}
int input()
{
int side;
cout << "Enter the sides for a die (2 or greater).\n";
cin >> side ;
while (side < 2)
{
cout << "Enter the sides for a die (2 or greater).\n";
cin >> side;
}
double weight[side-1];
double *p;
p = weight;
for (int i=0; i<=(side-1); i++)
{
cout << "Enter the weights for each side (greater than 0).\n";
cin >> *p;
if (*p > 0)
{
weight[i] = *p;
}
while (*p <= 0)
{
cout << "Invalid weight entered. Try again.\n";
cout << "Enter the weights for each side (greater than 0).\n";
cin >> *p;
}
}
roll(side, *p);
}
int roll(int sides, double *weights)
{
}
void output()
{
}
void quit()
{
}
|