Home > other >  Why doesn't this code for doing string comparisons with user input compile?
Why doesn't this code for doing string comparisons with user input compile?

Time:02-12

#include <stdio.h>
int main()
{
    int age;
    char marietal_status[15], sex[15];

    printf("Age, Marietal status, sex of the driver : ");
    scanf("%i %s %s", &age, marietal_status, sex);

    if((marietal_status == married) || (marietal_status == unmarried && age > 30 && sex == male) 
 || (marietal_status == unmarried && age >25 && sex == female))
    printf("insured");
    else
    printf("uninsured");
}

ouput:

gcc /tmp/NaKWq16d9Q.c -lm
/tmp/NaKWq16d9Q.c: In function 'main':
/tmp/NaKWq16d9Q.c:10:28: error: 'married' undeclared (first use in this function)
   10 |     if((marietal_status == married) || (marietal_status == unmarried && age > 30 && sex == male) || (marietal_status == unmarried && age >25 && sex == female))
      |                            ^~~~~~~
/tmp/NaKWq16d9Q.c:10:28: note: each undeclared identifier is reported only once for each function it appears in
/tmp/NaKWq16d9Q.c:10:60: error: 'unmarried' undeclared (first use in this function)
   10 |     if((marietal_status == married) || (marietal_status == unmarried && age > 30 && sex == male) || (marietal_status == unmarried && age >25 && sex == female))
      |                                                            ^~~~~~~~~
/tmp/NaKWq16d9Q.c:10:92: error: 'male' undeclared (first use in this function)
   10 | ried) || (marietal_status == unmarried && age > 30 && sex == male) || (marietal_status == unmarried && age >25 && sex == female))
      |                                                              ^~~~

/tmp/NaKWq16d9Q.c:10:152: error: 'female' undeclared (first use in this function)
   10 |  && sex == male) || (marietal_status == unmarried && age >25 && sex == female))
      |                                                                        ^~~~~~

CodePudding user response:

married, unmarried, male and female are undeclared entities.

It seems you mean string literals "married", "unmarried", "male" and "female".

But such a comparison like for example this

marietal_status == "married"

will always evaluate to false because there are compared two pointers.

Instead you need to use the C string function strcmp declared in the header <string.h>.

So the if statement will look

#include <string.h>

//...

if( ( strcmp( marietal_status, "married" ) == 0) || 
    ( strcmp( marietal_status, "unmarried" ) == 0 && 
      age > 30 && strcmp( sex, "male" ) == 0 ) 
    || ( strcmp( marietal_status, "unmarried" ) == 0 && age >25 && strcmp( sex, "female" ) == 0 ) )

CodePudding user response:

In C you can't compare two char array using ==. You have to use function strcmp like below:

#include <stdio.h>
#include <string.h>

int main()
{
    int age;
    char marietal_status[15], sex[15];

    printf("Age, Marietal status, sex of the driver : ");
    scanf("%i %s %s", &age, marietal_status, sex);

    if(strcmp(marietal_status, "married") == 0 || (strcmp(marietal_status, "unmarried") == 0 && age > 30 && strcmp(sex, "male")) 
 || (strcmp(marietal_status, "unmarried") == 0 && age >25 && strcmp(sex, "female") == 0))
        printf("insured");
    else
        printf("uninsured");
}

CodePudding user response:

You're trying to compare a variable, the compiler is telling you that it is undeclared (which means it doesn't exist, you didn't set it like int age for example which is a declared variable).

When working with strings use double quotations, "

For example when initializing a string, char sex[15] = "female";

So your if condition should look like this:

if((strcmp(marietal_status, "married") == 0) || (strcmp(marietal_status, "unmarried") == 0) && age > 30 && (strcmp(sex, "male") == 0) || (strcmp(marietal_status, "unmarried") == 0) && age >25 && (strcmp(sex,  "female") == 0))

Running it produces this:

./a.out
Age, Marietal status, sex of the driver : 23 unmarried male
uninsured

Note that this means you expect the user to input exactly this, otherwise it won't be matched by the comparison. I'd recommend going through a C tutorial, https://www.tutorialspoint.com/cprogramming/index.htm

Edit:As someone else has already pointed out, string comparison in C is done by the strcmp function, don't forget to #include <string.h>

marietal_status == "unmarried" would work in some other language like Python or JavaScript

  • Related