This is a cut down version of my code below it keeps on looping through playGame()
when y or n is entered I think it is a problem with 'if (playGame)' and 'if (!playGame) but not sure how else to do it.
Any help would be appreciated Thanks
#include <iostream>
using namespace std;
bool playGame()
{
//variables
char yesNo = '\0';
//loop while player does not enter either y, Y, n or N
do
{
if( (yesNo != 'y') && (yesNo != 'Y') && (yesNo != 'n') && (yesNo != 'N') )
{
cout<<"\n Sorry?! Just answer \'y\' or \'n\' \n\n";
}//end of if
//ask the player if he/she wants to play
cout<<" Do you want to play?";
//allow the player to key in either 'y' or 'n'
cin>>yesNo;
}while((yesNo!='y') && (yesNo!='Y') && (yesNo!='n') && (yesNo != 'N') );
//return true for 'y' and false for 'n'
if ( (yesNo == 'y') || (yesNo == 'Y') )
{
return true;
}else
{
return false;
}//end of else if
}//playGame()
int main()
{
playGame();
if (playGame() )
{
cout<<"yes\n\n";
}else if (!playGame() )
{
cout<<"no\n\n";
}
}//main()
First, please use the [ code ] and [ /code ] brackets when posting code, it makes it easier to read and helps replying, so that way we can reference lines.
Second to answer your problem, you may want to try this in your main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main()
{
bool status;
status = playGame();
if (status)
{
...
}
if (!status)
{
...
}
}
because you aren't returning your bool and setting it to a variable. I don't think playGame stores itself. So best to set it to a bool variable you declare in your main.