Reputation: 39
This is my first time working with classes in C++ and I seem to be getting tripped up quite a lot. My program is supposed to be a rewrite of a previous program that used struct (see here: Random number generator in a for loop gives same numbers each time), but using a class instead.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
const int WHEEL_POSITIONS = 30;
const char wheelSymbols[WHEEL_POSITIONS + 1] = "-X-X-X-X-X=X=X=X*X*X*X*X@X@X7X";
class slotMachine
{
private:
int spinPos;
char spinSymbol;
public:
slotMachine(); // Constructor
char symbols[WHEEL_POSITIONS + 1]; // Should be private?
void setSpinSymbol(); // Spins the wheels
char getSpinSymbol() const // Returns the symbol
{ return spinSymbol; }
} wheels[3];
// Constructor initializes slot wheels to contents of wheelSymbols
slotMachine::slotMachine()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < (WHEEL_POSITIONS + 1); j++)
{
wheels[i].symbols[j] = wheelSymbols[j];
}
}
}
void slotMachine::setSpinSymbol()
{
for (int i = 0; i < 3; i++)
{
wheels[i].spinPos = (rand() % WHEEL_POSITIONS);
wheels[i].spinSymbol = wheels[i].symbols[(wheels[i].spinPos)];
}
}
void displayResults(slotMachine fwheels[3])
{
for (int i = 0; i < 3; i++)
{
cout << fwheels[i].getSpinSymbol();
}
}
void displayResults(slotMachine []);
//bool getWinner(slotMachine []);
int main(void)
{
slotMachine wheels[3];
time_t seed;
time(&seed);
srand(seed);
displayResults(wheels);
return 0;
}
The code compiles but outputs the following:
I have a feeling this error is caused by something having gone amiss in my constructor slotMachine
, my getSpinSymbol()
function, or my setSpinSymbol()
function, but I've looked it over several times and can't seem to figure it out. I've read a handful of material online covering classes in C++, but I'm still very new and very shaky on the concept--apologies if it's something small or obvious that I've overlooked.
Upvotes: 0
Views: 339
Reputation: 39
The problem was explained to me by a friend not on StackOverflow, and I will transcribe his answer here in case anyone else (for any reason) runs into the same problem:
You aren't using constructors and methods correctly. You shouldn't be accessing
wheels
(the array of slotMachine objects) directly inside those methods; you should just be performing operations on "this," the slotMachine object on which the method was called. For example, the constructorslotMachine::slotMachine()
is automatically called for each element of the array wheels. You just need to initialize the current slotMachine object inside the constructor:slotMachine::slotMachine() { for (int j = 0; j < (WHEEL_POSITIONS + 1); j++) { this->symbols[j] = wheelSymbols[j]; } }
And
slotMachine::setSpinSymbol()
should just set the value of spinSymbol for the object on which the method was called:void slotMachine::setSpinSymbol() { this->spinPos = (rand() % WHEEL_POSITIONS); this->spinSymbol = symbols[this->spinPos]; }
(In all of this code, the
this->
part is actually unnecessary; you can leave it out if you want. I put it in to try to make it clearer that these methods are operating on fields of "the current object.")Now, the reason you are getting garbage is because you never call
setSpinSymbol()
, so the spinSymbol field is never initialized in these objects. You probably want to callsetSpinSymbol()
in the constructor, so that the spinSymbol field is guaranteed to be initialized.
This explanation did solve my problem, and my program now outputs the correct information, so I believe it to be correct. My issues with using constructors and methods correctly has been explained here, and the reason why I was getting garbage values (as well as a few other points) was answered by another commenter.
Upvotes: 0
Reputation: 7625
There are several issues with your code:
1.Class names should be started with upper case letter. slotMachine -> SlotMachine
2.Remove wheels[3] after class definition.You are using the array declared in main() method.
3.Why you are declaring displayResults(..) again after it's definition?
4.You are not calling setSpinSymbol() before displayResults(..).
Upvotes: 1