Hello,
I am working on two separate programs because I was trying one differently. I got my other program’s issue solved but now I would like to complete this one. But I am little lost on the error because the data it is complaining about is part of the class. I have included my header file code for verification.
The error is:
(126) : error C2677: binary '==' : no global operator found which takes type 'DiceGame:: Status' (or there is no acceptable conversion)
My header file:
// Craps class definition
class Dice
{
public:
int rollDice(); // function that rolls the dice and calculates the sum
};
class DiceGame
{
public:
void play(); // function play controls the details of the game
void chatter();
static bool test();
private:
enum Status { CONTINUE, WON, LOST }; // enumeration with constants that represent the game status - all caps in constants
int myPoint; // point if no win or loss on first roll
Status gameStatus; // can contain CONTINUE, WON or LOST
};
class Wagering
{
public:
static bool wageringTest(double, double);
};
My code is the following:
#include "Craps.h" // include definition of class Craps
// function that rolls the dice and calculates the sum
int Dice::rollDice()
{
// randomize random number generator using current time
srand ( (unsigned)time ( 0 ) );
// pick random die values
int die1 = 1 + rand() % 6; // first die roll
int die2 = 1 + rand() % 6; // second die roll
int sum = die1 + die2; // compute sum of die values
// display results of this roll
cout << "Player rolled " << die1 << " + " << die2 << " = " << sum << endl;
return sum; // end function rollDice
} // end function rollDice
// function play will control the details of the game
void DiceGame::play()
{
Dice rollDice;
int sumOfDice = rollDice.rollDice(); // first roll of the dice
// determine game status and point (if needed) based on first roll
switch ( sumOfDice )
{
case 7: // win with 7 on first roll
case 11: // win with 11 on first roll
gameStatus = WON;
break;
case 2: // lose with 2 on first roll
case 3: // lose with 3 on first roll
case 12: // lose with 12 on first roll
gameStatus = LOST;
break;
default: // did not win or lose, so remember point
gameStatus = CONTINUE; // game is not over
myPoint = sumOfDice; // remember the point
cout << "Point is " << myPoint << endl;
break; // optional at end of switch
} // end switch
// while game is not complete
while ( gameStatus == CONTINUE ) // not WON or LOST
{
chatter();
sumOfDice = rollDice.rollDice(); // roll dice again
// determine game status
if (sumOfDice == myPoint) // win by making point
gameStatus = WON;
else
if (sumOfDice == 7) // lose by rolling 7 before point
gameStatus = LOST;
} // end while
}
// chatter displays messages at random to create "chatter"
void DiceGame::chatter()
{
// choose message at random
switch ( rand() % 9 )
{
case 0:
cout << "Oh, you're going for broke, huh?";
break;
case 1:
cout << "Aw cmon, take a chance!";
break;
case 2:
cout << "Hey, I think this guy is going to break the bank!!";
break;
case 3:
cout << "You're up big. Now's the time to cash in your chips!";
break;
case 4:
cout << "Way too lucky! Those dice have to be loaded!";
break;
case 5:
cout << "Bet it all! Bet it all!";
break;
case 6:
cout << "Can I borrow a chip?";
break;
case 7:
cout << "Let's try our luck at another table.";
break;
case 8:
cout << "You're a cheat! It is just a matter of time"
<< "\nbefore I catch you!!!";
break;
} // end switch
cout << endl;
} // end function chatter
bool DiceGame::test()
{
double wager = 100.00; // wager for current game
double bankBalance = 1000.00; // current bank balance
int games = 20;
do
{
DiceGame roll; // create object
roll.play();
// display won or lost message
if( gameStatus == WON )
{
bankBalance += wager;
cout << "\nPlayer wins. Your new balance is $" << bankBalance << endl;
}
else
{
bankBalance -= wager;
if(bankBalance <= 0)
{
cout << "\nSorry. You busted!\n";
}
else
{
cout << "\nPlayer loses. Your new balance is $" << bankBalance << endl;
}
}
//roll.displayMessage();
roll.chatter();
if(Wagering::wageringTest(wager, bankBalance))
{
games--;
}
else
{
games = 0; // to terminate the loop and exit
cout << "Game over Wager or balance issue Game terminated.";
}
} while ( games > 0 );
return true;
}
bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
if (currentBankBalance >= 0)
{
if(currentWager <= currentBankBalance)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}