1

How to control user input for char type in C programming??

Problem: If user give 'Male' as first input then program did not ask to give 'age'

Output

Enter gender(M/F):Male

Enter age:

Your Gender is Male(M)

Your Age:71 //garbage value

#include <stdio.h>

int main()
{
    char gender;
    int age;

    printf("\nEnter gender(M/F):");
    scanf("%c", &gender);

    printf("\nEnter age:");
    scanf("%d", &age);

    if(gender=='M'){
        printf("\nYour Gender is Male(%c)",gender);
    }else{
        printf("\nYour Gender is not Male(%c)",gender);
    }

    printf("\nYour Age a:%d",age);

    return 0;
}

Expected Output: if user give input 'Male'/'Female'/'M'/'F' then hit enter then console will ask to give age

Enter gender(M/F):Male

Enter age:23

Your Gender is Male(M) //base on &gender

Your Age:23

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    C != C++. Tag only with the language that you're using, unless both are actually relevant. – tambre Oct 21 '17 at 07:18
  • 2
    I suggest you check the return value from `scanf` *every single time you use it*. If you type in **Male** and read one character, what do you suppose will be read when you try to scan the integer? – Weather Vane Oct 21 '17 at 07:19
  • On which operating system? See [this](https://stackoverflow.com/a/46763120/841108) – Basile Starynkevitch Oct 21 '17 at 07:38
  • 1
    I've seen this before. Maybe [Scanf skips every other while loop in C](https://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c), or [c - scanf won't ask for input the second time](https://stackoverflow.com/questions/13372923/scanf-wont-ask-for-input-the-second-time), or [c - My program skip getting input data?](https://stackoverflow.com/questions/18721375/my-program-skip-getting-input-data), of [c - scanf Getting Skipped - Stack Overflow](https://stackoverflow.com/questions/14484431/scanf-getting-skipped) – David C. Rankin Oct 21 '17 at 07:39
  • BTW, put `\n` at end of `printf` format control strings (or use `fflush`). Compile with all warnings `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/) and **use the debugger** `gdb`. Read [documentation](http://en.cppreference.com/w/c) of every function before using it. – Basile Starynkevitch Oct 21 '17 at 07:40

2 Answers2

1

You want to read a sting, but you are using a character, so change your code to this:

char gender[7];
scanf("%6s", gender);
...
if (gender[0] == 'm' || gender[0] == 'M')
    printf("\nYour Gender is Male(%c)", gender[0]);
...

Second solution:

Just use fgets() for gender, since you want to be able to read a string, and then scanf() for age, like this:

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

int main()
{
    char gender[7];
    printf("Enter gender(M/F): ");
    if (!(fgets(gender, sizeof(gender), stdin) != NULL)) {
        fprintf(stderr, "Error reading Gender.\n");
        exit(1);
    }
    gender[strcspn(gender, "\n")] = 0;
    int age;
    printf("\nEnter age:");
    scanf("%d", &age);

    if (gender[0] == 'm' || gender[0] == 'M')
        printf("\nYour Gender is Male(%c)", gender[0]);
    else if (gender[0] == 'f' || gender[0] == 'f')
        printf("\nYour Gender is Female(%c)", gender[0]);
    else
        printf("Unrecocognized gender\n");
    printf("\nYour Age is: %d\n",age);

    return 0;
}

Output:

Enter gender(M/F): f
Enter age: 25
Your Gender is Female(f)
Your Age is: 25
gsamaras
  • 71,951
  • 46
  • 188
  • 305
-2

Use gets() or char array and Strcmp() to compare strings.

#include <stdio.h>
#include <string.h>

int main()
{

char gender[40];
int age;

printf("\nEnter gender(M/F):");
gets(gender);

printf("\nEnter age:");
scanf("%d", &age);

if(gender=="M" || (strcmp(gender, "Male")==0)){
    printf("\nYour Gender is Male(%s)",gender);
}else{
    printf("\nYour Gender is not Male(%s)",gender);
}

printf("\nYour Age a:%d",age);

return 0;
}
iamsaquib8
  • 414
  • 6
  • 15
  • 2
    `gets` is so insecure and susceptible to buffer overrun it has been removed from the C11 Standard. Never, never use `gets`. Use `fgets` instead. (that said, I do applaud your suggestion to use a *line oriented* input function instead of a *formatted input* function) – David C. Rankin Oct 21 '17 at 07:41
  • `gender=="M"` ==> `gender[0]=='M'` and also it is better not to mix your input methods. – Weather Vane Oct 21 '17 at 07:42