-2

Was making a small code for a text based game but i tried to do thinks a bit different than the tutorial sort of to test my understanding but i tried to get a function value to make a do while statement but it seems whether the statement false or true the code just keep looping infinitely im a beginner so pls if you have a time explain why the code is faulty and Thank you in Advance

The main function

int main() {

    PrintIntroStart();
    do {
        PlayGame();
        AskToPlayAgain();
    } while (AskToPlayAgain() == true);
    return 0;
}

the Bool Function

bool AskToPlayAgain(){
//Asking The Player Whether To Play Again Or Not
std::string PlayerResponse = "";

std::cout << "Do You Want To Play Again? (Yes/No)" << std::endl;
std::cin >> PlayerResponse;
if (PlayerResponse[0] == 'y' || 'Y') 
    return true;
else
    return false;
}
Banana
  • 2,435
  • 7
  • 34
  • 60
  • 2
    Last four lines of your function should be `return (PlayerResponse[0] == 'y' || PlayerResponse[0] == 'Y')`. – Eziz Durdyyev Feb 16 '18 at 12:30
  • 2
    Remove the `AskToPlayAgain();` from inside the loop, you only need it in the `while (AskToPlayAgain());` part. – super Feb 16 '18 at 12:31

3 Answers3

3

Also, here you are asking twice to play again

do {
    PlayGame();
    AskToPlayAgain();
   } while (AskToPlayAgain() == true);

this should be

do {
    PlayGame();
} while (AskToPlayAgain() == true);
ziza
  • 96
  • 1
  • 6
2

if (PlayerResponse[0] == 'y' || 'Y') this one should be

if (PlayerResponse[0] == 'y' || PlayerResponse[0] == 'Y') 

Otherwise your if condition is always true, because 'Y' itself is non-zero.

And in fact you don't need this if statement, just

return PlayerResponse[0] == 'y' || PlayerResponse[0] == 'Y';
llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • " because 'Y' itself in non-zero. " can i ask what do you mean by this? cause i thought codes will check the matching Ascii value with the intended one.But i might be wrong – Sempak Balado Feb 16 '18 at 13:26
  • Sorry Nvm i didnt read the bottom answer – Sempak Balado Feb 16 '18 at 13:28
  • Just remember C is not like a natural language. `PlayerResponse[0] == 'y' || 'Y'` this one might be natural for human, but not compiler. For compiler, it means `(PlayerResponse[0] == 'y') || 'Y'. Thus `'Y'` is not comparing to the `PlayerResponse[0]`. – llllllllll Feb 16 '18 at 13:31
  • why you did not mentioned the problem in the main function? and you are doing copy-paste from comments.. – Eziz Durdyyev Feb 16 '18 at 14:00
  • @EzizDurdyyev Please check the time for post, my answer is 12:24:34, your comment is 12:30:09. Who was copying ? – llllllllll Feb 16 '18 at 14:03
  • @EzizDurdyyev If this is your reason for downvote, please remove it, it's ridiculous. – llllllllll Feb 16 '18 at 14:06
  • yes, maybe you answered it at 12:24:34, but you edited it right after my comment. I mean it is not a big deal for me. But SO must find a solution for problems like this. This is not even an answer. You and @ziza could just mention it in comments section. Anyways.. – Eziz Durdyyev Feb 16 '18 at 14:08
  • @EzizDurdyyev No. Answer is answer, comment is comment, they are different things. "Comments exist so that users can talk about questions and answers without posting new answers **that do not make an attempt to answer the question asked**." Quoted from [how do comments works](https://meta.stackexchange.com/questions/19756/how-do-comments-work) – llllllllll Feb 16 '18 at 14:12
0

So first of all, you are only checking the outcome of the game every second play. You should follow @ziza s answer and remove the function call inside the loop.

The second problem, that causes the infinite loop is your input check. A bool value in c++ is not a single bit, but just anoter regular value that is evaluated as false if it is 0 and to true if it has any other value. In your case the first comparison will evauate to 1 if the player input is 'y' and the second part of the condition will be the value of the 'Y' character (which is not 0). So your condition will always be true like @liliscent stated.

Nefrin
  • 338
  • 3
  • 11