Hi guys, so a while back I posted about a Library System I was implementing. I decided to revisit the project, but instead of using files to store all of the data I wanted to implement a linked list as they are a part of C that I wish to get better at. However, I'm having problems. I can't see any particular errors in my code. Here is the librarian.c file in which the problem occurs
Code:
//CHANGE librarian main
int librarian() {
//Variable declarations
STUDENT_DATA * stu_head_member = NULL, * sp = NULL, * sp_next = NULL;
BOOK_DATA * book_head_member = NULL, * bp = NULL, * bp_next = NULL;
int stu_id = 0;
int book_id = 0;
int quantity = 0;
char name[30] = {};
char pass[20] = {};
char author[30] = {};
char category[30] = {};
char cmd[10] = {};
for (;;) {
puts("Welcome Librarian!");
puts("For a list of available commands and info on how to login, type 'help'.");
puts("Please type a command");
printf(">");
scanf("%s", cmd);
if (!strcmp(cmd, "logout")) {
puts("The Librarian has logged out.");
break;
} else if (!strcmp(cmd, "studreg")) {
printf("Please enter the students name to register: ");
scanf(" %[^\n]s", name);
printf("What is %s's Student ID?", name);
scanf("%d", & stu_id);
printf("What is %s's desired password?", name);
scanf("%s", pass);
printf("\n%s (Password: %s, Student ID: %d) is now a member of the Library\n\n", name, pass, stu_id);
add_student(name, pass, stu_id);
} else if (!strcmp(cmd, "studsearch")) {
int count = 0;
if (stu_head_member == NULL) {
printf("There are currently no students signed up to the Library.\n\n");
} else {
// search_student();
}
} else if (!strcmp(cmd, "studremove")) {
int count = 0;
if (stu_head_member == NULL) {
printf("There are currently no students signed up to the Library.\n\n");
} else {
printf("\nEnter the students name to remove: ");
scanf("%s", name);
if (remove_student(name)) {
printf("\n%s's account has been removed from the Library.\n\n", name);
}
}
} else if (!strcmp(cmd, "studlist")) {
int count = 0;
if (stu_head_member == NULL) {
printf("There are currently no students signed up to the Library.\n\n");
} else {
printf("\n");
for (sp = stu_head_member; sp; sp = sp->next) {
printf("Name\tPassword\tID\n");
printf("---------------\n");
printf("[%d] %s, %s, %d\n", ++count, sp->name, sp->pass, sp->stu_id);
}
printf("\n");
}
} else if (!strcmp(cmd, "bookreg")) {
printf("Please enter the books name to register: ");
scanf("%s\n", name);
printf("What is %s's ID number? ", name);
scanf("%d", & book_id);
printf("Who is %s's author? ", name);
scanf("%s", author);
printf("What is %s's category? ", name);
scanf("%s", category);
printf("How many copies of %s are to be added to the Library? ");
scanf("%d", & quantity);
printf("\n%s (ID Number: %d, Author: %s, Category: %s, Quantity: %d) has been added to the Library\n\n", name, book_id, author, category, quantity);
add_book(book_id, name, author, category, quantity);
} else if (!strcmp(cmd, "booksearch")) {
int count = 0;
if (book_head_member == NULL) {
printf("There are currently no books in the Library.\n\n");
} else {
// search_books();
}
} else if (!strcmp(cmd, "bookremove")) {
int count = 0;
if (book_head_member == NULL) {
printf("There are currently no books in the Library.\n\n");
} else {
printf("\nEnter the name of the book to remove: ");
scanf("%s\n", name);
if (remove_book(name)) {
printf("\nThe book '%s' has been removed from the Library.\n\n", name);
}
}
} else if (!strcmp(cmd, "booklist")) {
int count = 0;
if (book_head_member == NULL) {
printf("There are currently no books in the Library.\n\n");
} else {
printf("\n");
for (bp = book_head_member; bp; bp = bp - > next) {
printf("Name\tAuthor\tCategory\tQuantity\tID\n");
printf("-------------------------------------\n");
printf("[%d] %s, %s, %s, %d, %d\n", ++count, bp->name, bp->author, bp->category, bp->quantity, bp->book_id);
}
printf("\n");
}
} else if (!strcmp(cmd, "help")) {
help_lib();
} else {
printf("\n%s is not a valid command. \n\n", cmd);
continue;
}
}
for (sp = stu_head_member; sp; sp = sp_next) {
sp_next = sp->next;
remove_student(sp->name);
}
for (bp = book_head_member; bp; bp = bp_next) {
bp_next = bp->next;
remove_book(bp->name);
}
return 0;
}
//CHANGE librarian login check
void lib_login() {
char password[10] = "password";
char temp_pass[15] = {};
for (;;) {
puts("Librarian Login");
puts("Hello Miss Librarian. Please enter your pass code:");
scanf("%s", temp_pass);
if (!strcmp(password, temp_pass)) {
puts("Password matched!");
librarian();
break;
} else {
puts("Password did not match. Please try again");
}
}
}
//CHANGE add_student to linked list
void add_student(char * name, char * pass, int id) {
STUDENT_DATA * stu_head_member = NULL, * newNode = NULL, * sp = NULL;
if ((newNode = stu_new_node()) == NULL)
return;
newNode->name = strdup(name);
newNode->pass = strdup(pass);
newNode->stu_id = id;
if (stu_head_member == NULL) {
stu_head_member = newNode; /*Clion is telling my that the head_member value is never used on this line*/
} else {
//Add node to end of list
for (sp = head_member; sp; sp = sp->next) {
if (sp->next == NULL) {
sp->next = newNode;
break;
}
}
}
}
//CHANGE remove_student from linked list
bool remove_student(char * name) {
STUDENT_DATA * stu_head_member = NULL, * sp = NULL, * temp = NULL;
if (stu_head_member == NULL) {
printf("\nThere are no students to remove.\n\n");
return false;
}
if (!strcmp(stu_head_member->name, name)) {
temp = stu_head_member;
stu_head_member = stu_head_member->next;
free(temp->name);
free(temp->pass);
free(temp);
temp = NULL;
return true;
}
for (sp = stu_head_member; sp; sp = sp->next) {
if (sp->next != NULL) {
if (!strcmp(sp->next->name, name)) {
temp = sp->next;
sp->next = sp-> next->next;
free(temp->name);
free(temp->pass);
free(temp);
temp = NULL;
return true;
}
}
}
printf("\nThere is no student named %s. \n\n", name);
return false;
}
//CHANGE add_book to linked list
void add_book(int book_id, char * name, char * author, char * category, int quantity) {
BOOK_DATA * book_head_member = NULL, * newNode = NULL, * bp = NULL;
if ((newNode = book_new_node()) == NULL)
return;
newNode->book_id = book_id;
newNode->name = strdup(name);
newNode->author = strdup(author);
newNode->category = strdup(category);
newNode->quantity = quantity;
if (book_head_member == NULL) {
book_head_member = newNode;
} else {
//Add node to end of list
for (bp = book_head_member; bp; bp = bp->next) {
if (bp->next == NULL) {
bp->next = newNode;
break;
}
}
}
}
//CHANGE remove_book from linked list
bool remove_book(char * name) {
BOOK_DATA * book_head_member = NULL, * bp = NULL, * temp = NULL;
if (book_head_member == NULL) {
printf("\nThere are no books to remove.\n\n");
return false;
}
if (!strcmp(book_head_member->name, name)) {
temp = book_head_member;
book_head_member = book_head_member->next;
free(temp->name);
free(temp->author);
free(temp->category);
free(temp);
temp = NULL;
return true;
}
for (bp = book_head_member; bp; bp = bp->next) {
if (bp->next != NULL) {
if (!strcmp(bp->next->name, name)) {
temp = bp->next;
bp->next = bp->next->next;
free(temp->name);
free(temp->author);
free(temp->category);
free(temp);
temp = NULL;
return true;
}
}
}
printf("\nThere is no book named %s. \n\n", name);
return false;
}
//help function
void help_lib() {
puts("Librarian Help Menu");
puts("Student commands:");
puts("To register a student, type 'studreg'.");
puts("To search for a student, type 'studsearch'.");
puts("To remove a student record, type 'studremove'.");
puts("To view a list of all students, type 'studlist'.");
puts("");
puts("Book commands");
puts("To add a book, type 'bookreg'.");
puts("To search for a book, type 'booksearch'.");
puts("To remove a book record, type 'bookremove'.");
puts("To view a list of all books, type 'booklist'.");
puts("");
puts("If you wish to logout, type 'logout'.");
puts("To go back, press any key");
getchar();
}
You can see that if the librarian types 'studreg', it asks for the name, ID, and password of the student, which is then passed to the
Code:
add_student(char * name, char * pass, int id)
function. This is the function which should add the information to the linked list. However, as you can see in my comments, Clion is telling me that the 'stu_head_member' value is never used (line 161).
I presume this is why it's not working, but I have no idea why that value is never used. Can anybody spot something I'm missing as to why it's not working?
This is also my LinkedList.c file:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
#include "structs.h"
STUDENT_DATA *stu_new_node() {
STUDENT_DATA *node = NULL, node_zero = {0};
if ((node = (STUDENT_DATA *)malloc(sizeof(STUDENT_DATA))) == NULL) {
// Error message here
return NULL;
}
*node = node_zero;
return node;
}
BOOK_DATA *book_new_node() {
BOOK_DATA *node = NULL, node_zero = {0};
if ((node = (BOOK_DATA *)malloc(sizeof(BOOK_DATA))) == NULL) {
// Error message here
return NULL;
}
*node = node_zero;
return node;
}
Many thanks
edit:
I moved the declaration for 'STUDENT_DATA * stu_head_member = NULL' outside of the add_student function as I thought this may be my problem. Clion no longer tells me that stu_head_member is never used so I thought this would solve my problem. However, when I went to compile it's given me several errors, as follows:
Code:
"C:\Program Files\JetBrains\CLion 2018.1.2\bin\cmake\bin\cmake.exe" --build C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\cmake-build-debug --target all -- -j 4
Scanning dependencies of target CW2_CompleteRework
[ 50%] Building C object CMakeFiles/CW2_CompleteRework.dir/main.c.obj
[ 50%] Building C object CMakeFiles/CW2_CompleteRework.dir/librarian.c.obj
In file included from C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\linkedlist.h:2:0,
from C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\main.c:7:
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\structs.h: In function 'add_student':
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\structs.h:8:29: error: storage class specified for parameter 'STUDENT_DATA'
typedef struct student_data STUDENT_DATA;
^~~~~~~~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\structs.h:9:26: error: storage class specified for parameter 'BOOK_DATA'
typedef struct book_data BOOK_DATA;
^~~~~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\structs.h:16:5: error: expected specifier-qualifier-list before 'STUDENT_DATA'
STUDENT_DATA *next;
^~~~~~~~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\structs.h:12:1: warning: empty declaration
struct student_data {
^~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\structs.h:25:5: error: expected specifier-qualifier-list before 'BOOK_DATA'
BOOK_DATA *next;
^~~~~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\structs.h:19:1: warning: empty declaration
struct book_data {
^~~~~~
In file included from C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\main.c:7:0:
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\linkedlist.h:5:1: error: expected declaration specifiers before 'STUDENT_DATA'
STUDENT_DATA *stu_new_node();
^~~~~~~~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\linkedlist.h:6:1: error: expected declaration specifiers before 'BOOK_DATA'
BOOK_DATA *book_new_node();
^~~~~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\main.c:13:12: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
int main() {
^
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c: In function 'add_student':
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\main.c:52:12: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
void help(){
^
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\main.c:64:16: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
void dev_info(){
^
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:10:1: error: parameter 'stu_head_member' is initialized
STUDENT_DATA *stu_head_member = NULL;
^~~~~~~~~~~~
In file included from C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\main.c:6:0:
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.h:5:6: error: old-style parameter declarations in prototyped function definition
void add_student(char *name, char *pass, int id)
^~~~~~~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:11:1: error: parameter 'book_head_member' is initialized
BOOK_DATA *book_head_member = NULL;
^~~~~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\main.c:72:1: error: expected '{' at end of input
}
^
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:14:16: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
int librarian(){
^
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:143:18: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
void lib_login() {
^
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:162:50: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
void add_student(char *name, char *pass, int id) {
^
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:186:33: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
bool remove_student(char *name) {
^
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:220:84: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
void add_book(int book_id, char *name, char *author, char *category, int quantity) {
^
mingw32-make.exe[2]: *** [CMakeFiles\CW2_CompleteRework.dir\build.make:62: CMakeFiles/CW2_CompleteRework.dir/main.c.obj] Error 1
mingw32-make.exe[2]: *** Waiting for unfinished jobs....
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:246:30: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
bool remove_book(char *name) {
^
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:282:16: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
void help_lib(){
^
In file included from C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:7:0:
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.h:5:6: error: old-style parameter declarations in prototyped function definition
void add_student(char *name, char *pass, int id)
^~~~~~~~~~~
C:\Users\punished_snake\CLionProjects\CW2_CompleteRework\librarian.c:299:1: error: expected '{' at end of input
}
^
mingw32-make.exe[2]: *** [CMakeFiles\CW2_CompleteRework.dir\build.make:110: CMakeFiles/CW2_CompleteRework.dir/librarian.c.obj] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:67: CMakeFiles/CW2_CompleteRework.dir/all] Error 2
mingw32-make.exe: *** [Makefile:83: all] Error 2
editedit: So I saved the project, and reopened it. All of a sudden, no errors at all. And the linkedlist is now working. Why is Clion so retarded sometimes? Also if anybody has any pointers for me it would be greatly appreciated.