Я пытаюсь получить несколько входных данных (строки, целые и двойные числа) и добавить их в связанный список. Я не могу получить функцию fgets()
для правильной работы, и нет конструктивной ошибки, которая помогла бы мне найти решение.
Я прошу пользователя ввести каждую деталь отдельно, а затем попробуйте добавить ее в связанный список.
Я понял, что scanf не работает, поэтому попытался использовать fgets и, похоже, тоже не работает.
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
}
}
}
вот полный код
#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;
}