Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I don\'t understand why my display function isn\'t working. Even my count functi

ID: 3752991 • Letter: I

Question

I don't understand why my display function isn't working. Even my count function isn't counting the inputs from the add function. Please help me on how to do this. Thanks a lot.

// READ BEFORE YOU START:

// You are given a partially completed program that creates a list of books, say for library database.

// Each book has the this information: book name, publication year, library name, number of copies of that book.

// The struct 'bookDetails' holds information of one book. Note that library name is enum type.

// An array of structs called 'list' is made to hold a list of books.

// To begin, you should trace through the given code and understand how it works.

// Please read the instructions above each required function and follow the directions carefully.

// If you modify any of the given code, the return types, or the parameters, you risk getting compile error

// because you are not allowed to modify main ().

// You can use string library functions.

// WRITE COMMENTS FOR IMPORANT STEPS OF YOUR CODE.

#include asdf

#include

#include

#include // needed to use tolower()

#pragma warning(disable: 4996) // for Visual Studio Only

#define MAX_BOOKS 20

#define MAX_NAME_LENGTH 35

typedef enum { noble = 0, hayden } library; // enumeration type library

struct bookDetails {

char bookName[MAX_NAME_LENGTH];

int pubYear; // publication year

library libName;

int noOfCopies;

};

struct bookDetails list[MAX_BOOKS]; // declare list of books

int count = 0; // the number of books currently stored in the list (initialized at 0)

// forward declaration of functions (already implmented)

void flushStdIn();

void executeAction(char);

void save(char* fileName);

// functions that need implementation:

int add(char* bookName_input, int pubYear_input, char* libName_input, int noOfCopies_input); // 20 points

void display(); // 10 points

void sort(); // 10 points

void load(char* fileName); // 10 points

int main()

{

load("Book_List.txt"); // load list of books from file (if it exists). Initially there will be no file.

char choice = 'i'; // initialized to a dummy value

do

{

printf(" CSE240 HW5 ");

printf("Please enter your selection: ");

printf(" a: add a new book to the list ");

printf(" d: display entire list of books ");

printf(" s: sort books by name ");

printf(" q: quit ");

choice = tolower(getchar());

flushStdIn();

executeAction(choice);

} while (choice != 'q');

save("Book_List.txt"); // save list of books to file (overwrites file, if it exists)

return 0;

}

// flush out leftover ' ' characters

void flushStdIn()

{

char c;

do c = getchar();

while (c != ' ' && c != EOF);

}

// ask for details from user for the given selection and perform that action

void executeAction(char c)

{

char bookName_input[MAX_NAME_LENGTH];

unsigned int pubYear_input, noOfCopies_input, add_result= 0;

char libName_input[10];

switch (c)

{

case 'a':

// input book details from user

printf(" Please enter book name: ");

fgets(bookName_input, sizeof(bookName_input), stdin);

bookName_input[strlen(bookName_input) - 1] = ''; // discard the trailing ' ' char

printf("Please enter publication year: ");

scanf("%d", &pubYear_input);

flushStdIn();

printf("Please enter library name (noble/hayden): ");

fgets(libName_input, sizeof(libName_input), stdin);

libName_input[strlen(libName_input) - 1] = ''; // discard the trailing ' ' char

printf("Please enter no. of copies of book: ");

scanf("%d", &noOfCopies_input);

flushStdIn();

// add the book to the list

add_result = add(bookName_input, pubYear_input, libName_input, noOfCopies_input);

if (add_result == 0)

printf(" That book is already on the list! ");

else

printf(" Book successfully added to the list! ");

break;

case 's': sort(); break;

case 'd': display(); break;

case 'q': break;

default: printf("%c is invalid input! ",c);

}

}

// Q1 : add (20 points)

// This function is used to insert a new book into the list. You can simply insert the new book to the end of list (array of structs).

// Do not allow the book to be added to the list if it already exists in the list. You can do that by checking the names of the books already in the list.

// If the book already exists then return 0 without adding it to the list. If the book does not exist in the list then go on to add the book at the end of the list and return 1.

// NOTE: You must convert the string 'libName_input' to an enum type and store it in the list because the struct has enum type for library name.

// You can assume that user will input library name in lowercase.

// Hint: 'count' holds the number of books currently in the list

int add(char* bookName_input, int pubYear_input, char* libName_input, int noOfCopies_input)

{

if (bookName_input < MAX_BOOKS)

return 0;

else

return 1;

struct bookDetails newAssign;

strcpy(newAssign.bookName, bookName_input);

newAssign.pubYear = pubYear_input;

newAssign.noOfCopies = noOfCopies_input;

if (strcmp(libName_input, "noble" == 0)) {

newAssign.libName = noble;

}

else {

newAssign.libName = hayden;

}

list[count] = newAssign;

count++;

//return 0; // Comment out this line when implementing this function

// It is added initially to avoid compile error

}

// Q2 : display (10 points)

// This function displays the list of books with the book details (struct elements) of each book.

// Parse through the book list and print the book details one after the other. See expected output screenshots in homework question file.

// NOTE: Library name is stored in the struct as enum type. You need to display 'Noble' or 'Hayden' as library name for the book.

void display()

{

for (int i = 0; i <= count; i++) {

printf(" Book name: %s",list[i].bookName);

printf(" Publication year: %d", list[i].pubYear);

printf(" Library: %s", list[i].libName);

printf(" No. of copies: %d", list[i].noOfCopies);

}

}

Explanation / Answer

The add function was not adding the book but was returning from function before that (the if-else in beginning itself). I fixed the code for add() and it should work fine now. display() is perfect. Please check and let me know if you still are facing some issues. Please do rate it if was helpful. thank you


int add(char* bookName_input, int pubYear_input, char* libName_input, int noOfCopies_input)
{

if (count < MAX_BOOKS)
{
//check if book already in list
for(int i = 0; i < count; i++)
{
if(strcmp(list[i].bookName, bookName_input) == 0) //already exists!
return 0;
}
}
else
return 0; //array full
  

struct bookDetails newAssign;
strcpy(newAssign.bookName, bookName_input);
newAssign.pubYear = pubYear_input;
newAssign.noOfCopies = noOfCopies_input;
if (strcmp(libName_input, "noble" == 0)) {
newAssign.libName = noble;
}
else {
newAssign.libName = hayden;
}

list[count] = newAssign;
count++;
}