0

strings variable conditions in c

I am a beginner... can someone help me in writting a code with an if statement having strings as the variable example scanf("%s%s%d", & marital_status, & gender, & age) ; if((gender==male) && marital_status == married & && age>37))...this is not working. help me with the Syntax of solving such a problem

17th Feb 2019, 8:09 PM
Paul Wanjohi
Paul Wanjohi - avatar
3 Respostas
+ 2
scanf("%d", &var), but scanf("%s", var) (without &). if(marital_status == "married") (in quotes), same for gender. && instead of &&&
17th Feb 2019, 8:16 PM
Anna
Anna - avatar
+ 2
In c you can't compare string content with == You must use strcmp(s1, s2); It returns 0 if both strings are equal. Ej. scanf("%s %s", marital_status, gender); if( (strcmp(marital_status, "married") == 0) && (strcmp(gender, "male")==0) ) { ..... ..... } #include <string.h> to use string finctions such as strcmp()
18th Feb 2019, 12:43 AM
unChabon
unChabon - avatar
0
Thanks guys
18th Feb 2019, 6:36 AM
Paul Wanjohi
Paul Wanjohi - avatar