I need help with an assignment

closed account (EAp4z8AR)
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()
{
    
}
Last edited on
closed account (EAp4z8AR)
I am now thinking that I might have to have another function that has the menu in it everything is done from it.
Hello vmansuria,


but I am having trouble returning back to the main function


The only two things I see right now is that the case statements are missing the "break;" statement. As is all the case statements will be executed.

The only thing I see that returns back to "main" is the "input" function. It is defined as returning an "int", but the function returns nothing. The compiler should have caught this error.

Hope that helps,

Andy
Hello vmansuria,

When I loaded up the program it helped me spot the other errors. You should compile the program before you post it that way if there any error messages that you do not understand you can include them in the post.

Your header files:
1
2
3
4
5
6
7
8
#include <iostream>  // <--- OK.
#include <math.h>  // <--- Should be "cmath".
#include <iomanip>  // <--- OK, but not used yet.
#include <vector>  // <--- Better than an array, but I do not see it used yet.
#include <cstdlib>  // <--- 50/50.
#include <ctime>  // <--- Will be used when you finally use "rand()".
#include <numeric>  // <--- Not sure what you had in mind here.
#include <fstream>  // <--- OK. 

To expand on "cstdlib" "iostream" will include other header files and along the way one of those files will include "stdlib.h". That is what makes including "cstdlib" 50/50 because some will tell you to include any header you think may be necessary.

In "main" case "C" you are calling a function sending it two variables neither of which are defined in "main". They are defined in the function "input", but that does not work, its backwards. "sides" and "weights" need to be defined in main and passed to the functions that need them.

You have defined "weights" as double weight[side-1];. This will not work because what is inside the []s needs to be a constant number. Either a variable defined as a constant or an actual number.

Done properly "main" could look something like this:
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
int roll(int sides, double *weights);
int input(int side, double* weights);  // <---Changed.
void output();
void quit();

int main()
{
	constexpr size_t SIDE{ 6 };

	int side{};
	double weight[SIDE]{};  // <---Changed.
	double *p = weight;  // <---Changed.
	//p = weight;
	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(side, p);
		}
		break;  // <--- Added.
		case 'B':

The "input" function will take some testing before I can comment in it.

FYI when you send an array to a function it degrades to a pointer at the function.

Hope that helps,

Andy
Topic archived. No new replies allowed.