I'm trying to construct a semi simple dice rolling project. I have two classes, onedie and threedice. The threedice class looks like this:

class threedice
{
	public:
		void rollem();  	  
		int getsumFaces();

	private:

		onedie die1;
		onedie die2;
		onedie die3;

};

Now, when I try to code the two functions rollem and getsumFaces, I don't know how to write those functions to add the three results from onedie. I've made a couple of attempts that give me nothing but a string of errors. Any example I can adapt will do, thank you in advance

Recommended Answers

All 5 Replies

It would help to see your onedie class as well. Does it have some method such as rolldie? How have you tried to write these two functions?

coming up

class onedie
{
	public:
		void rollit();
		int getroll();

	private:
		int result;

};

Here are the onedie functions (they may not be right either, I'm a tad weak in function writing)

#include "onedie.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


void onedie::rollit()
{
	srand(time_t(NULL));
	result= 1 + rand() % 6;
}

int onedie::getroll()
{
	return result;
}

as for trying to call the onedie class, since I made three members that call it onedie die1, onedie die2 etc. I tried the simplest possible for getsumFaces being return die1+die2+die3...that didn't work

I think it's a bit much to have separate rollit( ) and getroll( ) methods - wouldn't it be simpler to just have a getroll method that that also does the random value generation. Is there a reason to separate the two concepts?

In any case, as you have it now, the rollem( ) method should simply call the rollit( ) method for each of the three dice die1.rollit( ); die2.rollit( ); die3.rollit( ); and similarly your getSumFace( ) would use the three dice's getroll( ) methods.

Hmm, it would be easier to write if you had an array of dice instead of three individual members.

Can you now put it all together in a functioning program?

The reason they are separated is "professor says so" :) I do have to construct arrays, one to hold the rolls, and one to "sort" the results using a switch statement.

Can I put it all together? I don't know yet, but I'm workin at it slow but sure. I'm starting small and working my way up. I didn't know the syntax for calling from the other object. So for getsumFaces, would it look like?:

return die1.getroll()+die2.getroll()+die3.getroll()

It compiles, so until I can actually run it I'll take that as a yes :)

OK, if the assignment needs the two functions, that's what you do. Yes, you have a correct statement to implement your getsumFaces( ).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.