Derrick Rose
Derrick Rose

Reputation: 674

Why doesn't getchar() receive any input?

I made a program to see what your name is, if you were a male or female, how old you are, and if I should call you as Mrs, Ms, Mr, or just by your full name depending on the previous conditions. When I pick my gender as female and enter a first name, last name, and a age that's over or equal to 20 I ask if that person is married, however for whatever reason the program skips over the getchar, and just finishes the program.

Here is the code:

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

int main()
{
    char gender;
    char fName[15]; 
    char lName[15];
    char mar;
    char str[] = "Then I shall call you";
    int age = 0;

    printf("What is your gender (M or F): ");
    gender = getchar();

    printf("First name: ");
    scanf("%s", fName);
    //fgets(fName, 16, stdin);
    //fName[strcspn(fName, "\n")] = 0;

    printf("Last name: ");
    scanf("%s", lName);
    //fgets(lName, 16, stdin);
    //lName[strcspn(lName, "\n")] = 0;

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

    puts("");

    if(gender == 'F' || gender == 'f')
    {
        if(age >= 20)
        {   
            printf("Are you married, %s (y or n)?: ", fName);

            //scanf("%c", &mar);
            mar=getchar();

            printf("%c\n", mar); 

            if(mar == 'Y' || mar == 'y')
                printf("%s Mrs. %s.\n", str, lName);

            else if(mar == 'n' && age >= 20|| mar == 'N' && age >= 20)
                printf("%s Ms. %s.\n", str, lName);
        }

        else
            printf("%s %s %s.\n", str, fName, lName);
    }

    else if(gender == 'M' || gender == 'm')
    {
        if(age >= 20)
            printf("%s Mr. %s.\n", str, lName);

        else
            printf("%s %s %s.\n", str, fName,lName);
    }   

    return 0;
}

And the output:

What is your gender (M or F): F
First name: Jane
Last name: Doe
Age: 25

Are you married, Jane (y or n)?: 

I also have another question as to when I used fgets instead of scanf to read the string. As I heard to typically stay away from scanf when reading strings I tried fgets but the output wasn't as I wanted it to be.

Here is the output when I used fgets instead of scanf:

What is your gender (M or F): M 
First name: Last name: Joe
Age: 23

Then I shall call you Mr. Joe.

The output should be as it was when I used the scanf so that The last name is underneath the first name.

Upvotes: 1

Views: 581

Answers (2)

chux
chux

Reputation: 153447

Why doesn't getchar() receive any input?

Code has 2 problems:

1) It got the left-over '\n' from the previous line, use scanf(" %c", &mar); (note space) or better yet, replace all input with fgets()

2) It did not report unexpected input. Code should detect and report unexpected input and handle all possible paths of logic. Example:

    {   
        printf("Are you married, %s (y or n)?: ", fName);
        mar=getchar();
        printf("%c\n", mar); 
        if(mar == 'Y' || mar == 'y')
            printf("%s Mrs. %s.\n", str, lName);
        else if(mar == 'N' || mar == 'n') {
           if (age >= 20)
             printf("%s Ms. %s.\n", str, lName);
           else 
             printf("%s ??? %s.\n", str, lName);
        }
        else { 
          printf("Unexpected input char:'%c' code:%d\n", mar, mar);
        }  
    }

Upvotes: 3

qwertz
qwertz

Reputation: 14792

The problem is scanf. It does not remove the new-line character from the input buffer, so the first thing getchar reads is the \n-char.

To solve this you can, for instance add a getchar call before, so the character gets removed from the buffer and the next getchar reads your input. Like this:

...
scanf("%d", &age);
...
getchar();
mar = getchar();
...

Upvotes: 2

Related Questions