Question

I wrote a bool function again() to prompt the user to re-run the program, but for whatever answer the user enters the program will re-run no matter what.

bool again()
{
  //Local variable                                                                                                                        
  char answer;

  cout<<"Would you like to re-run? (Y/N) ";
  cin>>answer;
  cin.ignore(100,'\n');


  if(answer == 'Y' || 'y')
  {
    return true;
  }

  if(answer == 'N'  || 'n')
  {
     return false;
  }
}

...

  do
       {
         menu(gameamount,response);

         if (response == 1)
         {
           inputs(gameamount, game_count);
           writeout(gameamount);
         }
         if (response == 2)
         {
           outputs(gameamount, game_count);
         }
       }while(again());

       return 0;
}

I tried using else and else if and returning false after the first if-statement but that still did not work, I can't put the block of code in main to conserve space so I have to create a boolean function to ask the user to re-run the program.

Was it helpful?

Solution

There's a logic error in your code.

Try this:

if(answer == 'Y' || answer == 'y')
{
    return true;
}

if(answer == 'N'  || answer == 'n')
{
    return false;
}

You should also possibly output a message in case the user decides to type something other than 'Y','y','N' or 'n'.

Also, I'm not 100% sure about this, but I think that your code is always returning true in the first if statement because you are checking to see if 'y' is true, which I think it always is.

if(answer == 'Y' || 'y')
{
    return true;
}

Any value other than 0 would equate to true, and the character 'y' when treated as an integer is equal to it's corresponding ASCII code (121 in this case).

OTHER TIPS

Try to replace the line

if(answer == 'Y' || 'y')

by

if(answer == 'Y' || answer == 'y')

The line

if (answer == 'Y' || 'y')

will enter the if block in every case, since it won't evaluate to what you intended. It evaluates answer == 'Y' which is true or false, but then ORs this value with 'y'. In this step, 'y' is converted to a boolean value in order to be ORed logically, and since all numbers unequal to 0 are converted to true the whole expression is always true.

If you correct this to

if (answer == 'Y' || answer == 'y')

you still have the following problem:

Your function only returns a value in the case where the user enters either a Y or an N in lower or upper case. But what if the user enters an invalid letter? You need to define a behavior for this situation. Currently your program doesn't have a return statement for this case, resulting in undefined behavior, which means the result of the function can be anything.

You can either return a default value (e.g. false), then you need to remove the second conditional if but keep the return false.

Another solution is to loop until the user entered a valid value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top