Array Values As Histogram

How do I set up my code to call a class object from another class?
I'm supposed to have it roll a die, get the face value, then use that portion to roll 3 dice and get the sum of those values.

My Code:
Die.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef DIE_H					
#define DIE_H

class Die

{
public:										
		Die();								
		void Roll();							
		int GetFaces();

private:									
		int Face;
};
            	END OF CONDITIONAL BLOCK
#endif 


Die.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Die.h"				
#include <cstdlib>					

Die::Die()					
{
int Face = 0;
}

void Die::Roll()
{
    Face = rand() % 6 + 1;	
}
 
int Die::GetFaces()
{
    return Face;
}


DiceRoll.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef DIEROLL_H			
#define DIEROLL_H				

class DiceRoll					

{
public:
		void DiceRoll();		
		int GetRollFaces();		

private:
		Die Die1;				
		Die Die2;				
		Die Die3;				
};

#endif 


And DiceRoll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>  				
#include <cstdlib>					
#include "DieRoll.h"				
#include "Die.h"

     void DiceRoll::DiceRoll() 
	{
		this->Roll();
	}
     int DiceRoll::GetRollFaces()
	{
                // insert computation for 3 die
        }
Last edited on
1
2
3
4
Die::Die()					
{
int Face = 0;
}

Remove the 'int' here. By putting the 'int' here you're creating a new variable named Face instead of zeroing the member variable Face.

1
2
3
4
     void DiceRoll::DiceRoll() 
	{
		this->Roll();
	}


You have 3 dice: Die1, Die2, and Die3.

If you want to roll them, you call Roll with each of them, not from 'this':

1
2
3
4
5
6
     void DiceRoll::DiceRoll() 
	{
		Die1.Roll();
		Die2.Roll();
		Die3.Roll();
	}
I see your logic, but changing the code as suggested gives me a plethora of errors. I'm going through them now to try to problem solve them one by one, but here they are:

dieroll.h(41): error C2380: type(s) preceding 'DiceRoll' (constructor with return type, or illegal redefinition of current class-name?)
dieroll.h(45): error C2146: syntax error : missing ';' before identifier 'Die1'
dieroll.h(45): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(45): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(46): error C2146: syntax error : missing ';' before identifier 'Die2'
dieroll.h(46): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(46): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(47): error C2146: syntax error : missing ';' before identifier 'Die3'
dieroll.h(47): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(47): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

dieroll.cpp(55): error C2533: 'DiceRoll::{ctor}' : constructors not allowed a return type
dieroll.cpp(56): error C2065: 'Die1' : undeclared identifier
dieroll.cpp(56): error C2228: left of '.Roll' must have class/struct/union
1> type is ''unknown-type''
dieroll.cpp(57): error C2065: 'Die2' : undeclared identifier
dieroll.cpp(57): error C2228: left of '.Roll' must have class/struct/union
1> type is ''unknown-type''
dieroll.cpp(58): error C2065: 'Die3' : undeclared identifier
dieroll.cpp(58): error C2228: left of '.Roll' must have class/struct/union
1> type is ''unknown-type''
DieRoll.h should be #including Die.h, since Die is an include dependency.

See section 4:
http://www.cplusplus.com/forum/articles/10627/#msg49679

EDIT:

also, your constructor should not be returning void. Ctors and dtors have no return type:

