1

I am attempting to create a function which reads the personal details of a person and pass the data to the calling function via a structure, then return the number of names returned.

#include <stdio.h>
#include <string.h>
#define MAX 100
int readin(Employee *emp);
int main()
{
  Employee emp[MAX];
  int size;
  size = readin(emp);
  return 0;
}
int readin(Employee *emp)
{
  char name[40], tel[40], *p;
  int id,size=0,i=0;
  double sal;
  printf("Enter name:\n");
  scanf("\n");
  fgets(name,40,stdin);
  if (p=strchr(name,'\n')) *p = '\0';
  if (strcmp("#",name) != 0) {
    while (strcmp("#",name) != 0) {
      strcpy(emp->name, name);
      printf("Enter tel:\n");
      fgets(tel,40,stdin);
      if (p=strchr(tel,'\n')) *p = '\0';
      strcpy(emp->telno, tel);
      printf("Enter id:\n");
      scanf("%d",id);
      emp->id = id;
      printf("Enter salary:\n");
      scanf("%f",sal);
      emp->salary = sal;
      size++;
      i++;
      printf("Enter name:\n");
      fgets(name,40,stdin);
    }
  }
  return size;
}

The structure Employee is defined as follows:

typedef struct {
  char name[40];
  char telno[40];
  int id;
  double salary;
} Employee;

However, when I execute the program, the first fgets for the name is ignored, and the loop does not stop even when I key in #, which is supposed to indicate termination. Any help would be appreciated.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kim Minseo
  • 81
  • 7
  • 1
    `while (strcmp("#",name) != 0) {` This will never yield true: fgets() retains the (CR)LF in the input string. – wildplasser Nov 14 '20 at 13:49
  • Maybe your environment uses `'\r'` for ? Also try `strchr(name, '\r')`... – pmg Nov 14 '20 at 14:05
  • 1
    The `scanf()` for salary leaves the newline in the input buffer, so the `fgets()` after it reads to the newline. Don't mix `fgets()` and `scanf()`; use `fgets()` to get the data and `sscanf()` to parse the lines that `fgets()` reads. – Jonathan Leffler Nov 14 '20 at 14:14

2 Answers2

1
if (p=strchr(name,'\n')) *p = '\0';

Here, you properly get rid of the newline at the end of your input. However, you only do it before the loop. Thus, when someone enters # past the initial input, the trailing newline won't get removed and it will never equal #. You should also trim it at the end of the loop:

// last 4 lines of loop
size++;
i++;
printf("Enter name:\n");
fgets(name,40,stdin);
if (p=strchr(name,'\n')) *p = '\0';
// loop ends

You also have some unnecessary things, like scanf("\n") and the if statement around the while loop (the while loop just won't run if the condition isn't met, no need for an extra if statement).

Aplet123
  • 33,825
  • 1
  • 29
  • 55
1

As wildplasser pointed out , \n will be added at the end of the name . There is an another way to get rid of \n from name

printf("Enter name:\n");
fgets(name,40,stdin);
name[strcspn(name, "\n")] = '\0'; 

Please refer this link :https://www.geeksforgeeks.org/strcspn-in-c/#:~:text=The%20C%20library%20function%20strcspn,search%20has%20to%20be%20made.

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26