1

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.

3
  • 2
    Your problems come from mixing single character getchar() with multi-char input in scanf or fgets. When you press Enter, that produces a character as well, which you have to handle. Commented Sep 5, 2015 at 16:19
  • 1
    Use either scanf(" %c", &mar); or getchar(); mar=getchar(). The problem is because of the newline character left over by the previous scanf. Commented Sep 5, 2015 at 16:19
  • 1
    when calling the system function: scanf(), 1) always check the returned value (not the parameter value) to assure the operation was successful. 2) when using a input/format string that does not automatically skip over white space, insert a space into the format string before the (for instance) %s, so any leading white space is automatically consumed. 3) when using the format %s always use the max length modifier (which needs to be 1 less than the actual input buffer length) I.E. %14s for a 15 char input buffer. Commented Sep 6, 2015 at 8:11

2 Answers 2

3

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);
        }  
    }
Sign up to request clarification or add additional context in comments.

6 Comments

I used the scanf(" %c", &mar); but I would rather use the fgets due to it being a better habit, but when I try to do it it gives me warnings, saying that its a pointer from a integer, when clearly its a cast, is there any way to do this correctly?
@Asad Mahmood "when I try to do it"?? it is unclear.
@Asad Mahmood What is unclear is your use of fgets(). An example of your the code that uses fgets() would be helpful.
Instead of the scanf(" %c", &mar); I tried to do it with fgets(mar, 1, stdin); When I compile it, it says "warning: passing argument 1 of âfgetsâ makes pointer from integer without a cast [enabled by default]" , I wrote it then like this :fgets(mar, 1, stdin); and no errors were found, however when I compile and run the program and I get to the point where it asks if the female is married, it goes to the else statement and prints Unexpected input char: '' code:0.
@Asad Mahmood fgets(mar, 1, stdin) will not work. 1) first argument nneds to be a char *, not char. 2) fgets() wants to read a line of data and form a string, that takes at least an char s[3]. 3) fgets() does not code well with scanf(). 4) Unfortunately, the idea of using fgets() vs. scanf() involves removing all scant(). Suggest you do that on your next assignment.
|
2

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();
...

1 Comment

or maybe he should just cease using scanf() altogether instead of trying to hack around its horrible behavior. OP wants to read one piece of data by line; therefore he should use fgets(). Using fgets() is also safer as it eliminates buffer overflow errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.