Issue in taking character input to C program

510 Views Asked by At

I am trying to run a program that check one input character from user when they enter either M, F or N. It will check and compare char character. but when I run the program. It doesn't work. I have try many medhod with my friends and we can't figure this out. The program can't valid data that user enter. Some help please.

typedef struct {
    char onePatient.gender;
}PATIENT_T

PATIENT_T onePatient;

is a typedef already declared on the top, sorry for that.

This is program function:

void checkGender(char pointerGender)
{
char gender;
char buffer[32];
char word_M = ('M'); /* Male */
char word_F = ('F'); /* Female */
char word_N = ('N'); /* None */

printf("\nEnter Gender: ");
fgets(buffer,sizeof(buffer),stdin);
//strcpy(&gender,buffer);
sscanf(buffer,"%c",&gender);

while((strcasecmp(&gender,&word_M) != 0)||(strcasecmp(&gender,&word_M) != 0)||(strcasecmp(&gender,&word_N) != 0))
{
    printf("\nError: Invalidate Grade");
    printf("\n(Please enter wheather M [for Male], F [for Female] or N [for None])");
    printf("\nEnter gender: ");
    fgets(buffer,sizeof(buffer),stdin);
    //strcpy(&gender,buffer);
    sscanf(buffer,"%c",&gender);
}

}

This is in main function:

int main()
{
    char pointerGender;

    PATIENT_T onePatient;

    checkGender(pointerGender);
    strcpy(&onePatient.gender,&pointerGender);

    printf("\n%s",&onePatient.gender);
}

Terminal show:

192:iHeal Hospital iFluke$ gcc -o checkFormat_Fluke checkFormat_Fluke.c
192:iHeal Hospital iFluke$ ./checkFormat_Fluke

Enter Name: fluke

Enter Gender: M

Error: Invalidate Grade
(Please enter wheather M [for Male], F [for Female] or N [for None])
Enter gender: F

Error: Invalidate Grade
(Please enter wheather M [for Male], F [for Female] or N [for None])
Enter gender: N

Error: Invalidate Grade
(Please enter wheather M [for Male], F [for Female] or N [for None])
Enter gender: 
3

There are 3 best solutions below

0
On BEST ANSWER

You can't use strcasecmp to compare characters. It's not completely wrong, but a single character is not a properly terminated string.

If you must do comparison this way, use strncasecmp:

if(strncasecmp(&gender,&word_M, 1) == 0)

That will limit it to comparing one character.

Also, your retry loop doesn't check for 'F', but checks 'M' twice.

8
On

Please check below code. I think, it solves your purpose, minus the complexity.

Note:

  1. EOF is achieved by pressing CTRL + D

  2. This code is case insensitive [M and m denotes same thing].

Code:

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

int main()
{
    char userInput;

    printf("Enter the Gender ( M [for Male], F [for Female] or N [for None] )\n");
    while ( scanf (" %c", &userInput) != EOF )
    {
    switch (userInput)
    {
        case 'M':
        case 'm':
                    printf("Male\n");
                    break;

        case 'F':
        case 'f':
                    printf("Female\n");
                    break;

        case 'N':
        case 'n':
                    printf("None\n");
                    break;

        default:
                printf("Wrong value !!!\n");
    }
    }
    return 0;
}
0
On

Since your input is only a single char, why have you declared buffer[32] ?

Use something like

char input;
scanf("%c" &input);