In my C program, I call gets() twice to get input from the user. The first time the user is asked to enter the fullname and the second time the user is asked to enter a friends fullname. However, on the second call of gets() , it doesn't wait for the input from the user it just skips over it and finishes the program. Here is my complete code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main()
{
char fullname[30];
char friendsname[30];
char sentence[70]= "";
char gender;
printf("Enter your full name: ");
gets(fullname);
printf("\n");
printf("%s , Please enter your gender(m/f)? : ", fullname);
scanf("%c", &gender );
puts("\n");
if(gender =='m')
{
printf("Mr. %s , please enter your friends name:", fullname);
gets(friendsname);
puts("\n");
}
else if(gender =='f')
{
printf("Mrs. %s , please enter your friends name:", fullname);
gets(friendsname);
puts("\n");
}
strcat(sentence, "Hello Mr./Mrs. ");
strcat(sentence, friendsname );
strcat(sentence, ", " );
strcat(sentence, fullname);
strcat(sentence, " considered you as a friend. ");
puts(sentence);
return 0;
}
Here is a sample output:
Enter your full name: Brad Pitt
Brad Pitt , Please enter your gender(m/f)? : m
Mr. Brad Pitt , please enter your friends name:
Hello Mr./Mrs. , Brad Pitt considered you as a friend.
Process returned 0 (0x0) execution time : 8.110 s Press any key to continue.
The gets(friendsname); line is completely being skipped and the program continues on for some reason. Can anyone explain why this is happening ?