### Dice (with varying number of sides) program: to calculate percentage of results

I am trying to code and compile a program that requests a uses to first inpu the number of sides they wish for a dice to have and secondly input the number of dice throws they would like to calculate the value for. The program will print an error message and default the number of sides the dice has to 5 if the number entered is below 4. Finally it will print out a percentage figure for each value that results from all the the dice rolls.

I have attached my header, driver and class files below along with accompanying error messages below them

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````// file name Exercise.cpp #include #include "Die.h" using namespace std; int main() // the main program begins here { char restart = 'y'; // declare variables double numRolls = 0.0; int diceTot = 0; int numSides; while (restart == 'y'){ // reload program every time user types 'y' at the end of the programs execution cout << "How many sides does the dice have? "; //prompt user to input the number of required dice rolls cin >> numSides; Die *dice1 = new Die(numSides); double score[numSides]; cout << "How many times will the dices be rolled? "; //prompt user to input the number of required dice rolls cin >> numRolls; for (int i=0; idiceRoll(); diceTot = dice1->getValue(); score[diceTot-1] = score[diceTot-1]+1; // increment values in score array each time its corresponding - } // value is matched by that of each dice throw for (int k=0; k> restart; } }``````

line 18 expected type-specifier before 'Die'
line 18 expected ';' before 'Die'
line 18 'dice1' was not declared in this scope
line 18 'Die' was not declared in this scope
line 30 method 'diceRoll' could not be resolved
line 32 method 'getValue could not be resolved

 ``123456789101112131415161718192021222324252627282930313233`` ``````// filename: Die.cpp #include #include #include #include "Die.h" using namespace std; int sides; int value; // constructor for a die object Die::Die(int size) { if (size <4){ sides = 6; cout << "This program will nor generate a dice with less than four sides. " << "Your dice will be be set to a default size of 6 sides. " << endl; } else{ sides = size; } } // generate value for a dice roll void Die::diceRoll() { value = rand()%sides+1; //generate random numbers from 1 to 6 } // return number of sides the dice has int Die::getNumSides(){ return sides; } // return the value of each dice roll int Die::getValue(){ return value; }``````

 ``123456789101112131415161718`` ``````//filename: Die.h #ifndef _DIE_H #define _DIE_H class Die { public: Die(int numSides); //constructor int getNumSides(); // Returns number of sides of dice int getValue(); // Returns dice roll value private: int value; // value of each dice roll int sides; // number of sides on die void diceRoll(); // Generate a random value }; #endif ``````

I cant seem to figure what to next. Any suggestions would be very welcome please :)
Last edited on
You forgot to #include something in Exercise.cpp. :P

Just so you know, having a `using namespace` statement in a header file is considered bad form because if your code were to be used as a library by someone else, you're taking away their choice of not using namespace std. Similarly, you don't actually need <stdlib.h> (which should be <cstdlib>) or <iostream> in the header, do you?

-Albatross
Last edited on
Hi Albatross,
Thank you for your corrections and speedy reply. I wasn't really aware about that as I am very new to C++ having only had a few lectures in it so far.
I added the ' include "Die.h" to the main file and remover the other code you mentioned and I still have the remaining errors:

make: *** [Exercise_1_1.o] Error 1 L4_Exercise_1_1 C/C++ Problem

Method 'diceRoll' could not be resolved Exercise_1_1.cpp /L4_Exercise_1_1 line 31 Semantic Error

Method 'getValue' could not be resolved Exercise_1_1.cpp /L4_Exercise_1_1 line 32 Semantic Error

request for member ‘diceRoll’ in ‘dice1’, which is of non-class type ‘Die*’
Exercise_1_1.cpp /L4_Exercise_1_1 line 31 C/C++ Problem

request for member ‘getValue’ in ‘dice1’, which is of non-class type ‘Die*’
Exercise_1_1.cpp /L4_Exercise_1_1 line 32 C/C++ Problem

 ``12`` ``````//Die *dice1 = new Die(numSides); Die dice1(numSides);``````
Mmm, somehow I overlooked that.

dice1 is a pointer to an object. The dot operator only works on objects, not pointers. You'll need to dereference the pointer, then try to call your methods.

Fortunately, there is a nice little operator `->` that does both for you, like so...
`ptr->member`
...and it'll work just like the way you were trying to use the dot operator.

Finally, remember to `delete` what you `new`.

ne555's solution is cleaner.

-Albatross
Last edited on
Thank you ne555 and Albatross! I made and tried the changes you both suggested and it works great either way :)

Thank you for again for being such a great help :)
Last edited on
Topic archived. No new replies allowed.