Hello,

I have 3 major problems:
1) I am not sure if I am doing my static test method correctly.
2) I am not sure if the wagering class is actually checking the bankBalance and the wager
3) I have the following error, which I am not sure how to correct because I was
wondering what I should do if my test failed? Line 177 is the last line of code, its
complaining about my last function. The error reads:

(177) : warning C4715: 'Wagering::wageringTest' : not all control paths return a value


My code is the following:

// Craps game simulation with wagering.

#include "Dice.h"


Dice::Dice()
{
   srand ( (unsigned)time ( 0 ) );	// initialize the random number generator
}

int rollDice( void ); // function prototype
Status craps( void ); // function prototype
void chatter( void ); // function prototype

// roll dice, calculate sum and display results
int Dice::rollDice( void )
{
   die1 = 1 + rand() % 6; // pick random die1 value
   die2 = 1 + rand() % 6; // pick random die2 value
   return die1 + die2; // sum die1 and die2
}
void Dice::displayValue()
{
   // display results of this roll
   cout << "Player rolled " << die1 << " + " << die2
      << " = " << die1 + die2 << endl;
} // end function displayValue

// craps plays one game of craps, returns result of game
Status DiceGame::playCraps( void )
{
   sum = die.rollDice(); // first roll of dice
   die.displayValue();

   // determine game status and point based on sum of dice
   switch ( sum ) 
   {
      case 7: // win on first roll
      case 11:
         gameStatus = WON;
         break;
      case 2: // lose on first roll
      case 3: 
      case 12:
         gameStatus = LOST;
         break;
      default: // remember point
         gameStatus = CONTINUE;
         myPoint = sum;
         cout << "Point is " << myPoint << '\n';
         break;
   } // end switch

   // while game not complete ...
   while ( gameStatus == CONTINUE ) 
   {
      chatter(); // create "chatter"
      sum = die.rollDice(); // roll dice again
	  die.displayValue();
   
      // determine game status
      if ( sum == myPoint )
         gameStatus = WON; // win by making point
      else 
      {
         if ( sum == 7 ) // lose by rolling 7
            gameStatus = LOST;
      } // end else
   } // end while

   // display won or lost message and return status
   if ( gameStatus == WON ) 
   {
      cout << "Player wins" << endl;
      return WON;
   } // end if
   else 
   {
      cout << "Player loses" << endl;
      return LOST;
   } // end else
} // end function craps

// chatter displays messages at random to create "chatter"
void DiceGame::chatter()
{
   // choose message at random
   switch ( rand() % 9 ) 
   {
      case 0:
         cout << "\nOh, you're going for broke, huh?";
         break;
      case 1:
         cout << "\nAw cmon, take a chance!";
         break;
      case 2:
         cout << "\nHey, I think this guy is going to break the bank!!";
         break;
      case 3:
         cout << "\nYou're up big. Now's the time to cash in your chips!";
         break;
      case 4:
         cout << "\nWay too lucky! Those dice have to be loaded!";
         break;
      case 5:
         cout << "\nBet it all! Bet it all!";
         break;
      case 6:
         cout << "\nCan I borrow a chip?";
         break;
      case 7:
         cout << "\nLet's try our luck at another table.";
         break;
      case 8:
         cout << "\nYou're a cheat! It is just a matter of time before I catch you!!!";
         break;
   } // end switch

   cout << endl;
} // end function chatter

bool DiceGame::test()
{
	Status result; // result of current game
   double wager = 100; // wager for current game
   double bankBalance = 1000; // current bank balance
   int games = 20;
		
   DiceGame d1;
		   // loop until user enters sentinel value
   do 
   {
      // display current balance and prompt for wager
      cout << "You have $" << bankBalance << " in the bank.\n";

      result = d1.playCraps(); // play game of craps

      if ( result == LOST ) // if player lost current game
      {
         bankBalance -= wager; // decrease balance by wager

         // display  new balance
         cout << "Your new bank balance is $" << bankBalance << "\n";

         if ( bankBalance == 0 ) // balance is 0
         {
            // display message
            cout << "Sorry. You Busted! Thank You For Playing.\n";
            break; // break out of while loop
         } // end if
      } // end if
      else // else, player won current game
      {
         bankBalance += wager; // increase balance by wager

         // display new balance
         cout << "Your new bank balance is $" << bankBalance << "\n";
      } // end else

	  Wagering::wageringTest(wager, bankBalance);
      games--;

   } while ( games > 0 );
   return true;
}

bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
	if (currentBankBalance > 0)
	{
		while(currentWager > currentBankBalance)
		{
			return true;
		}	
	}
}

Recommended Answers

All 6 Replies

> if (currentBankBalance > 0)
If this is false, you fall off the end of the function.
Without a return statement (that's what the error message means), you return garbage to the caller.

> if (currentBankBalance > 0)
If this is false, you fall off the end of the function.
Without a return statement (that's what the error message means), you return garbage to the caller.

ok but now I have changed the end of the code, just the function and where I use the function to the following (below) but my error is the same:

if(Wagering::wageringTest(wager, bankBalance))
	  {
      games--;
	  }
	  else
	  {
		  games = 0;	// to terminate the loop and exit
		  cout << "Game over.";
	  }

   } while ( games > 0 );
   return true;
}

bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
	if (currentBankBalance > 0)
	{
		while(currentWager <= currentBankBalance)
		{
			return true;
		}	
	}
	else if(currentBankBalance < 0) 
	{
		return false;
	}
}

because when i use this code

if(Wagering::wageringTest(wager, bankBalance))
	  {
      games--;
	  }
	  else
	  {
		  games = 0;	// to terminate the loop and exit
		  cout << "Game over.";
	  }

