简体   繁体   中英

Unable to use fgets function to read a string

I'm trying to take in multiple inputs(strings,integers and doubles) and add them to a linked list i cant get fgets() function to work properly and there's no constructive error that would help me find a solution.

I ask the user to enter each detail separately then try add it to a Linked List. I realised that scanf doesn't work so tried to use fgets and it doesn't seem to work either.

void add(){ 
  struct LinearNode *aNode;
    struct book *anElement;

    char *idB,*nameB,*authorB;
    int yearB,loanedTimesB;
    double valueB;

  anElement = (struct book *)malloc(sizeof(struct book));
  if (anElement == NULL)
            printf("Error - no space for the new element\n");
        else{
      printf("Please enter the book ID.\n");
      fgets(anElement->id,sizeof anElement->id,stdin);
      //strcpy(anElement->id, idB);
      printf("Please enter the name of the book.\n");
      scanf("%s", anElement->name);
      printf("Please enter the name of the book author.\n");
      scanf("%s", anElement->author);
      printf("Please enter the year of publication.\n");
      scanf("%d", &anElement->year);
      printf("Please enter the initial book value.\n");
      scanf("%lf", &anElement->value);
      anElement->loanedTimes = 0;
      anElement->loaned = false;

      aNode = (struct LinearNode *)malloc(sizeof(struct LinearNode));
      if (aNode == NULL)
                printf("Error - no space for the new node\n");
          else { // add data part to the node
                aNode->element = anElement;
                aNode->next = NULL;
                //add node to end of the list
                if (isEmpty()) {
                   front = aNode;
                   last = aNode;
                }
                else {
                     last->next = aNode;
                     last = aNode;
                  } //end else
           }
    }
}

here is the full code

#define _CRT_SECURE_NO_WARNINGS
#define bool int
#define false 0
#define true (!false)

//Libraries
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
//Preprocessor Variable
#define SIZE 10

struct book{
  char *id,*name,*author;
  int year,loanedTimes;
  bool loaned;
  double value;
};

struct LinearNode {
    struct book *element;
    struct LinearNode *next;
};

struct LinearNode *front = NULL;
struct LinearNode *last = NULL;

void check(),menu(),add(),borrow(),returnBook(),deleteBook(),viewAll(),viewOne(),viewPopNo(),viewValue();
bool isEmpty();
int main(int argc, char *argv[]) {
    check();
  menu();
    return 0;
}

void check(){
  if( access( "book.dat", F_OK ) != -1 ) {
    printf("The system has been populated with books from the data file.\n");
} else {
    printf("Database of books doesn't exist.\nPlease populate it using the first function from the menu.\n");
}
}

void menu(){
    int x = 0;
    do{
    printf("1. Add a new book to the library \n");
    printf("2. Allow customer to take out a book \n");
    printf("3. Allow Customer to return a book \n");
    printf("4. Delete an old book from stock \n");
    printf("5. View all books \n");
    printf("6. View a specific book \n");
    printf("7. View details of most popular and least popular books \n");
    printf("8. Check value of the books \n");
    printf("9. Exit the system \n");
  scanf("%d",&x);
  getchar();
  if (x==1)
  add();
    }while(x!=9);
}
void add(){ 
  struct LinearNode *aNode;
    struct book *anElement;

    char *idB,*nameB,*authorB;
    int yearB,loanedTimesB;
    double valueB;

  anElement = (struct book *)malloc(sizeof(struct book));
  if (anElement == NULL)
            printf("Error - no space for the new element\n");
        else{
      printf("Please enter the book ID.\n");
      fgets(anElement->id,sizeof anElement->id,stdin);
      //strcpy(anElement->id, idB);
      printf("Please enter the name of the book.\n");
      scanf("%s", anElement->name);
      printf("Please enter the name of the book author.\n");
      scanf("%s", anElement->author);
      printf("Please enter the year of publication.\n");
      scanf("%d", &anElement->year);
      printf("Please enter the initial book value.\n");
      scanf("%lf", &anElement->value);
      anElement->loanedTimes = 0;
      anElement->loaned = false;

      aNode = (struct LinearNode *)malloc(sizeof(struct LinearNode));
      if (aNode == NULL)
                printf("Error - no space for the new node\n");
          else { // add data part to the node
                aNode->element = anElement;
                aNode->next = NULL;
                //add node to end of the list
                if (isEmpty()) {
                   front = aNode;
                   last = aNode;
                }
                else {
                     last->next = aNode;
                     last = aNode;
                  } //end else
           }
    }
}
bool isEmpty() {
    if (front == NULL)
        return true;
    else
        return false;
}




sizeof anElement->id yields the size of a pointer!

fgets(anElement->id,sizeof anElement->id,stdin);
//                  ^^^^^^^^^^^^^^^^^^^^
//                  size of a pointer
//                  probably 4 or 8

You should allocate enough space and read that many bytes

anElement->id = malloc(100);      // error checking omitted for brevity
fgets(anElement->id, 100, stdin); // error checking omitted for brevity
// use anElement->id
free(anElement->id);

Don't forget to free() the memory when you no longer need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM