| | |
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:
Solved Threads: 0
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:
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:
C++ Syntax (Toggle Plain Text)
// 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; } } }
Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value
0
#2 Jun 30th, 2008
•
•
Join Date: May 2008
Posts: 96
Reputation:
Solved Threads: 0
Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value
0
#3 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.
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
if(Wagering::wageringTest(wager, bankBalance)) { games--; } else { games = 0; // to terminate the loop and exit cout << "Game over."; }
Last edited by QuantNeeds; Jun 30th, 2008 at 2:27 am.
•
•
Join Date: May 2008
Posts: 99
Reputation:
Solved Threads: 1
Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value
0
#4 Jun 30th, 2008
C++ Syntax (Toggle Plain Text)
bool Wagering::wageringTest(double currentWager, double currentBankBalance) { if (currentBankBalance > 0) { while(currentWager <= currentBankBalance) { return true; } } else if(currentBankBalance < 0) { return false; } return 0; }
????
•
•
Join Date: May 2008
Posts: 96
Reputation:
Solved Threads: 0
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
????[/QUOTE]
Below is my entire code. The error is:
(187) : warning C4715: 'Wagering::wageringTest' : not all control paths return a value
C++ Syntax (Toggle Plain Text)
// 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; } }
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value
0
#6 Jun 30th, 2008
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: May 2008
Posts: 96
Reputation:
Solved Threads: 0
Re: warning C4715: 'Wagering::wageringTest' : not all control paths return a value
0
#7 Jun 30th, 2008
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
bool Wagering::wageringTest(double currentWager, double currentBankBalance) { if (currentBankBalance >= 0) { if(currentWager <= currentBankBalance) { return true; } else { return false; } } else { return false; } }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Keeping random number inside bounds.
- Next Thread: Pairs in random numbers
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






