简体   繁体   中英

C “comparison between pointer and integer” warning for menu

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?

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

More about the difference

Adjust your comparisons. Presently they compare a an integer (or char such as menuChoice) to an array (such as "y").

// menuChoice != "y"
menuChoice != 'y'

The while(menuChoice != "y") && ...) likely should be removed.

The if(menuChoice == "N" ... should be else if(menuChoice == "N" ...) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM