Ok, so i'm supposed to create this ADT structure and i'm having problems storing data into a function and printing it.

These are the three files I have:

#include "myBook.h"
#include <stdio.h>
#include <stdlib.h>

Book *createBook(char title, float price, int stock, char person)
{
    Book *myBook;
    myBook = (Book*) malloc(sizeof(Book));
    (*myBook).bookName = title;
    (*myBook).bookPrice = price;
    (*myBook).bookStock = stock;
    (*myBook).bookPerson = person;
    return myBook;
}

void destroyBook(Book *myBook)
{
    free (myBook);
}

void printBook(Book *myBook)
{
    printf (*myBook).bookName;
    printf (*myBook).bookPrice;
    printf (*myBook).bookStock;
    printf (*myBook).bookPerson;
}

int setName(Book *myBook, char title)
{
    (*myBook).bookName = title;
}

int retrieveName(Book *myBook)
{
    (*myBook).bookName;
}

int lendBook(Book *myBook, char person)
{
    (*myBook).bookPerson = person;
}

int bookReturn(Book *myBook, int stock)
{
    (*myBook).bookStock = stock;
}

int bookLentOut(Book *myBook)
{
    if (*myBook).bookStock = 0
        return FALSE;
    else
        return TRUE;
}

int bookSetPrice(Book *myBook, float price)
{
    (*myBook).price = price;
}

int bookRetrievePrice(Book *myBook)
{
    (*myBook).price;
}
#define TRUE 1
#define FALSE 0

typedef struct {
    char bookName;
    float bookPrice;
    int bookStock;
    char bookPerson;
} Book;

Book *createBook(char title, float price, int stock, char person);

void destroyBook(Book *myBook);

void printBook(Book *newBook);

int setName(Book *myBook, char title);

int retrieveName(Book *myBook);

int lendBook(Book *myBook, char person);

int bookReturn(Book *myBook, int stock);

int bookLentOut(Book *myBook);

int bookSetPrice(Book *myBook, float price);

int bookRetrievePrice(Book *myBook);
#include "myBook.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{

    FILE* pFile = NULL;
    char filename[80];
    char title[100];
    float price;
    int stock;
    char person[100];
  
    // asking the user to enter a filename, the file will read the conents
    printf("Please enter a filename for input: ");
    scanf("%s", &filename); 
  
    pFile = fopen(filename, "r");
    if (pFile == NULL)
    {
        printf ("Error opening file!\n");
        return -1;
    }
 
    while (!feof(pFile))
    {
        
        fgets(title, sizeof(title), pFile);
        fscanf(pFile, "%f", &price);
        fscanf(pFile, "%d", &stock);
        fgets(person, sizeof(person), pFile);
    
        fclose(pFile);
    }
    
    createBook(*title, price, stock, *person);
    printBook();
    destroyBook();
    
  
    getch();
    return 0;
}

This is the error i'm getting:

In function `main':
39 too few arguments to function `printBook'
40 too few arguments to function `destroyBook'

Can anyone help please?

  1. The file is closed in the loop. That does not work well if you want the loop to continue reading the file. ;)
  2. The file reading loop does not do anything else. Only the last book in the file will be printed.
  3. createBook returns a pointer to the new book, but the code does not save it anywhere.
  4. printBook and deleteBook both take a book pointer as arguments.

Ed would expect to see something more like this.

while (!feof(pFile))
{
    Book *book;

    fgets(title, sizeof(title), pFile);
    fscanf(pFile, "%f", &price);
    fscanf(pFile, "%d", &stock);
    fgets(person, sizeof(person), pFile);

    book = createBook(*title, price, stock, *person);
    printBook(book);
    destroyBook(book);
}

fclose(pFile);

There are other problems, but you should not overwhelm yourself, so Ed won't mention them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.