0
#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))
      |                                                                        ^~~~~~
0

3 Answers 3

2

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 ) )
Sign up to request clarification or add additional context in comments.

Comments

1

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");
}

5 Comments

And also you can't use variable names that aren't declared.
Which variable name is not declared here?
Damn it, totally got lost when I was trying to explain what is a declared variable that I forgot string comparison doesn't work the way OP thought it does in C
married was not declared in the original code. You fixed it without mentioning it, that's why I added the comment.
Got it. I think the OP didn't want to make married a variable. The OP was trying to compare the input with married but didn't know that he/she has to put it inside double quotation @mkrieger1
-1

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

2 Comments

Thanks, it worked
Please check the other answers, the code I've written here before my edit does not give the expected outcome because string comparison isn't done with == in C

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.