1
2
3
{
public:
		/*void*/ DiceRoll();	// <- get rid of that 'void' 


1
2
     /*void*/ DiceRoll::DiceRoll()    // <- here too
	{
Last edited on
Die and DiceRoll are now both working to specifications, next I need to create an array to store the values and then for extra credit (as we haven't covered it yet) create a vertical histogram using '*' for the values. Any suggestions?
Well, I went ahead and kept working on my problems, and have most of them resolved. However, I have to take the data in the array and output it into a histogram (vertical and horizontal). Here's my code thus far. Could anyone assist me with the rest?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef DIE_H					// allows for additional
#define DIE_H

class Die

{
public:										// available outside of class
		Die();								// SMF to set value
		void Roll();							// member functions
		int GetFaces();

private:									// not available outside class
		int Face;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Die.h"				// for processing die face
#include <cstdlib>					// for the library rand() function

Die::Die()						//Initializes Face data member
{
 Face = 1;
}

void Die::Roll()
{
    Face = rand() % 6 + 1;
	
}
 
int Die::GetFaces()
{
    return Face;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef DIEROLL_H			// allows for additional
#define DIEROLL_H				

#include "Die.h"

class DiceRoll					// classs that specifies a collection of 3 contained Die objects

{
public:
		void RollDice();				// Calls Roll() function on each contained die
		int GetRollFaces();		// Returns the sum of the current Face value of Die1, Die2, & Die3

private:
		Die Die1;				// The three die contained in this -i.e
		Die Die2;				// objects of this class automatically contain
		Die Die3;				// three dice
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>  				// for cin,cout
#include <cstdlib>					// for the library rand() function
#include "DieRoll.h"				// for processing die roll
#include "Die.h"

	void DiceRoll::RollDice()		
	{
		Die1.Roll();
		Die2.Roll();
		Die3.Roll();
	}
	int DiceRoll::GetRollFaces()
	{
		int result;
		result = Die1.GetFaces()+Die2.GetFaces()+Die3.GetFaces();
		return result;
	}


1
2
3
4
5
6
7
8
#ifndef SHELL_H   // Avoid duplicate compilations
#define SHELL_H   //

void GatherStats(int RollsArray[], int RollsArraySize, int ResultsArray[]); // 1st global function prototype
void DisplayResults(int ResultsArray[ ], int ResultsArraySize ); //2nd global function prototype
void createHistogram(int frequency[], int range);				 // function to display histogram

#endif 


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
#include <iostream>  				// for cin,cout
#include <iomanip>
#include <cstdlib>					// for the library rand() function
#include <cmath>
#include <ctime>
#include "FreeFunction.h"			// for processing the roll
#include "Die.h"				// for processing die face
#include "DieRoll.h"				// for processing die roll


using namespace std;
void GatherStats(int RollsArray[], int RollsArraySize, int ResultsArray[])
{
	for ( int i = 0; i <200; i++)
	{
		switch ( RollsArray[ i ] ) 
		{
		case 3: ResultsArray[ 0 ] += 1; break;
		case 4: ResultsArray[ 1 ] += 1; break;
		case 5: ResultsArray[ 2 ] += 1; break;
		case 6: ResultsArray[ 3 ] += 1; break;
		case 7: ResultsArray[ 4 ] += 1; break;
		case 8: ResultsArray[ 5 ] += 1; break;
		case 9: ResultsArray[ 6 ] += 1; break;
		case 10: ResultsArray[ 7 ] += 1; break;
		case 11: ResultsArray[ 8 ] += 1; break;
		case 12: ResultsArray[ 9 ] += 1; break;
		case 13: ResultsArray[ 10 ] += 1; break;
		case 14: ResultsArray[ 11 ] += 1; break;
		case 15: ResultsArray[ 12 ] += 1; break;
		case 16: ResultsArray[ 13 ] += 1; break;
		case 17: ResultsArray[ 14 ] += 1; break;
		case 18: ResultsArray[ 15 ] += 1;
		}
	}
}

void DisplayResults(int ResultsArray[ ], int ResultsArraySize )
{
	for ( int i = 0; i < 200; i++)
	{
		cout<< "\n\nOut of 200 Rolls of 3 Dice: \n\n"<< endl;
		cout << "The number " << i + 1 << " was rolled " << ResultsArray[i] << " times." << endl;
	}
}

//Creates Histogram of scores
void createHistogram(int frequency[], int range)
{
       for(int i = 0;i<=range;i++)
	   {
		   cout<<setw(3)<<i<<setw(3)<<frequency[i];
		   for(int j = 1;j<=frequency[i];j++)
		   {
			   cout<<"*";
			   cout<<endl;
		   }
	   }
}


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
#include <iostream>  				// for cin,cout
#include <iomanip>
#include <cstdlib>					// for the library rand() function
#include "FreeFunction.h"			// for processing the roll
#include "DieRoll.h"				// for processing die roll


using namespace std;
int main ()
{
	const int NUM_RANGE = 15;
	int frequency[NUM_RANGE+1];
	int RollsArray[100];
	DiceRoll TheIvories;
	for (int i = 0; i < 100; i++)
	{
	TheIvories.RollDice();						// Roll the dice
	RollsArray[i] = TheIvories.GetRollFaces(); // Record the result of the roll end loop
	}
		for (int j = 0; j< 100; j++)
	{
		cout << RollsArray[j]<< endl;
	
	}


return 0;
}

Topic archived. No new replies allowed.