Hello WouldyCaulk,
I am not sure if you are familiar with using separate file for the class and member functions, but it would be better to split them up.
Starting at the top:
<stdio.h> is a C header file and should not be used in a C++ program. <iostream> is all you need to handle input and output.
<stdlib.h> and <time.h> should be <cstdlib> and <ctime>.
using namespace std;
should be avoided.
Your class would work better something like this:
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
|
#ifndef _DICE_
#define _DICE_
class Dice //Dice class
{
private:
int dice1Roll, dice2Roll; //all of the private stored variables that will be accessed by later functions
int total2;
int total1;
public:
Dice(); // <--- ctor.
~Dice(); // <--- dtor.
int rollDicePlayer();
int rollDiceComputer();
// ******* Get functions ******
int GetDice1Roll();
int GetDice2Roll();
int GetTotal1();
int GetTotal2();
// ******* Set functions ******
//void SetDice1Roll(); // <--- If needed.
};
#endif // end !_DICE_
|
The variables "total1" and "total2" are better put in the private section. As you have them they are in the :public" section and the entire program has access to them. This tends to defeat the purpose of the class.
This will give you an idea what I did with the "Dice.cpp" file:
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
|
#include <iostream>
#include <cstdlib> // <--- Put here for "rand()".
#include "Dice.hpp" // <--- Sorry for any confusion. I use ".hpp" for C++ header files.
Dice::Dice() // <--- ctor.
{
dice1Roll = 0; // <--- Variables are initialized here.
dice2Roll = 0;
total1 = 0;
total2 = 0;
// <--- As a one liner.
//dice1Roll = dice2Roll = total1 = total2 = 0;
}
Dice::~Dice() {} // <--- dtor.
int Dice::GetDice1Roll()
{
return dice1Roll;
}
int Dice::GetDice2Roll()
{
return dice2Roll;
}
int Dice::GetTotal1()
{
return total1;
}
int Dice::GetTotal2()
{
return total2;
}
// These next two functions are what you wrote.
//int Dice::rollDicePlayer()
//{
|
This should give you an idea of what needs to be there. It does not matter if you put this all in the main file or separate files.
For what you have accessing the "total" variables is a matter of "d->total1". When I looked at main closer making "d" a "Dice*" is not necessary unless you have a need to do this that I have not seen yet. Defining "d" as "Dice d;" is all you need to create an object/instance of "Dice". Then to access the public members of the class it would be "d.GetDice1Roll()" or "d.GetTotal1();". To access the "private" members of the class you would use the
Get" and "set" functions.
You should try and avoid using "system" anything. First it is specific to windows, so not everyone can use it fo a pause waiting for a key press I use this:
1 2 3 4
|
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
|
Mostly I use this right before the "return" in main to keep the console window before "return" has a chance to end the program. This can be used anywhere you have used "system("pause");".
If you would want a timed pause I use this line:
std::this_thread::sleep_for(std::chrono::seconds()); // Requires header files "chrono" and "thread"
The program does compile and run which is a good start. I still need to go through the program and check some of the variables and may have to make some adjustments to what I have changed.
Hope that helps,
Andy