Problems with passing an array of structures by pointer

This is a discussion on Problems with passing an array of structures by pointer within the C Programming forums, part of the General Programming Boards category; Hello everyone. I have looked through other posts and have not found a solution to my problem but if there ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    12

    Problems with passing an array of structures by pointer

    Hello everyone.

    I have looked through other posts and have not found a solution to my problem but if there is another thread with the same problem please reply with the URL.

    This is a first time post for me. I am having trouble with the function Additem which should take a pointer to an array called 'inventory', of structures called 'vbook's then modify the the structure in a certain coordinate within the array. I cannot tell what I am doing wrong but I think it has something to do with the way that I am passing the array or in the prototype and definition of the structure.

    I have eliminated a lot of code that has nothing to do with my problem (I think). Any help would be appreciated as this has been driving me crazy for the last 3 days.

    Thank you.

    Let me know if there is any other information you need.

    Code:
    #define INV_SIZE 20
    #define SIZE 30
    #define INITIAL_SIZE 5
    #include <stdio.h>
    #include <string.h>
    typedef struct
    {
        char book_name[SIZE];
        char publisher[SIZE];
        int in_stock;
        int isbn;
        double cost;
        double srp;
    }vbook;
    
    void Initialize(vbook *inventory[], int *inv_size);
    int Menu();
    void AddNewItem(vbook *inventory[], int *inv_size);
    
    
    int main()
    {
       vbook *inventory[INV_SIZE];
       int selection = 0;
       int *inv_size;
       int exit = 0;
       *inv_size = 5;
       FILE *fp;
        fp = fopen("report.txt", "w");
        
       do{
            selection = Menu();
       
            switch (selection)
                {
                    case 1:
                        AddNewItem(inventory, inv_size);
                        break;
           
                }
       }while(exit != -1);
       return(0);
    }
    
    
    int Menu()
    {
        int selection;
        
        printf("Enter 1 to add a new item to inventory.\n");
        printf("Enter 2 to delete an item from inventory.\n");
        printf("Enter 3 to display all inventory records.\n");
        printf("Enter 4 to create a current inventory report.\n");
        printf("Enter 5 to clear entire inventory.\n");
        printf("Enter 6 to Exit.\n");
        
        scanf("%d", &selection);
        return(selection);
          
    }
    
    void AddNewItem(vbook *inventory[], int *inv_size)
    {
        int i = 0;
        int exit = 0;
        
        do{   
            if(i > INV_SIZE)
            {
                printf("Your inventory size has surpassed available memory.\n");
                printf("Please delete some items from inventory or increase inventory size.\n");
                exit = -1;
            }
            if(inventory[i].isbn == -1 && exit == 0)
            {
                
                printf("Please enter the name of the book.\n");
                scanf("%s", inventory[i]->book_name);
                
                printf("Please enter the publisher of the book.\n");
                scanf("%s", inventory[i]->publisher);
                
                printf("Please enter the number of books in stock.\n");
                scanf("%d", &inventory[i]->in_stock);
                
                printf("Please enter the ISBN number of the book.\n");
                scanf("%d", &inventory[i]->isbn);
                
                printf("Please enter the suppliers cost for one book.\n");
                scanf("%lf", &inventory[i]->cost);
                
                printf("Please enter the retail price for the book.\n");
                scanf("%lf", &inventory[i]->srp);
                
                exit = -1;
                *inv_size = (*inv_size + 1);
            }
            else
                i++;
            
        }while(exit == 0 && i < INV_SIZE);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do not have an array of vbook's anywhere in your code. Did you intend to? If so, you should probably add one, or perhaps change inventory from an array of pointers-to-vbooks into an array of vbooks.

    Note also that if you have a function prototype like so:
    Code:
    void Initialize(vbook inventory[], int *inv_size);
    that function takes a array of vbooks, as though it were a pointer, and a pointer to an int.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    12

    Re: Problem Solution

    Thank you for your help. My program is compiling and running but I'm getting a really strange run time error.

    If you look at the Initialize function, i think that i'm initializing the first 6 addresses of the inventory array and yet from within the program if you select '3' to display the array to standard output for some reason the ISBN variable in the 6th element of the inventory array has become equal to the value of *inv_size.

    I know I probably have some little mistake somewhere thats causing it but I cant seem to find it.

    Thanks again for any help.

    Code:
    #define INV_SIZE 20
    #define SIZE 30
    #define INITIAL_SIZE 5
    #include <stdio.h>
    #include <string.h>
    typedef struct
    {
        char book_name[SIZE];
        char publisher[SIZE];
        int in_stock;
        int isbn;
        double cost;
        double srp;
    }vbook;
    
    void Initialize(vbook inventory[]);
    int Menu();
    void AddNewItem(vbook inventory[], int *inv_size);
    void DeleteItem(vbook inventory[], int *inv_size);
    void DisplayRecords(vbook inventory[], int *inv_size);
    void InventoryReport(vbook inventory[], int *inv_size, FILE *fp);
    void ClearInventory(int *inv_size);
    int Exit();
    
    int main()
    {
        vbook inventory[INV_SIZE] = {0};
       int selection = 0;
       int *inv_size;
       int exit = 0;
       *inv_size = 6;
       FILE *fp;
        fp = fopen("report.txt", "w");
        
       Initialize(inventory);//Initializes the first five sections of the inventory array
       *inv_size = 6;
       do{
            selection = Menu();
       
            switch (selection)
                {
                    case 1:
                        AddNewItem(inventory, inv_size);
                        break;
           
                    case 2:
                        DeleteItem(inventory, inv_size);
                        break;
               
                    case 3:
                        DisplayRecords(inventory, inv_size);
                        break;
                    case 4:
                        InventoryReport(inventory, inv_size, fp);
                        break;
                    case 5:
                        ClearInventory(inv_size);
                        break;
                    case 6:
                        exit = Exit();
                        break;
                }
       }while(exit != -1);
       return(0);
    }
    
    void Initialize(vbook inventory[])
    {
        int i;
        
        strcpy(inventory[0].book_name, "Twilight");
        strcpy(inventory[0].publisher, "Little_Readers");
        inventory[0].isbn = 000;
        inventory[0].in_stock = 99;
        inventory[0].cost = 4.20;
        inventory[0].srp = 19.99;
        
        strcpy(inventory[1].book_name, "New_Moon");
        strcpy(inventory[1].publisher, "Little_Readers");
        inventory[1].isbn = 111;
        inventory[1].in_stock = 16;
        inventory[1].cost = 5.30;
        inventory[1].srp = 24.99;
        
        strcpy(inventory[2].book_name, "Eclipse");
        strcpy(inventory[2].publisher, "Little_Readers");
        inventory[2].isbn = 222;
        inventory[2].in_stock = 20;
        inventory[2].cost = 6.20;
        inventory[2].srp = 26.99;
        
        strcpy(inventory[3].book_name, "Brave_New_World");
        strcpy(inventory[3].publisher, "Penguin_Classics");
        inventory[3].isbn = 333;
        inventory[3].in_stock = 43;
        inventory[3].cost = 13.28;
        inventory[3].srp = 35.99;
        
        strcpy(inventory[4].book_name, "Erotix");
        strcpy(inventory[4].publisher, "Adam_and_Eve");
        inventory[4].isbn = 444;
        inventory[4].in_stock = 13;
        inventory[4].cost = 1.99;
        inventory[4].srp = 10.99;
        
        strcpy(inventory[5].book_name, "Harry_Potter");
        strcpy(inventory[5].publisher, "Wizard");
        inventory[5].isbn = 5555;
        inventory[5].in_stock = 17;
        inventory[5].cost = 1.99;
        inventory[5].srp = 10.99;
        
        for(i = 6;i <= INV_SIZE;i++)
        {
            inventory[i].isbn = -1;
        }
        
    }
    
    int Menu()
    {
        int selection;
        
        printf("Enter 1 to add a new item to inventory.\n");
        printf("Enter 2 to delete an item from inventory.\n");
        printf("Enter 3 to display all inventory records.\n");
        printf("Enter 4 to create a current inventory report.\n");
        printf("Enter 5 to clear entire inventory.\n");
        printf("Enter 6 to Exit.\n");
        
        scanf("%d", &selection);
        return(selection);
          
    }
    
    void AddNewItem(vbook inventory[], int *inv_size)
    {
        int i = 0;
        int exit = 0;
        
        do{   
            if(i > INV_SIZE)
            {
                printf("Your inventory size has surpassed available memory.\n");
                printf("Please delete some items from inventory or increase inventory size.\n");
                exit = -1;
            }
            if(inventory[i].isbn == -1 && exit == 0)
            {
                
                printf("Please enter the name of the book.\n");
                scanf("%s", inventory[i].book_name);
                
                printf("Please enter the publisher of the book.\n");
                scanf("%s", inventory[i].publisher);
                
                printf("Please enter the number of books in stock.\n");
                scanf("%d", &inventory[i].in_stock);
                
                printf("Please enter the ISBN number of the book.\n");
                scanf("%d", &inventory[i].isbn);
                
                printf("Please enter the suppliers cost for one book.\n");
                scanf("%lf", &inventory[i].cost);
                
                printf("Please enter the retail price for the book.\n");
                scanf("%lf", &inventory[i].srp);
                
                exit = -1;
                printf("i is %d", i);
                printf("inv size is %d", *inv_size);
                *inv_size = (*inv_size + 1);
                printf("inv size is %d", *inv_size);
            }
            else
                i++;
            
        }while(exit == 0 && i < INV_SIZE);
    }
    
    void DeleteItem(vbook inventory[], int *inv_size)
    {
        char temp_title[SIZE];
        int temp_isbn;
        int i;
        
        printf("Please enter the name of the book you would like to delete.\n");
        scanf("%s", temp_title);
        
        printf("Please enter the ISBN number of the item you want to delete.\n");
        scanf("%d", temp_isbn);
        
        for(i = 0; i < *inv_size; i++)
        {
            if(temp_isbn == inventory[i].isbn)
            {
                inventory[i] = inventory[*(inv_size-1)];
            }
            else
                printf("That item could not be found in the inventory.\n");
        }
    }
    
    void DisplayRecords(vbook inventory[], int *inv_size)
    {
        int i;
        for(i = 0; i < *inv_size; i++)
        {
            printf("Book Name: %s\n", inventory[i].book_name);
            printf("Publisher Name: % s\n", inventory[i].publisher);
            printf("ISBN: %d\n", inventory[i].isbn);
            printf("Number of items in stock: %d\n", inventory[i].in_stock);
            printf("Supplier Price: %.2f\n", inventory[i].cost);
            printf("Suggested Retail Price: %.2f\n\n", inventory[i].srp);
        }
    }
    
    void InventoryReport(vbook inventory[], int *inv_size, FILE *fp)
    {
        int i;
        for(i = 0; i < *inv_size; i++)
        {
           fprintf(fp,"Book Name: %s\n", inventory[i].book_name);
           fprintf(fp,"Publisher Name: % s\n", inventory[i].publisher);
           fprintf(fp,"ISBN: %d\n", inventory[i].isbn);
           fprintf(fp,"Number of items in stock: %d\n", inventory[i].in_stock);
           fprintf(fp,"Supplier Price: %.2f\n", inventory[i].cost);
           fprintf(fp,"Suggested Retail Price: %.2f\n\n", inventory[i].srp);
        }
    }
    
    void ClearInventory(int *inv_size)
    {
        *inv_size = 0;
    }
    
    int Exit()
    {
        return(-1);
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    See the changes marked in red; surprised that it produces output and does not crash at runtime.
    Code:
    #define INV_SIZE 20
    #define SIZE 30
    #define INITIAL_SIZE 5
    #include <stdio.h>
    #include <string.h>
    typedef struct
    {
        char book_name[SIZE];
        char publisher[SIZE];
        int in_stock;
        int isbn;
        double cost;
        double srp;
    }vbook;
    
    void Initialize(vbook inventory[]);
    int Menu();
    void AddNewItem(vbook inventory[], int *inv_size);
    void DeleteItem(vbook inventory[], int *inv_size);
    void DisplayRecords(vbook inventory[], int *inv_size);
    void InventoryReport(vbook inventory[], int *inv_size, FILE *fp);
    void ClearInventory(int *inv_size);
    int Exit();
    
    int main()
    {
        vbook inventory[INV_SIZE] = {0};
       int selection = 0;
       int *inv_size;     
       int exit = 0;
       *inv_size = 6;  /* cannot do this without first making inv_size point to an int object */
       FILE *fp;
        fp = fopen("report.txt", "w");
        
       Initialize(inventory);//Initializes the first five sections of the inventory array
       *inv_size = 6;  /* same here */
       do{
            selection = Menu();
       
            switch (selection)
                {
                    case 1:
                        AddNewItem(inventory, inv_size);
                        break;
           
                    case 2:
                        DeleteItem(inventory, inv_size);
                        break;
               
                    case 3:
                        DisplayRecords(inventory, inv_size);
                        break;
                    case 4:
                        InventoryReport(inventory, inv_size, fp);
                        break;
                    case 5:
                        ClearInventory(inv_size);
                        break;
                    case 6:
                        exit = Exit();
                        break;
                }
       }while(exit != -1);
       return(0);
    }
    
    void Initialize(vbook inventory[])
    {
        int i;
        
        strcpy(inventory[0].book_name, "Twilight");
        strcpy(inventory[0].publisher, "Little_Readers");
        inventory[0].isbn = 000;
        inventory[0].in_stock = 99;
        inventory[0].cost = 4.20;
        inventory[0].srp = 19.99;
        
        strcpy(inventory[1].book_name, "New_Moon");
        strcpy(inventory[1].publisher, "Little_Readers");
        inventory[1].isbn = 111;
        inventory[1].in_stock = 16;
        inventory[1].cost = 5.30;
        inventory[1].srp = 24.99;
        
        strcpy(inventory[2].book_name, "Eclipse");
        strcpy(inventory[2].publisher, "Little_Readers");
        inventory[2].isbn = 222;
        inventory[2].in_stock = 20;
        inventory[2].cost = 6.20;
        inventory[2].srp = 26.99;
        
        strcpy(inventory[3].book_name, "Brave_New_World");
        strcpy(inventory[3].publisher, "Penguin_Classics");
        inventory[3].isbn = 333;
        inventory[3].in_stock = 43;
        inventory[3].cost = 13.28;
        inventory[3].srp = 35.99;
        
        strcpy(inventory[4].book_name, "Erotix");
        strcpy(inventory[4].publisher, "Adam_and_Eve");
        inventory[4].isbn = 444;
        inventory[4].in_stock = 13;
        inventory[4].cost = 1.99;
        inventory[4].srp = 10.99;
        
        strcpy(inventory[5].book_name, "Harry_Potter");
        strcpy(inventory[5].publisher, "Wizard");
        inventory[5].isbn = 5555;
        inventory[5].in_stock = 17;
        inventory[5].cost = 1.99;
        inventory[5].srp = 10.99;
        
        for(i = 6;i <= INV_SIZE;i++)
        {
            inventory[i].isbn = -1;
        }
        
    }
    
    int Menu()
    {
        int selection;
        
        printf("Enter 1 to add a new item to inventory.\n");
        printf("Enter 2 to delete an item from inventory.\n");
        printf("Enter 3 to display all inventory records.\n");
        printf("Enter 4 to create a current inventory report.\n");
        printf("Enter 5 to clear entire inventory.\n");
        printf("Enter 6 to Exit.\n");
        
        scanf("%d", &selection);
        return(selection);
          
    }
    
    void AddNewItem(vbook inventory[], int *inv_size)
    {
        int i = 0;
        int exit = 0;
        
        do{   
            if(i > INV_SIZE)
            {
                printf("Your inventory size has surpassed available memory.\n");
                printf("Please delete some items from inventory or increase inventory size.\n");
                exit = -1;
            }
            if(inventory[i].isbn == -1 && exit == 0)
            {
                
                printf("Please enter the name of the book.\n");
                scanf("%s", inventory[i].book_name);
                
                printf("Please enter the publisher of the book.\n");
                scanf("%s", inventory[i].publisher);
                
                printf("Please enter the number of books in stock.\n");
                scanf("%d", &inventory[i].in_stock);
                
                printf("Please enter the ISBN number of the book.\n");
                scanf("%d", &inventory[i].isbn);
                
                printf("Please enter the suppliers cost for one book.\n");
                scanf("%lf", &inventory[i].cost);
                
                printf("Please enter the retail price for the book.\n");
                scanf("%lf", &inventory[i].srp);
                
                exit = -1;
                printf("i is %d", i);
                printf("inv size is %d", *inv_size);
                *inv_size = (*inv_size + 1);
                printf("inv size is %d", *inv_size);
            }
            else
                i++;
            
        }while(exit == 0 && i < INV_SIZE);
    }
    
    void DeleteItem(vbook inventory[], int *inv_size)
    {
        char temp_title[SIZE];
        int temp_isbn;
        int i;
        
        printf("Please enter the name of the book you would like to delete.\n");
        scanf("%s", temp_title);
        
        printf("Please enter the ISBN number of the item you want to delete.\n");
        scanf("%d", &temp_isbn);
        
        for(i = 0; i < *inv_size; i++)
        {
            if(temp_isbn == inventory[i].isbn)
            {
                inventory[i] = inventory[*(inv_size-1)];
            }
            else
                printf("That item could not be found in the inventory.\n");
        }
    }
    
    void DisplayRecords(vbook inventory[], int *inv_size)
    {
        int i;
        for(i = 0; i < *inv_size; i++)
        {
            printf("Book Name: %s\n", inventory[i].book_name);
            printf("Publisher Name: % s\n", inventory[i].publisher);
            printf("ISBN: %d\n", inventory[i].isbn);
            printf("Number of items in stock: %d\n", inventory[i].in_stock);
            printf("Supplier Price: %.2f\n", inventory[i].cost);
            printf("Suggested Retail Price: %.2f\n\n", inventory[i].srp);
        }
    }
    
    void InventoryReport(vbook inventory[], int *inv_size, FILE *fp)
    {
        int i;
        for(i = 0; i < *inv_size; i++)
        {
           fprintf(fp,"Book Name: %s\n", inventory[i].book_name);
           fprintf(fp,"Publisher Name: % s\n", inventory[i].publisher);
           fprintf(fp,"ISBN: %d\n", inventory[i].isbn);
           fprintf(fp,"Number of items in stock: %d\n", inventory[i].in_stock);
           fprintf(fp,"Supplier Price: %.2f\n", inventory[i].cost);
           fprintf(fp,"Suggested Retail Price: %.2f\n\n", inventory[i].srp);
        }
    }
    
    void ClearInventory(int *inv_size)
    {
        *inv_size = 0;
    }
    
    int Exit()
    {
        return(-1);
    }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    There is an "abnormal program termination" after inputting the ISBN number of the book. The program closes after experiencing that problem.

    The error is:
    Code:
    scanf: floating point formats not linked
    Abnormal program termination
    I was able to experience that problem before when I was using Borland C/C++ compiler 3.0. I switched to DevC++ and I never had any problem. Since I was having a problem with using the floating point in a structure. I tried not to use the double nor float data type in a structure but I used int and I never had any abnormal termination of the program. But as of right now I wasn't sure if the problem is with the compiler or the code that you have. By the way what compiler are you using?

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    12

    Re: Fixed

    Quote Originally Posted by $l4xklynx View Post
    By the way what compiler are you using?
    Im using Cygwin.

    I just got the whole thing to work perfectly (I think).

    Heres the code I have as of now.

    Feel free to test it and I would appreciate anyone informing me of bugs I missed.

    Thanks a lot for all your help. This thing was driving me nuts. :-P

    Code:
    #define INV_SIZE 20
    #define SIZE 30
    #define INITIAL_SIZE 5
    #include <stdio.h>
    #include <string.h>
    typedef struct
    {
        char book_name[SIZE];
        char publisher[SIZE];
        int in_stock;
        int isbn;
        double cost;
        double srp;
    }vbook;
    
    void Initialize(vbook inventory[]);
    int Menu();
    void AddNewItem(vbook inventory[], int *inv_size);
    void DeleteItem(vbook inventory[], int *inv_size);
    void DisplayRecords(vbook inventory[], int *inv_size);
    void InventoryReport(vbook inventory[], int *inv_size, FILE *fp);
    void ClearInventory(int *inv_size);
    int Exit();
    
    int main()
    {
        vbook inventory[INV_SIZE] = {0};
       int selection = 0;
       int i_size = 5;
       int *inv_size;
       int exit = 0;
       inv_size = &i_size;
       FILE *fp;
        fp = fopen("report.txt", "w");
        
       Initialize(inventory);//Initializes the first five sections of the inventory array
       *inv_size = 5;
       do{
            selection = Menu();
       
            switch (selection)
                {
                    case 1:
                        AddNewItem(inventory, inv_size);
                        break;
           
                    case 2:
                        DeleteItem(inventory, inv_size);
                        break;
               
                    case 3:
                        DisplayRecords(inventory, inv_size);
                        break;
                    case 4:
                        InventoryReport(inventory, inv_size, fp);
                        break;
                    case 5:
                        ClearInventory(inv_size);
                        break;
                    case 6:
                        exit = Exit();
                        break;
                }
       }while(exit != -1);
       return(0);
    }
    
    void Initialize(vbook inventory[])
    {
        int i;
        
        strcpy(inventory[0].book_name, "Twilight");
        strcpy(inventory[0].publisher, "Little_Readers");
        inventory[0].isbn = 000;
        inventory[0].in_stock = 99;
        inventory[0].cost = 4.20;
        inventory[0].srp = 19.99;
        
        strcpy(inventory[1].book_name, "New_Moon");
        strcpy(inventory[1].publisher, "Little_Readers");
        inventory[1].isbn = 111;
        inventory[1].in_stock = 16;
        inventory[1].cost = 5.30;
        inventory[1].srp = 24.99;
        
        strcpy(inventory[2].book_name, "Eclipse");
        strcpy(inventory[2].publisher, "Little_Readers");
        inventory[2].isbn = 222;
        inventory[2].in_stock = 20;
        inventory[2].cost = 6.20;
        inventory[2].srp = 26.99;
        
        strcpy(inventory[3].book_name, "Brave_New_World");
        strcpy(inventory[3].publisher, "Penguin_Classics");
        inventory[3].isbn = 333;
        inventory[3].in_stock = 43;
        inventory[3].cost = 13.28;
        inventory[3].srp = 35.99;
        
        strcpy(inventory[4].book_name, "Harry_Potter");
        strcpy(inventory[4].publisher, "Wizard");
        inventory[4].isbn = 5555;
        inventory[4].in_stock = 17;
        inventory[4].cost = 1.99;
        inventory[4].srp = 10.99;
        
        for(i = 5;i <= INV_SIZE;i++)
        {
            inventory[i].isbn = -1;
        }
        
    }
    
    int Menu()
    {
        int selection;
        
        printf("Enter 1 to add a new item to inventory.\n");
        printf("Enter 2 to delete an item from inventory.\n");
        printf("Enter 3 to display all inventory records.\n");
        printf("Enter 4 to create a current inventory report.\n");
        printf("Enter 5 to clear entire inventory.\n");
        printf("Enter 6 to Exit.\n");
        
        scanf("%d", &selection);
        return(selection);
          
    }
    
    void AddNewItem(vbook inventory[], int *inv_size)
    {
        int i = 0;
        int exit = 0;
        
        do{   
            if(i > INV_SIZE)
            {
                printf("Your inventory size has surpassed available memory.\n");
                printf("Please delete some items from inventory or increase inventory size.\n");
                exit = -1;
            }
            if(inventory[i].isbn == -1 && exit == 0)
            {
                
                printf("Please enter the name of the book.\n");
                scanf("%s", inventory[i].book_name);
                
                printf("Please enter the publisher of the book.\n");
                scanf("%s", inventory[i].publisher);
                
                printf("Please enter the number of books in stock.\n");
                scanf("%d", &inventory[i].in_stock);
                
                printf("Please enter the ISBN number of the book.\n");
                scanf("%d", &inventory[i].isbn);
                
                printf("Please enter the suppliers cost for one book.\n");
                scanf("%lf", &inventory[i].cost);
                
                printf("Please enter the retail price for the book.\n");
                scanf("%lf", &inventory[i].srp);
                
                exit = -1;
                printf("i is %d", i);
                printf("inv size is %d", *inv_size);
                *inv_size = (*inv_size + 1);
                printf("inv size is %d", *inv_size);
            }
            else
                i++;
            
        }while(exit == 0 && i < INV_SIZE);
    }
    
    void DeleteItem(vbook inventory[], int *inv_size)
    {
        char temp_title[SIZE];
        int temp_isbn;
        int i;
        int success = 0;
        
        printf("Please enter the name of the book you would like to delete.\n");
        scanf("%s", temp_title);
        
        printf("Please enter the ISBN number of the item you want to delete.\n");
        scanf("%d", &temp_isbn);
        
        for(i = 0; i <= *inv_size; i++)
        {
            if(temp_isbn == inventory[i].isbn)
            {
                inventory[i] = inventory[(*inv_size) - 1];
                success = 1;
                (*inv_size)--;
            }
            
        }
        if(success = 0)
                printf("That item could not be found in the inventory.\n");
    }
    
    void DisplayRecords(vbook inventory[], int *inv_size)
    {
        int i;
        for(i = 0; i < *inv_size; i++)
        {
            printf("Book Name: %s\n", inventory[i].book_name);
            printf("Publisher Name: % s\n", inventory[i].publisher);
            printf("ISBN: %d\n", inventory[i].isbn);
            printf("Number of items in stock: %d\n", inventory[i].in_stock);
            printf("Supplier Price: %.2f\n", inventory[i].cost);
            printf("Suggested Retail Price: %.2f\n\n", inventory[i].srp);
        }
    }
    
    void InventoryReport(vbook inventory[], int *inv_size, FILE *fp)
    {
        int i;
        for(i = 0; i < *inv_size; i++)
        {
           fprintf(fp,"Book Name: %s\n", inventory[i].book_name);
           fprintf(fp,"Publisher Name: % s\n", inventory[i].publisher);
           fprintf(fp,"ISBN: %d\n", inventory[i].isbn);
           fprintf(fp,"Number of items in stock: %d\n", inventory[i].in_stock);
           fprintf(fp,"Supplier Price: %.2f\n", inventory[i].cost);
           fprintf(fp,"Suggested Retail Price: %.2f\n\n", inventory[i].srp);
        }
    }
    
    void ClearInventory(int *inv_size)
    {
        *inv_size = 0;
    }
    
    int Exit()
    {
        return(-1);
    }

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    just my 2c
    Code:
    #define INV_SIZE 20
    #define SIZE 30
    #define INITIAL_SIZE 5
    #include <stdio.h>
    #include <string.h>
    typedef struct
    {
        char book_name[SIZE];
        char publisher[SIZE];
        int in_stock;
        int isbn;
        double cost;
        double srp;
    }vbook;
    
    void Initialize(vbook inventory[]);
    int Menu();
    void AddNewItem(vbook inventory[], int *inv_size);
    void DeleteItem(vbook inventory[], int *inv_size);
    void DisplayRecords(vbook inventory[], int *inv_size);
    void InventoryReport(vbook inventory[], int *inv_size, FILE *fp);
    void ClearInventory(int *inv_size);
    int Exit();
    
    int main()
    {
        vbook inventory[INV_SIZE] = {0};
       int selection = 0;
       int i_size = 5;
       int *inv_size;
       int exit = 0;
       inv_size = &i_size;
       FILE *fp;
        fp = fopen("report.txt", "w");
        
       Initialize(inventory);//Initializes the first five sections of the inventory array
       *inv_size = 5;  /* redundant since *inv_size = i_size = 5 */
       do{
            selection = Menu();
       
            switch (selection)
                {
                    case 1:
                        AddNewItem(inventory, inv_size);
                        break;
           
                    case 2:
                        DeleteItem(inventory, inv_size);
                        break;
               
                    case 3:
                        DisplayRecords(inventory, inv_size);
                        break;
                    case 4:
                        InventoryReport(inventory, inv_size, fp);
                        break;
                    case 5:
                        ClearInventory(inv_size);
                        break;
                    case 6:
                        exit = Exit();
                        break;
                }
       }while(exit != -1);
       return(0);
    }
    
    void Initialize(vbook inventory[])
    {
        int i;
        
        strcpy(inventory[0].book_name, "Twilight");
        strcpy(inventory[0].publisher, "Little_Readers");
        inventory[0].isbn = 000;
        inventory[0].in_stock = 99;
        inventory[0].cost = 4.20;
        inventory[0].srp = 19.99;
        
        strcpy(inventory[1].book_name, "New_Moon");
        strcpy(inventory[1].publisher, "Little_Readers");
        inventory[1].isbn = 111;
        inventory[1].in_stock = 16;
        inventory[1].cost = 5.30;
        inventory[1].srp = 24.99;
        
        strcpy(inventory[2].book_name, "Eclipse");
        strcpy(inventory[2].publisher, "Little_Readers");
        inventory[2].isbn = 222;
        inventory[2].in_stock = 20;
        inventory[2].cost = 6.20;
        inventory[2].srp = 26.99;
        
        strcpy(inventory[3].book_name, "Brave_New_World");
        strcpy(inventory[3].publisher, "Penguin_Classics");
        inventory[3].isbn = 333;
        inventory[3].in_stock = 43;
        inventory[3].cost = 13.28;
        inventory[3].srp = 35.99;
        
        strcpy(inventory[4].book_name, "Harry_Potter");
        strcpy(inventory[4].publisher, "Wizard");
        inventory[4].isbn = 5555;
        inventory[4].in_stock = 17;
        inventory[4].cost = 1.99;
        inventory[4].srp = 10.99;
        
        for(i = 5;i <= INV_SIZE;i++)
        {
            inventory[i].isbn = -1;
        }
        
    }
    
    int Menu()
    {
        int selection;
        
        printf("Enter 1 to add a new item to inventory.\n");
        printf("Enter 2 to delete an item from inventory.\n");
        printf("Enter 3 to display all inventory records.\n");
        printf("Enter 4 to create a current inventory report.\n");
        printf("Enter 5 to clear entire inventory.\n");
        printf("Enter 6 to Exit.\n");
        
        scanf("%d", &selection);
        return(selection);
          
    }
    
    void AddNewItem(vbook inventory[], int *inv_size)
    {
        int i = 0;
        int exit = 0;
        
        do{   
            if(i > INV_SIZE)
            {
                printf("Your inventory size has surpassed available memory.\n");
                printf("Please delete some items from inventory or increase inventory size.\n");
                exit = -1;
            }
            if(inventory[i].isbn == -1 && exit == 0)
            {
                
                printf("Please enter the name of the book.\n");
                scanf("%s", inventory[i].book_name);
                
                printf("Please enter the publisher of the book.\n");
                scanf("%s", inventory[i].publisher);
                
                printf("Please enter the number of books in stock.\n");
                scanf("%d", &inventory[i].in_stock);
                
                printf("Please enter the ISBN number of the book.\n");
                scanf("%d", &inventory[i].isbn);
                
                printf("Please enter the suppliers cost for one book.\n");
                scanf("%lf", &inventory[i].cost);
                
                printf("Please enter the retail price for the book.\n");
                scanf("%lf", &inventory[i].srp);
                
                exit = -1;
                printf("i is %d", i);
                printf("inv size is %d", *inv_size);
                *inv_size = (*inv_size + 1);
                printf("inv size is %d", *inv_size);
            }
            else
                i++;
            
        }while(exit == 0 && i < INV_SIZE);
    }
    
    void DeleteItem(vbook inventory[], int *inv_size)
    {
        char temp_title[SIZE];
        int temp_isbn;
        int i;
        int success = 0;
        
        printf("Please enter the name of the book you would like to delete.\n");
        scanf("%s", temp_title);
        
        printf("Please enter the ISBN number of the item you want to delete.\n");
        scanf("%d", &temp_isbn);
        
        for(i = 0; i <= *inv_size; i++)
        {
            if(temp_isbn == inventory[i].isbn)
            {
                inventory[i] = inventory[(*inv_size) - 1];
                success = 1;
                (*inv_size)--;
            }
            
        }
        if(success = 0)
                printf("That item could not be found in the inventory.\n");
    }
    
    void DisplayRecords(vbook inventory[], int *inv_size)
    {
        int i;
        for(i = 0; i < *inv_size; i++)
        {
            printf("Book Name: %s\n", inventory[i].book_name);
            printf("Publisher Name: % s\n", inventory[i].publisher);
            printf("ISBN: %d\n", inventory[i].isbn);
            printf("Number of items in stock: %d\n", inventory[i].in_stock);
            printf("Supplier Price: %.2f\n", inventory[i].cost);
            printf("Suggested Retail Price: %.2f\n\n", inventory[i].srp);
        }
    }
    
    void InventoryReport(vbook inventory[], int *inv_size, FILE *fp)
    {
        int i;
        for(i = 0; i < *inv_size; i++)
        {
           fprintf(fp,"Book Name: %s\n", inventory[i].book_name);
           fprintf(fp,"Publisher Name: % s\n", inventory[i].publisher);
           fprintf(fp,"ISBN: %d\n", inventory[i].isbn);
           fprintf(fp,"Number of items in stock: %d\n", inventory[i].in_stock);
           fprintf(fp,"Supplier Price: %.2f\n", inventory[i].cost);
           fprintf(fp,"Suggested Retail Price: %.2f\n\n", inventory[i].srp);
        }
    }
    
    void ClearInventory(int *inv_size)
    {
        *inv_size = 0;
    }
    
    int Exit()
    {
        return(-1);
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Firstly, bug:
    Code:
    void DeleteItem(vbook inventory[], int *inv_size)
    {
    	char temp_title[SIZE];
    	int temp_isbn;
    	int i;
    	int success = 0;
    
    	printf("Please enter the name of the book you would like to delete.\n");
    	scanf("&#37;s", temp_title);
    
    	printf("Please enter the ISBN number of the item you want to delete.\n");
    	scanf("%d", &temp_isbn);
    
    	for(i = 0; i <= *inv_size; i++)
    	{
    		if(temp_isbn == inventory[i].isbn)
    		{
    			inventory[i] = inventory[(*inv_size) - 1];
    			success = 1;
    			(*inv_size)--;
    		}
    
    	}
    	if(success = 0)
    		printf("That item could not be found in the inventory.\n");
    }
    Secondly, do not read strings with scanf.
    Need help with altering info on a txt file
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Thank you everyone for your help.

    Quote Originally Posted by Elysia View Post
    do not read strings with scanf.
    Need help with altering info on a txt file
    I have read this on your comments on another program before and I think its a great idea. Unfortunately the programming course I am in has not covered fgets so I don't think it would be wise in this case. but I completely intend to use it in the future now that I know about it.

    Thanks again and keep up the good work!
    Last edited by raptor1770; 11-29-2008 at 09:16 AM. Reason: Spelling

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
            if(i > INV_SIZE)
            {
                printf("Your inventory size has surpassed available memory.\n");
                printf("Please delete some items from inventory or increase inventory size.\n");
                exit = -1;
            }
    My guess would be that you haven't tested this bit very thoroughly - because you actually allow one more entry than you should. The comparison should be >= . And it should use *inv_size, not i, as i is initialized to zero in each call to the AddNewItem function.

    If you are using ISBN as the indicator of "I want to exit", then you probably should ask for the ISBN before you ask for the other book information, and if the ISBN is -1, then skip all other input.

    Calling a variable the same as a standard function (exit) is not a great idea - it works ok, but should you require the exit function as well, you'll end up having to rename the variable, so it is a good habit to not use that name.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Passing An Array Of Structures To A Function
    By nexusdarkblue in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 08:24 AM
  3. pointer to array of structures
    By strokebow in forum C Programming
    Replies: 14
    Last Post: 12-09-2006, 02:14 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02:09 PM
  5. Passing pointer to array to a function
    By Tojam in forum C Programming
    Replies: 1
    Last Post: 10-09-2002, 10:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21