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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

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

 
0
  #1
Jun 30th, 2008
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:
  1.  
  2.  
  3. // Craps game simulation with wagering.
  4.  
  5. #include "Dice.h"
  6.  
  7.  
  8. Dice::Dice()
  9. {
  10. srand ( (unsigned)time ( 0 ) ); // initialize the random number generator
  11. }
  12.  
  13. int rollDice( void ); // function prototype
  14. Status craps( void ); // function prototype
  15. void chatter( void ); // function prototype
  16.  
  17. // roll dice, calculate sum and display results
  18. int Dice::rollDice( void )
  19. {
  20. die1 = 1 + rand() % 6; // pick random die1 value
  21. die2 = 1 + rand() % 6; // pick random die2 value
  22. return die1 + die2; // sum die1 and die2
  23. }
  24. void Dice::displayValue()
  25. {
  26. // display results of this roll
  27. cout << "Player rolled " << die1 << " + " << die2
  28. << " = " << die1 + die2 << endl;
  29. } // end function displayValue
  30.  
  31. // craps plays one game of craps, returns result of game
  32. Status DiceGame::playCraps( void )
  33. {
  34. sum = die.rollDice(); // first roll of dice
  35. die.displayValue();
  36.  
  37. // determine game status and point based on sum of dice
  38. switch ( sum )
  39. {
  40. case 7: // win on first roll
  41. case 11:
  42. gameStatus = WON;
  43. break;
  44. case 2: // lose on first roll
  45. case 3:
  46. case 12:
  47. gameStatus = LOST;
  48. break;
  49. default: // remember point
  50. gameStatus = CONTINUE;
  51. myPoint = sum;
  52. cout << "Point is " << myPoint << '\n';
  53. break;
  54. } // end switch
  55.  
  56. // while game not complete ...
  57. while ( gameStatus == CONTINUE )
  58. {
  59. chatter(); // create "chatter"
  60. sum = die.rollDice(); // roll dice again
  61. die.displayValue();
  62.  
  63. // determine game status
  64. if ( sum == myPoint )
  65. gameStatus = WON; // win by making point
  66. else
  67. {
  68. if ( sum == 7 ) // lose by rolling 7
  69. gameStatus = LOST;
  70. } // end else
  71. } // end while
  72.  
  73. // display won or lost message and return status
  74. if ( gameStatus == WON )
  75. {
  76. cout << "Player wins" << endl;
  77. return WON;
  78. } // end if
  79. else
  80. {
  81. cout << "Player loses" << endl;
  82. return LOST;
  83. } // end else
  84. } // end function craps
  85.  
  86. // chatter displays messages at random to create "chatter"
  87. void DiceGame::chatter()
  88. {
  89. // choose message at random
  90. switch ( rand() % 9 )
  91. {
  92. case 0:
  93. cout << "\nOh, you're going for broke, huh?";
  94. break;
  95. case 1:
  96. cout << "\nAw cmon, take a chance!";
  97. break;
  98. case 2:
  99. cout << "\nHey, I think this guy is going to break the bank!!";
  100. break;
  101. case 3:
  102. cout << "\nYou're up big. Now's the time to cash in your chips!";
  103. break;
  104. case 4:
  105. cout << "\nWay too lucky! Those dice have to be loaded!";
  106. break;
  107. case 5:
  108. cout << "\nBet it all! Bet it all!";
  109. break;
  110. case 6:
  111. cout << "\nCan I borrow a chip?";
  112. break;
  113. case 7:
  114. cout << "\nLet's try our luck at another table.";
  115. break;
  116. case 8:
  117. cout << "\nYou're a cheat! It is just a matter of time before I catch you!!!";
  118. break;
  119. } // end switch
  120.  
  121. cout << endl;
  122. } // end function chatter
  123.  
  124. bool DiceGame::test()
  125. {
  126. Status result; // result of current game
  127. double wager = 100; // wager for current game
  128. double bankBalance = 1000; // current bank balance
  129. int games = 20;
  130.  
  131. DiceGame d1;
  132. // loop until user enters sentinel value
  133. do
  134. {
  135. // display current balance and prompt for wager
  136. cout << "You have $" << bankBalance << " in the bank.\n";
  137.  
  138. result = d1.playCraps(); // play game of craps
  139.  
  140. if ( result == LOST ) // if player lost current game
  141. {
  142. bankBalance -= wager; // decrease balance by wager
  143.  
  144. // display new balance
  145. cout << "Your new bank balance is $" << bankBalance << "\n";
  146.  
  147. if ( bankBalance == 0 ) // balance is 0
  148. {
  149. // display message
  150. cout << "Sorry. You Busted! Thank You For Playing.\n";
  151. break; // break out of while loop
  152. } // end if
  153. } // end if
  154. else // else, player won current game
  155. {
  156. bankBalance += wager; // increase balance by wager
  157.  
  158. // display new balance
  159. cout << "Your new bank balance is $" << bankBalance << "\n";
  160. } // end else
  161.  
  162. Wagering::wageringTest(wager, bankBalance);
  163. games--;
  164.  
  165. } while ( games > 0 );
  166. return true;
  167. }
  168.  
  169. bool Wagering::wageringTest(double currentWager, double currentBankBalance)
  170. {
  171. if (currentBankBalance > 0)
  172. {
  173. while(currentWager > currentBankBalance)
  174. {
  175. return true;
  176. }
  177. }
  178. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value

 
0
  #2
Jun 30th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value

 
0
  #3
Jun 30th, 2008
Originally Posted by Salem View Post
> 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:

  1.  
  2. if(Wagering::wageringTest(wager, bankBalance))
  3. {
  4. games--;
  5. }
  6. else
  7. {
  8. games = 0; // to terminate the loop and exit
  9. cout << "Game over.";
  10. }
  11.  
  12. } while ( games > 0 );
  13. return true;
  14. }
  15.  
  16. bool Wagering::wageringTest(double currentWager, double currentBankBalance)
  17. {
  18. if (currentBankBalance > 0)
  19. {
  20. while(currentWager <= currentBankBalance)
  21. {
  22. return true;
  23. }
  24. }
  25. else if(currentBankBalance < 0)
  26. {
  27. return false;
  28. }
  29. }

