Not working as it should be !

+2 Gitesh Khanna · July 2, 2015
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char sex[5] , mar[5] ;
    int age ;

    printf("Please enter your sex here(M/F): ") ;
    scanf("%s" , sex) ;

    printf("What is your marital status(M/U): ") ;
    scanf("%s" , mar) ;

    printf("What is your age? ") ;
    scanf("%d" , &age) ;

    if((mar=='M') || (mar=='U' && age>30 && sex=='M') || (mar=='U' && age>25 && sex=='F'))
        printf("Congratulations! You are eligible.") ;
        else
        printf("Sorry, but you are not eligible.") ;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Aju Antony · July 2, 2015
after scanning marriage, include fflush(stdin) line

...
scanf("%s",mar);

fflush(stdin);

printf("what is your age?");
...

not sure if this will actually work, i just remember this from my school days.. :)
0 Gitesh Khanna · July 2, 2015
Thank you for the reply, @Ajay Antony , but even if i add it i wont know what it means. Can you explain me something about it? :)
+1 c student · July 3, 2015
fflush (stdin) invokes undefined behaviour and should only be used for output streams.  the problem you are having is that you are comparing a character array with a character.  to fix this, simply change your character array types into normal character types:
char sex, mar;

then use scanf to read as a character:
...

scanf ("%c", &sex);

...

scanf ("%c", &mar);

...
0 Gitesh Khanna · July 3, 2015
@C Student
Thankyou so much !


It really worked!

But i still don't understand what is the difference between char sex and char sex[] with a specified memory of bytes which can be stored in.


  1. What is the difference between character arrays and normal character ?

  2. What is the difference between %s and %c ?



Also, i remember Bucky telling in his tutorial that '&' wasnt required when we are trying to get an input from the user which is a string. He told that it is only required when we are using %d .

Thankyou for the reply! Really appreciate !
+1 c student · July 3, 2015
char foo = 'M';

means that you are declaring a variable of type char.  although deep down in memory, there isn't really anything that says if something is a char or not.  a char is just a variable type that means one byte in size.  the character M is 1 byte in size so that works nicely.
char bar[5] = "M";

means that you are declaring a variable with 5 char types.  again, in a computer, it is just interpreted as 5 bytes (1 byte for each char).  it looks something like this:
[M] [0] [0] [0] [0]

for the sake of simplicity, let's just say that all the elements were initialized to 0 (sometimes it might not be).

the difference between the two is that one stores only one character whereas the other stores an array of characters.  in the most primitive sense, a string is just an array of characters and in c, a string must be null-terminated.  so "M" in its basic string equivalent form is:
[M] [0]


the %c format means character of course, and when you use scanf to read in a character, you must specify the address of the variable so that scanf can put the input into memory demonstrated like so:
scanf ("%c", &foo);    /* puts input into the address of foo */


however, when you use %s, it reads in a sequence of non-white space characters and puts it into the address of the specified variable plus a null-terminator:
scanf ("%s", bar);    /* reads input until white space and then adds in a null terminator */

yes, Bucky is right, it isn't necessary to place the address of operator before the variable when writing into an array and that's because by default, arrays are referenced to the address of their first element.  the above is equivalent to:
scanf ("%s", &bar[0]);

you need to specify the address of the variable in all other cases, not just with the %d format.

scanf manpage:
http://linux.die.net/man/3/scanf
0 Gitesh Khanna · July 5, 2015
Thank You so much for the reply :)
Really appreciate !
  • 1

C

107,005 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator