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
// file name Exercise.cpp
#include <iostream>
#include "Die.h"
usingnamespace 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; i<numSides; i++)
{
score[i] = 0; //reset score to 0 at the first and every subsequent restart of the program
}
for (int j=0 ; j<numRolls; j++) // loop through each throw of the dice
{
dice1->diceRoll();
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<numSides; k++)
{
double per = (100/numRolls)*score[k]; //calculate percentage of each value
int perRndUp = per*100; // round down to 0 deicmal places - multiply by 100 and change to int
double perRnd = perRndUp; // change back to double
double perCent = perRnd/100; // round down to 2 deciaml places
cout << k+1.0 << "\t" << perCent << "%" << endl; //print out percentage of occurences for each value
}
cout << "Would you like to play again y/n? "; //prompt user to restart the program
cin >> 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
// filename: Die.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Die.h"
usingnamespace 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;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//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 :)
You forgot to #include something in Exercise.cpp. :P
Just so you know, having a usingnamespace 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?
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
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.