why this code does not work properly

0 girish gogia · June 4, 2015
#include <stdio.h>
#include <stdlib.h>

int main()
{
int age;
char gender;

       printf("how old are u\n");
       scanf(" %d", &age);
printf("gender? (m/f) \n");
       scanf(" %s", &gender);

       if(age >= 18){
        printf("welcome ");

        if(gender == 'm'){
            printf("dude");
        }
if(gender == 'f'){
        printf("m'lady");
}
        }
if(age < 18){
    printf("nothing");

}




return 0;
}


Post a Reply

Replies

Oldest  Newest  Rating
0 girish gogia · June 23, 2015
thanks for your help guys.
0 Maneh Clovis · June 11, 2015
I think it is more easy this way. give it a try.




#include <stdio.h>
#include <stdlib.h>

int main()
{
int age;
char gender;

printf("how old are u\n");
scanf(" %d", &age);

printf("gender? (m/f) \n");
scanf(" %c", &gender);

if(age >= 18)
printf("welcome ");

else
   printf("nothing for you here");


if(gender == 'm')
printf(" dude");
else
    printf(" m'lady");


return 0;
}
0 Suraj Gautam · June 6, 2015
scanf("%s", gender);
remove & sign dude and it will work
0 Franz Schmidt · June 5, 2015
I estimate you got a error like stack around variable corrupted or something like this. But if you have a problem and your code crashes please post your error as well.

So your problem is the scanfof your gender.
You wrote scanf(" %s", &gender);
the s from %s is a placeholder for a string but you only enter a single char so you need to replace the s with a c, which stands for character (char) and your problem will be solved.
  • 1

C

107,174 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator