0

I'm building an Address Book to store an individuals name, address and phone number. I'm having trouble with the append function.

struct node{

char name[21];
char address[101];
char phone[15];

struct node *next;

};

void append(){

if(root == NULL){   // Empty list

    struct node *temp = (struct node*) malloc(sizeof(struct node)); 

    printf("Enter Name: ");
    fgets(temp->name,20,stdin);

    printf("Enter Address: ");
    fgets(temp->address,100,stdin);

    printf("Enter Phone Number: ");
    scanf("%s",&temp->phone);
}

}

For the name and address I except the user to enter a string with whitespaces so I decided to use fgets(). However, I'm not sure how to store the name or address the user enters into the node. When I run my program fgets() is not called for the name, instead it jumps to the address and then asks the user to enter a phone number.

aDabOfRanch
  • 137
  • 9
  • You can specify the full size to `fgets()`; by specifying 20 instead of 21, you are potentially wasting the last byte of `temp->name`, for example. Better, use `sizeof(temp->name)` instead of either 20 or 21. You still have to worry about what happens if some brute types 'Caractacus Sophocles MacWhorter' as their name. Maybe you should have a big buffer (`char buffer[4096];` for example) and use `if (fgets(buffer, sizeof(buffer), stdin) == 0) { …handle EOF or error… }` and then drop the newline (`buffer[strcspn(buffer, "\n")] = '\0';` and then decide whether the string is short enough to fit. – Jonathan Leffler May 22 '17 at 05:44
  • 1
    Also, mixing `fgets()` twice with `scanf()` once is a recipe for trouble. The `scanf()` leaves the newline in the input — so the next time you call the function, the newline is read by the first `fgets()`, so the next entry has an empty name. That isn't what you want. Use `fgets()` consistently. – Jonathan Leffler May 22 '17 at 05:45
  • __flush__ the input buffer after getting details of a user. As Jonathan has pointed out, `new line` character in the input buffer might cause a problem. – Jithin Pavithran May 22 '17 at 09:58
  • @JithinPavithran Yeah I got rid of the new line character but it still doesn't fix the problem. When I run the program the fgets() to input a name is not called, it skips to prompting the user to enter an address. What's weird is that when I comment out the code to print "Enter name" and fgets() for name and run the program it skips the fgets() for address and prompts the user to enter a phone number. – aDabOfRanch May 22 '17 at 13:30
  • Add `fflush(stdin)` just before 1st input statement and see if the problem is solved. – Jithin Pavithran May 23 '17 at 03:46

3 Answers3

0

I think that you should use scanf instead of fgets.

fgets

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

Yunbin Liu
  • 1,484
  • 2
  • 11
  • 20
0

One of the problems that might be occurring is that you have a loose '\n' (newline) character and as you may already know fgets stops as soon as it reaches an EOF or newline character. In order to capture any loose newline characters add:

while(getchar() != '\n');

The previous statement should come right before that first fgets statement. This frees up stdin for any user input.

Fern
  • 33
  • 6
0

You should be consistent and i would suggest to use scanf here, even though fgets is a better choice, while reading from the input stream.

Or, if you are so much in love with fgets(), handle newline condition from your end. If you want to keep it simple, yet usabele, I would suggest:

printf("Enter Name: ");
scanf(temp->name,20,stdin);

printf("Enter Address: ");
scanf(temp->address,100,stdin);

printf("Enter Phone Number: ");
scanf("%s",&temp->phone);

Also you will benefit largely, if you would just look at the discussion here: gets() vs scanf() vs fgets()

Community
  • 1
  • 1