3

I am new to C and working through some exercises, but having trouble with gets() in a while loop. In searching, I believe that it may have something to do with the \n character, but I was hoping that someone would be able to give me a more thorough explanation of what is going on here:

This loop will only run once - it will print the 'Enter last name' to screen a second time and then drop out of the loop before gets() has a chance to take any input a second time:

while (employee_num <= 10)
{
    printf("Enter last name ");
    gets(employee[employee_num].last_name);
    if(strlen(employee[employee_num].last_name) == 0)
        break;
    printf("Enter first name ");
    gets(employee[employee_num].first_name);
    printf("Enter title ");
    gets(employee[employee_num].title);
    printf("Enter salary ");
    scanf("%d", &employee[employee_num].salary);        
    ++employee_num;
}

Thanks in advance!

6
  • What is employee_num set to originally?
    – Ryan M
    Jan 22, 2013 at 18:45
  • 3
    NEVER EVER use gets. This function is BROKEN beyound repair its own manpage tells you not to use it: Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead. Jan 22, 2013 at 18:45
  • employee_num is initially set to 0. I have just started to read about the problems with gets(), but I am still curious as to why this will not loop properly.
    – Gitarooman
    Jan 22, 2013 at 18:49
  • 1
    @user1929959 Using fflush on input streams is undefined behavior, so no, that would be a really bad idea. Two adjacent, dangerous functions don't cancel each other out...
    – Lundin
    Jan 22, 2013 at 19:16
  • 1
    And for the record, gets has been removed from the C language in the current standard. It is no longer valid C.
    – Lundin
    Jan 22, 2013 at 19:17

2 Answers 2

6

You'd have a newline character (\n) in the input buffer after reading the salary. That is being picked up as the last name in the second iteration. You can ignore that by adding a getchar() after your last scanf:

while (employee_num <= 10) {
    ...
    printf("Enter salary ");
    scanf("%d", &employee[employee_num].salary);        
    ++employee_num;
    getchar();
}
6
  • This worked. Thank you! When using fgets() in place of gets(), Im assuming this problem will not exist - is that correct?
    – Gitarooman
    Jan 22, 2013 at 18:58
  • 1
    Yes. You will have to get rid of the extra \n at the end of the input string, though. As for salary, you might want to fgets it into a string buffer, then use sscanf() (not scanf) on that buffer.
    – LSerni
    Jan 22, 2013 at 19:01
  • 1
    And in general replace the gets() with fgets! Jan 22, 2013 at 19:13
  • Would it be necessary to flush the string buffer being used each time before reading in more data or am I completely off base?
    – Gitarooman
    Jan 22, 2013 at 23:11
  • Didn't scanf() reject whatever input after "space" is entered? Jan 7, 2018 at 10:13
2

Referring to answer by skjaidev,

With gets(), The newline character(\n), if found, is not copied into string and this is the reason for your problem.

Also, Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).

It is considered to be a bad practise to use gets() in a program

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.