i am trying to say that if the function returns a false, then the balance is 0 or the wage is higher and none of these should occur the so the game should end, and that is why I made game = 0 so i could end the do loop and exit the function and then the program

bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
	if (currentBankBalance > 0)
	{
		while(currentWager <= currentBankBalance)
		{
			return true;
		}	
	}
	else if(currentBankBalance < 0) 
	{
		return false;
	}
        return 0;
}

????

????

Below is my entire code. The error is:

(187) : warning C4715: 'Wagering::wageringTest' : not all control paths return a value

Code:

// Craps game simulation with wagering.

#include "Dice.h"


Dice::Dice()
{
    srand ( (unsigned)time ( 0 ) ); // initialize the random number generator
}

int rollDice( void ); // function prototype
Status craps( void ); // function prototype
void chatter( void ); // function prototype

// roll dice, calculate sum and display results
int Dice::rollDice( void )
{
   die1 = 1 + rand() % 6; // pick random die1 value
   die2 = 1 + rand() % 6; // pick random die2 value
   return die1 + die2; // sum die1 and die2
}
void Dice::displayValue()
{
   // display results of this roll
   cout << "Player rolled " << die1 << " + " << die2
      << " = " << die1 + die2 << endl;
} // end function displayValue

// craps plays one game of craps, returns result of game
Status DiceGame::playCraps( void )
{
   sum = die.rollDice(); // first roll of dice
   die.displayValue();

   // determine game status and point based on sum of dice
   switch ( sum ) 
   {
      case 7: // win on first roll
      case 11:
         gameStatus = WON;
         break;
      case 2: // lose on first roll
      case 3: 
      case 12:
         gameStatus = LOST;
         break;
      default: // remember point
         gameStatus = CONTINUE;
         myPoint = sum;
         cout << "Point is " << myPoint << '\n';
         break;
   } // end switch

   // while game not complete ...
   while ( gameStatus == CONTINUE ) 
   {
      chatter(); // create "chatter"
      sum = die.rollDice(); // roll dice again
      die.displayValue();

      // determine game status
      if ( sum == myPoint )
         gameStatus = WON; // win by making point
      else 
      {
         if ( sum == 7 ) // lose by rolling 7
            gameStatus = LOST;
      } // end else
   } // end while

   // display won or lost message and return status
   if ( gameStatus == WON ) 
   {
      cout << "Player wins" << endl;
      return WON;
   } // end if
   else 
   {
      cout << "Player loses" << endl;
      return LOST;
   } // end else
} // end function craps

// 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 before I catch you!!!";
         break;
   } // end switch

   cout << endl;
} // end function chatter

bool DiceGame::test()
{
    Status result; // result of current game
   double wager = 100; // wager for current game
   double bankBalance = 1000; // current bank balance
   int games = 20;

   DiceGame d1;  // loop until user enters sentinel value
   do 
   {
      // display current balance and prompt for wager
      cout << "You have $" << bankBalance << " in the bank.\n";

      result = d1.playCraps(); // play game of craps

      if ( result == LOST ) // if player lost current game
      {
         bankBalance -= wager; // decrease balance by wager

         // display  new balance
         cout << "\nYour new bank balance is $" << bankBalance << "\n";

         if ( bankBalance == 0 ) // balance is 0
         {
            // display message
            cout << "\nSorry. You Busted! Thank You For Playing.\n";
            break; // break out of while loop
         } // end if
      } // end if
      else // else, player won current game
      {
         bankBalance += wager; // increase balance by wager

         // display new balance
         cout << "\nYour new bank balance is $" << bankBalance << "\n";
      } // end else

      if(Wagering::wageringTest(wager, bankBalance))
      {
      games--;
      }
      else
      {
          games = 0;    // to terminate the loop and exit
          cout << "Game over.";
      }

   } while ( games > 0 );
   return true;
}

bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
    if (currentBankBalance > 0)
    {
        while(currentWager <= currentBankBalance)
        {
            return true;
        }   
    }
    else if(currentBankBalance < 0) 
    {
        return false;
    }
}

It's a warning, not an error:

bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
	if (currentBankBalance > 0)
	{
		while(currentWager <= currentBankBalance)
		{
			return true;
		}	
	}
	else if(currentBankBalance < 0) 
	{
		return false;
	}
}

You can change the "while" to an "if" in line 5. You'll never repeat this while loop. What if the curentBankBalance == 0? What do you want to have the program do then and can that ever happen? If it can happen, your bool function won't return anything. What if currentBankBalance > 0 and currentWager > currentBankBalance? Can that happen and if so, what should the function return? Right now the function doesn't return anything in those circumstances. That's the warning. If you are positive this will never ever happen, then you can ignore the warning, though even then it never hurts to somehow handle such "impossible" situations. But if they CAN happen, then you NEED to handle them. Currently you do not.

It's a warning, not an error:

You can change the "while" to an "if" in line 5. You'll never repeat this while loop. What if the curentBankBalance == 0? What do you want to have the program do then and can that ever happen? If it can happen, your bool function won't return anything. What if currentBankBalance > 0 and currentWager > currentBankBalance? Can that happen and if so, what should the function return? Right now the function doesn't return anything in those circumstances. That's the warning. If you are positive this will never ever happen, then you can ignore the warning, though even then it never hurts to somehow handle such "impossible" situations. But if they CAN happen, then you NEED to handle them. Currently you do not.

thank you. this is what I changed it to and it worked:

bool Wagering::wageringTest(double currentWager, double currentBankBalance)
{
	if (currentBankBalance >= 0)
	{
		if(currentWager <= currentBankBalance)
		{
			return true;
		}	
		else
		{
			return false;
		}
	}
	else 
	{
		return false;
	}
}
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.