Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C "comparison between pointer and integer" warning for menu

Tags:

c

pointers

I'm trying to write a program that involves comparing chars as part of a menu system for a program that decodes resistor codes. At the end of the menu it asks if you want to decode another resistor, repeating the program if the user selects "yes".

This is the code I have so far:

  //used for repeating input menu
  int menu = 0;
  char menuChoice = 0;

  //rest of program goes here      

  printf("Do you want to decode another resistor? (y/n)");
  scanf("%c", &menuChoice);

  while(menuChoice != "y" && menuChoice != "Y" && menuChoice != "n" && menuChoice != "N")
  {
    if(menuChoice != "Y" && menuChoice != "y")
    {
      if(menuChoice == "N" || menuChoice == "n")
      {
        menu = 0;
      }

      else
      {
        printf("Invalid choice.");
      }
    }
  }

When I try to compile with GCC, I end up with a warning that says "comparison between pointer and integer." Since scanf only accepts a pointer, I don't know how exactly to compare the scanned char with to "Y" or "n". Is there something I'm missing here?

like image 417
user2309750 Avatar asked Jan 26 '26 02:01

user2309750


1 Answers

You are using the string literal syntax "a" instead of the char literal syntax 'a'

More about the difference

like image 130
horns Avatar answered Jan 27 '26 18:01

horns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!