I am learning C. What I want my program to do is prompt for three variables compare them to specific values using if statement and return true and print a welcome massage only if (all) comparisons are true. Any thing else return false and print a sorry massage. But when I compile and run the code, which is shown below.
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main()
{ int years;
char name[15];
char gender [7];
char evaluation [7];
char age [2];
printf("enter name, please ");
fgets(name,15,stdin);
printf("enter gender, please ");
fgets(gender,7,stdin);
printf("enter evaluation, please ");
fgets(evaluation,20,stdin);
printf("Please enter age ");
fgets(age,2,stdin);
years = strlen(age);
{
if((strcmp(gender,"male")== 1) && (strcmp(evaluation, "good" )== 1) && years < 50 )
printf("welcome volunteer, %s . You're fit to participate\n", name);
else
printf("Sorry, %s. You're not fit to participate/n", name);
}
return(0);
}
-It compiles with no errors and no warnings. But, - the if statement returns true if any of the first two comparisons, gender and evaluation, is true.
but, - it returns false when all are false. and - when the first two comparisons ,gender and evaluation,are both false and the third comparison ,age, is true it returns false.
I would like to know what's wrong with my if statement. Thanks for your help
Aucun commentaire:
Enregistrer un commentaire