because when i use this code
  1.  
  2. if(Wagering::wageringTest(wager, bankBalance))
  3. {
  4. games--;
  5. }
  6. else
  7. {
  8. games = 0; // to terminate the loop and exit
  9. cout << "Game over.";
  10. }
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
Last edited by QuantNeeds; Jun 30th, 2008 at 2:27 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value

 
0
  #4
Jun 30th, 2008
  1. bool Wagering::wageringTest(double currentWager, double currentBankBalance)
  2. {
  3. if (currentBankBalance > 0)
  4. {
  5. while(currentWager <= currentBankBalance)
  6. {
  7. return true;
  8. }
  9. }
  10. else if(currentBankBalance < 0)
  11. {
  12. return false;
  13. }
  14. return 0;
  15. }

????
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value

 
0
  #5
Jun 30th, 2008
[QUOTE=FTProtocol;637233
????[/QUOTE]

Below is my entire code. The error is:

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

  1.  
  2.  
  3. // Craps game simulation with wagering.
  4.  
  5. #include "Dice.h"
  6.  
  7.  
  8. Dice::Dice()
  9. {
  10. srand ( (unsigned)time ( 0 ) ); // initialize the random number generator
  11. }
  12.  
  13. int rollDice( void ); // function prototype
  14. Status craps( void ); // function prototype
  15. void chatter( void ); // function prototype
  16.  
  17. // roll dice, calculate sum and display results
  18. int Dice::rollDice( void )
  19. {
  20. die1 = 1 + rand() % 6; // pick random die1 value
  21. die2 = 1 + rand() % 6; // pick random die2 value
  22. return die1 + die2; // sum die1 and die2
  23. }
  24. void Dice::displayValue()
  25. {
  26. // display results of this roll
  27. cout << "Player rolled " << die1 << " + " << die2
  28. << " = " << die1 + die2 << endl;
  29. } // end function displayValue
  30.  
  31. // craps plays one game of craps, returns result of game
  32. Status DiceGame::playCraps( void )
  33. {
  34. sum = die.rollDice(); // first roll of dice
  35. die.displayValue();
  36.  
  37. // determine game status and point based on sum of dice
  38. switch ( sum )
  39. {
  40. case 7: // win on first roll
  41. case 11:
  42. gameStatus = WON;
  43. break;
  44. case 2: // lose on first roll
  45. case 3:
  46. case 12:
  47. gameStatus = LOST;
  48. break;
  49. default: // remember point
  50. gameStatus = CONTINUE;
  51. myPoint = sum;
  52. cout << "Point is " << myPoint << '\n';
  53. break;
  54. } // end switch
  55.  
  56. // while game not complete ...
  57. while ( gameStatus == CONTINUE )
  58. {
  59. chatter(); // create "chatter"
  60. sum = die.rollDice(); // roll dice again
  61. die.displayValue();
  62.  
  63. // determine game status
  64. if ( sum == myPoint )
  65. gameStatus = WON; // win by making point
  66. else
  67. {
  68. if ( sum == 7 ) // lose by rolling 7
  69. gameStatus = LOST;
  70. } // end else
  71. } // end while
  72.  
  73. // display won or lost message and return status
  74. if ( gameStatus == WON )
  75. {
  76. cout << "Player wins" << endl;
  77. return WON;
  78. } // end if
  79. else
  80. {
  81. cout << "Player loses" << endl;
  82. return LOST;
  83. } // end else
  84. } // end function craps
  85.  
  86. // chatter displays messages at random to create "chatter"
  87. void DiceGame::chatter()
  88. {
  89. // choose message at random
  90. switch ( rand() % 9 )
  91. {
  92. case 0:
  93. cout << "Oh, you're going for broke, huh?";
  94. break;
  95. case 1:
  96. cout << "Aw cmon, take a chance!";
  97. break;
  98. case 2:
  99. cout << "Hey, I think this guy is going to break the bank!!";
  100. break;
  101. case 3:
  102. cout << "You're up big. Now's the time to cash in your chips!";
  103. break;
  104. case 4:
  105. cout << "Way too lucky! Those dice have to be loaded!";
  106. break;
  107. case 5:
  108. cout << "Bet it all! Bet it all!";
  109. break;
  110. case 6:
  111. cout << "Can I borrow a chip?";
  112. break;
  113. case 7:
  114. cout << "Let's try our luck at another table.";
  115. break;
  116. case 8:
  117. cout << "You're a cheat! It is just a matter of time before I catch you!!!";
  118. break;
  119. } // end switch
  120.  
  121. cout << endl;
  122. } // end function chatter
  123.  
  124. bool DiceGame::test()
  125. {
  126. Status result; // result of current game
  127. double wager = 100; // wager for current game
  128. double bankBalance = 1000; // current bank balance
  129. int games = 20;
  130.  
  131. DiceGame d1; // loop until user enters sentinel value
  132. do
  133. {
  134. // display current balance and prompt for wager
  135. cout << "You have $" << bankBalance << " in the bank.\n";
  136.  
  137. result = d1.playCraps(); // play game of craps
  138.  
  139. if ( result == LOST ) // if player lost current game
  140. {
  141. bankBalance -= wager; // decrease balance by wager
  142.  
  143. // display new balance
  144. cout << "\nYour new bank balance is $" << bankBalance << "\n";
  145.  
  146. if ( bankBalance == 0 ) // balance is 0
  147. {
  148. // display message
  149. cout << "\nSorry. You Busted! Thank You For Playing.\n";
  150. break; // break out of while loop
  151. } // end if
  152. } // end if
  153. else // else, player won current game
  154. {
  155. bankBalance += wager; // increase balance by wager
  156.  
  157. // display new balance
  158. cout << "\nYour new bank balance is $" << bankBalance << "\n";
  159. } // end else
  160.  
  161. if(Wagering::wageringTest(wager, bankBalance))
  162. {
  163. games--;
  164. }
  165. else
  166. {
  167. games = 0; // to terminate the loop and exit
  168. cout << "Game over.";
  169. }
  170.  
  171. } while ( games > 0 );
  172. return true;
  173. }
  174.  
  175. bool Wagering::wageringTest(double currentWager, double currentBankBalance)
  176. {
  177. if (currentBankBalance > 0)
  178. {
  179. while(currentWager <= currentBankBalance)
  180. {
  181. return true;
  182. }
  183. }
  184. else if(currentBankBalance < 0)
  185. {
  186. return false;
  187. }
  188. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value

 
0
  #6
Jun 30th, 2008
It's a warning, not an error:

  1. bool Wagering::wageringTest(double currentWager, double currentBankBalance)
  2. {
  3. if (currentBankBalance > 0)
  4. {
  5. while(currentWager <= currentBankBalance)
  6. {
  7. return true;
  8. }
  9. }
  10. else if(currentBankBalance < 0)
  11. {
  12. return false;
  13. }
  14. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value

 
0
  #7
Jun 30th, 2008
Originally Posted by VernonDozier View Post
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:

  1.  
  2. bool Wagering::wageringTest(double currentWager, double currentBankBalance)
  3. {
  4. if (currentBankBalance >= 0)
  5. {
  6. if(currentWager <= currentBankBalance)
  7. {
  8. return true;
  9. }
  10. else
  11. {
  12. return false;
  13. }
  14. }
  15. else
  16. {
  17. return false;
  18. }
  19. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC