Simple Dice Program

This program was designed as a simple tool, for my brother, to replace the need of physical dice for D&D. As, I would like to think, an intermediate level programmer with what seems to be a beginner level problem, I would appreciate any help here...

Essentially, my code (with added comments and empty lines for all of you to better understand what should be happening) is as follows:

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <cstdlib>
#include <ctime>

// Function Declarations
void DisplayMenu();
void Roller(int);
void Percentile();

int main()
{
  srand(time(0)); // Initialize PRNG Seed
  DisplayMenu(); // Enter "Main" Program Body
  return 0;
}

void DisplayMenu()
{
  int iChoice=0; // Integer Initialized To 0, Used To Retrieve Menu Option

  // Begin Menu
  std::cout << "1. 4-Sided Dice\n";
  std::cout << "2. 6-Sided Dice\n";
  std::cout << "3. 8-Sided Dice\n";
  std::cout << "4. 10-Sided Dice\n";
  std::cout << "5. 12-Sided Dice\n";
  std::cout << "6. 20-Sided Dice\n";
  std::cout << "7. % Dice\n";
  std::cout << "8. Exit Dice Roller\n\n";
  // End Menu

  std::cout << "Choice: ";
  std::cin >> iChoice; // Input Menu Choice
  std::cout << "\n\n";

  if(iChoice<1) // Internal Check, Used To Assure Valid Input
  {
    std::cout << "\n\nError: Invalid Choice\n\n"; // Tell User Input Was Not A Valid Option
    std::cout << "Please choose a number between 1 and 8, corresponding to the menu option you would like to select.\n\n"; // Give User Acceptable Values
    std::cout << "\n\n"; // Add Empty Lines For Aesthetic Appeal
    DisplayMenu(); // Return To Top Of Menu Function
    return;
  }
  else if(iChoice>8) // Internal Check, Used To Assure Valid Input
  {
    std::cout << "\n\nError: Invalid Choice\n\n"; // Tell User Input Was Not A Valid Option
    std::cout << "Please choose a number between 1 and 8, corresponding to the menu option you would like to select.\n\n"; // Give User Acceptable Values
    std::cout << "\n\n"; // Add Empty Lines For Aesthetic Appeal
    DisplayMenu(); // Return To Top Of Menu Function
    return;
  }

  /* This Section Commented Out

  Wondering If:

  if(iChoice<1||iChoice>8)

  Would Be An Acceptable Substitute For The Two Previous Checks, Or If Only One Condition Should Be Checked Per "if" Statement

  */

  else if(iChoice==7) // Percentile Dice Use Different Function. The Results Of Two Dice Must Be Added Differently
  {
    Percentile(); // Call Percentile Dice Function
    return;
  }
  else if(iChoice==8) // Directs Program To Return With No Further Action, Or "Exit"
  {
    return;
  }
  else // Continue To Rolling Function
  {
    Roller(iChoice); // Call Roller Function
    return;
  }
  return;
}

void Roller(int iChoice)
{
  int iDice=0; // Integer Initialized To 0, Used To Determine Number Of Dice To Roll
  int iSides=0; // Integer Initialized To 0, Used To Determine Number Of Sides On Selected Die

  // Begin Setting Number Of Sides On Die
  if(iChoice==1)
  {
    iSides=4;
  }
  else if(iChoice==2)
  {
    iSides=6;
  }
  else if(iChoice==3)
  {
    iSides=8;
  }
  else if(iChoice==4)
  {
    iSides=10;
  }
  else if(iChoice==5)
  {
    iSides=12;
  }
  else if(iChoice==6)
  {
    iSides=20;
  }
  // End Of Setting Number Of Sides On Die

  // Begin Number Of Dice Being Rolled
  std::cout << "\n\nHow many dice would you like to roll?\n\n";
  std::cout << "Roll: ";
  std::cin >> iDice;
  std::cout << "\n\n";
  // End Number Of Dice Being Rolled

  do
  {
    std::cout << rand()%iSides+1; // Display Die Roll With iSides Deciding Which Die Is Rolled
    std::cout <<"\n"; // Add Empty Line For Aesthetic Appeal
    iDice=iDice-1; // Decrease Number Of Dice To Be Rolled
  }
  while(iDice>0); // Stop Rolling When Number Of Die Rolls Reaches 0

  std::cout << "\n\n"; // Add Empty Lines For Aesthetic Appeal
  DisplayMenu(); // Return To Menu Function
  return;
}

void Percentile()
{
  int iDice=0; // Integer Initialized To 0, Used To Determine Number Of Dice To Roll
  int iTens=0; // Integer Initialized To 0, Used To Hold First Die Roll - Tens Digit
  int iOnes=0; // Integer Initialized To 0, Used To Hold Second Die Roll - Ones Digit
  int iResult=0; // Integer Initialized To 0, Used To Hold Total Of Both Dice

  // Begin Number Of Dice Being Rolled
  std::cout << "\n\nHow many dice would you like to roll?\n\n";
  std::cout << "Roll: ";
  std::cin >> iDice;
  std::cout << "\n\n";
  // End Number Of Dice Being Rolled

  do
  {
    iTens=rand()%11; // iTens Is Rolled To A Number Between 0 And 10
    iOnes=rand()%10; // iOnes Is Rolled To A Number Between 0 And 9
    if(iTens==10) // Internal Check, Used To Decide If Percent Is At 100, Or Max Value
    {
      iResult=100; // Set Result To Max Percent Value
    }
    else
    {
      iResult=iTens+iOnes; // Add Tens Digit And Ones Digit To Get Total Percent Value
    }
    std::cout << iResult; // Display Result
    std::cout << "%\n"; // Add % Symbol To Displayed Value And Add Empty Line For Aesthetic Appeal
    iDice=iDice-1; // Decrease Number Of Dice To Be Rolled
  }
  while(iDice>0); // Stop Rolling When Number Of Dice To Be Rolled Reaches 0

  std::cout << "\n\n"; // Add Empty Lines For Aesthetic Appeal
  DisplayMenu(); // Return To Menu Function
  return;
}


The entire program up to this point works correctly, with exception to the percentile dice function. No matter how many times I roll the percentile dice the result is a value between 1 and 19, or 100. The tens digit seems to be refusing to take any value between 2 and 9...any suggestions, or anything that I may have overlooked in my code? Also, suggestions on how I could improve, or otherwise change, the code to be more productive/efficient in less lines?

EDIT: I have tested the rand()%11 section by itself and have successfully gotten varying results of every number from 0 to 10. However, somewhere in the code above, I assume, any values in the iTens variable seems to be lost if the result is 2 to 9...
Last edited on
Regarding the comment occupying Lines 53 - 59; neither. Drop the if else ad nauseum stuff and put it in a switch case with a default case that handles anything that you don't have a case for.

As for your underlying problem... just don't kick yourself too hard. Any number modulated by 11 will give you a value between 0 and 10. If this is supposed to be the tens place in the number then you should multiply the result by ten before adding it to the ones place but after modulating the result.
EDIT: I noticed you were referring to the if/else mess in the menu function. I changed it. Thank you.

Well...

*takes long, deep breath*

... just don't kick yourself too hard.


Too late for that. ><

Thank you for pointing that out. Simple mistake that I seemed to overlook about a million times...I have spent hours staring at that code, and have, somehow, never realized that I was not making the tens digit an actual tens digit and just adding it as a second ones digit...so, thank you again.

EDIT: Line 156 has been changed to: iResult=(iTens*10)+iOnes;
Last edited on
I have spent hours staring at that code, ...


This condition is known as fatigue. People are under the misapprehension that if you aren't sweating, sore and coughing your lungs out that you aren't weary. Do yourself a favor and lose that idea.
I was thinking more along the lines of "it's often the smaller, simpler aspects that are the easiest overlooked." Though, you make a valid point, and good advice.

Perhaps the next time I encounter a problem I'll take a break from the code. Then, I'll return to look again before deciding I can not figure it out, instead of just driving myself crazy staring at it. Hahaha

Again, thank you very much, Computergeek01. You have been very helpful.
I suppose that I can flag this topic as solved now.
Topic archived. No new replies allowed.