Array and Ampersand

+1 Sandesh Gupta · January 19, 2016
Hello Guys,
Need will with the following program, why is it not working :


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

int main()
{


 int age;

 char Sex[20];

 printf("Enter your age kid!\n");
 scanf(" %d", &age);

 printf("What are you? <M/F>\n");
 scanf("% s", Sex);

 if(age >=  18)
    {
    printf("Alright kid you may go ahead!\n");

    if(Sex == 'M')
    {
        printf("Get lost idiot!\n");
    }

    if (
        Sex == 'F')
    {
        printf("Get lost bitch!\n");

    }
    }


 if (age <= 18)
 {
     printf("Go home kid you are drunk\n");
 }
    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Laura Lee · January 20, 2016
Sex is a pointer, why are you comparing a pointer to a single character 'F'? 
0 Eva Rays · January 20, 2016
Do you know about character and int, and difference? so why are you comparing of both? both are different value.
0 Sandesh Gupta · January 20, 2016
I am learning C and i don't understand pointer yet... So i made following changes but program is still not working (Tutorial -18 http://pastebin.com/88c7fBLN) :

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

int main()
{


 int age;

 char Sex;

 printf("Enter your age kid!\n");
 scanf(" %d", &age);

 printf("What are you? <M/F>\n");
 scanf("% c", &Sex);

 if(age >=  18)
    {
    printf("Alright kid you may go ahead!\n");

    if(Sex == 'M')
    {
        printf("Get lost idiot!\n");
    }

    if (
        Sex == 'F')
    {
        printf("Get lost bitch!\n");

    }
    }


 if (age <= 18)
 {
     printf("Go home kid you are drunk\n");
 }
    return 0;
}
0 Linguist Llama · January 20, 2016
Your compiler ought to have warned you about this... Did you bother reading the compiler warnings?

OTOH, a decent resource ought to have taught you about this. If you can't be bothered reading a book to learn C, I suppose you're not going to care what your compiler thinks. So, why ask us? Do you think we are better at teaching than the books we used to learn? How can you think that, if you haven't yet read the books?
0 Sandesh Gupta · January 20, 2016
@linguist   Sir, compiler is giving any warning, the program is running  but it is not printing the part  "Get lost idiot!\n" / "Get lost bitch!\n" .....
0 Sandesh Gupta · January 20, 2016
Thank you  Guys,

The only problem was that , there was space between '%' and 'c.' The following programs is working fine:

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

int main()
{
 int age;

 char Sex;

 printf("Enter your age kid!\n");
 scanf(" %d", &age);

 printf("What are you? <M/F>\n");
 scanf(" %c", &Sex);

 if(age >= 18)
    {
    printf("Alright kid you may go ahead!\n");

    if(Sex == 'M')
    {
        printf("Get lost idiot!\n");
    }

    if (
        Sex == 'F')
    {
        printf("Get lost bitch!\n");

    }
    }


 if (age <= 18)
 {
     printf("Go home kid you are drunk\n");
 }
    return 0;

}
  • 1

C

107,279 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator