1

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 ?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
JonSnow
  • 67
  • 3
  • 14
  • 2
    The first piece of advice would be: don't use `gets`, use `fgets` instead. – Yu Hao Aug 31 '15 at 07:11
  • 1
    Read the newline explicitly using `getchar` following each `gets` – perreal Aug 31 '15 at 07:18
  • Welcome to SO. `gets` has been removed from the latest C standard, don't use it. Also please indent your code properly when asking, here. Makes it much easier for us to read. – Jens Gustedt Aug 31 '15 at 08:18
  • 1
    possible duplicate of [gets() taking input without actually giving it any input?](http://stackoverflow.com/questions/3739983/gets-taking-input-without-actually-giving-it-any-input) – melpomene Aug 31 '15 at 08:53
  • `gets` has been removed from the C language, so you shouldn't use it. – Lundin Aug 31 '15 at 11:09

4 Answers4

1

See Here I have edited your code.The change I have made is only that befor e the second gets (friends name) I have scanned a character(ch). What is happening in your code is that it is taking the return (Enter) you press after typing m/f for gender is taken as friends name as gets catches enter and is skipped. Now that enter you press after entering gender is caught in ch by the ch statement and the name you enter afterwards goes to the friends name.

#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;

char ch; //ch declared edit 1 

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

scanf("%c",&ch); // Second Edit line to catch the enter

gets(friendsname);
puts("\n");
}

else if(gender =='f')
{
printf("Mrs. %s , please enter your friends name:", fullname);

scanf("%c",&ch); // Third Edit line to catch the enter

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;


}
Anup
  • 2,384
  • 4
  • 15
  • 16
1

Answer:

I think it is because of a dangling newline after the call to scanf function.

I would consume the dangling newline with a getchar function like so:

scanf("%c", &gender), getchar();

Then your program should proceed as expected for the further input.

However, at the end your program is probably going to crash, because the sentence character array has only a length of 70. It is not long enough to accommodate correctly all the characters that you concatenate to it.

Increase it to 130 and you should be ok.

char sentence[130] = "";

Further tip:

I think it is not recommended to use gets nowadays; at least I see a warning from my compiler (gcc). If you can use fgets, you should do so. You could use it like so.

fgets(fullname, 30, stdin);
fullname[strlen(fullname) - 1] = '\0';

It reads the standard input up to a given length (more secure) and ends the string with the newline character. The second line removes the newline character.

Ely
  • 10,860
  • 4
  • 43
  • 64
1

NEVER NEVER NEVER NEVER use gets. It will introduce a point of failure/security hole in your code. It is no longer part of the standard library. Use fgets instead, just be aware that it will attempt to store the trailing newline to the target buffer if there's room.

The reason the gets(friendsname) is being skipped is that you have a trailing newline in the input stream after the scanf call to read gender; gets sees that newline before any other input and returns immediately.

One way around this is to have your scanf call consume the trailing newline without assigning it to anything:

scanf(" %c%*c", &gender );

The * in the second conversion specifier tells scanf to read a single character and discard it. Also, the leading blank in the format string tells scanf to skip over any leading whitespace and to read the first non-whitespace character.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Thank you John!, I read about the trailing new line that occurs with using gets() but was not sure on how to address it so thank you for clarifying that part. – JonSnow Sep 01 '15 at 06:57
0
  • Just add one line after scanf,as below

    printf("%s , Please enter your gender(m/f)? : ", fullname);
    scanf("%c",&gender );
    getc(stdin);
    puts("\n");
    
Sagar Patel
  • 864
  • 1
  • 11
  • 22