Search code examples
c++structured-programming

Comparing chars in C++ - argument of type "char" is incompatible with parameter of type "const char *


Im trying to compare 2 values in C++ (which im new to so please go easy)

struct styles{
int itemNo;
char desc[26]; 
char brand[21]; //between 3 - 20
char category;
double cost;
};  

struct declared above, then im using this code in another function

char cat[2];
for (x=0;x<size;x++)
{

    if (strcmp(cat,styleAr[x].category)==0)

its giving me an error with the 'styleAr[x].category' in the if statement: argument of type "char" is incompatible with parameter of type "const char *

any explanations on how I could solve this would be great


Solution

  • Assuming that cat is a string (that is, one character followed by a '\0'), then you should do this:

    if (cat[0] == styleAr[x].category)
    

    But of course, if you use std::string cat and std::string category, you can just write:

    if (cat == styleAr[x].category)