vendredi 2 avril 2021

variable is not being evaluated and skips if statement c

I have a question. When I try to ask a user to enter yes or no as a single character and set the char variable with brackets as I either get that Y or y is not valid in my if statement. If I do it without brackets I only get the ascii value which will evaluate in the if statement. The following code would fail to evaluate recAnswer value. When I output the value in a printf it would show that recAnswer = "Y" or "y" depending on the input. I've tried if(&recAnswer == "Y") and if(*recAnswer = "Y")

int main(int argc, char** argv)
{
  int numEntered;
  char recAnswer[1];

  while(numEntered < 1 || numEntered > 15) // Continually ask user to enter a number until the number entered is between 1 and 15 . 
  {
     printf("\nPlease enter a number between 1 and 15:");
     scanf(" %d", &numEntered);
  } 
  printf("\nDo you want to get the factorial value recursively? Enter Y or N:"); //Ask user if they want to get the answer recursively 
  scanf(" %c",&recAnswer);
     
if(recAnswer == "y" || recAnswer == "Y")
{
  printf("The recursive value of %d is %d", numEntered, recursive(numEntered)); //Print out recursive value
}   
else
{
  printf("The non-recursive value of %d is %d", numEntered, nonRecursive(numEntered));  //Print out looped value
}
return 0;
}

Thank you for looking into this

Aucun commentaire:

Enregistrer un commentaire