Thread: Delete function: "Windows stopped working" PROBLEM in a Link List Code in C

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Delete function: "Windows stopped working" PROBLEM in a Link List Code in C

    Hello,

    I am a beginner in C programming, and this code shown below is part of my final exam in Programming. But, although when I run it, it works fine, when I input an ID number in the 'delete' function that I have previously input in the 'add' function, it gives me a 'windows stopped working error'. I do not understand why, I personally have written the code, and went trough every line of it.

    I would really appreciate your help. Thank you in advance.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <windows.h>
    
    
    typedef struct BookDetails
    {
         char bookName[30];
         char bookAuthor[30];
         char bookPub[30];
         int bookQuan;
         int bookID;
         struct BookDetails *nextBook; // the pointer link to the next node
    
    
    } b ;
    
    
    struct BookDetails *head=NULL, *temp, *disp;
    
    
    void addBook();
    void deleteBook();
    void disBook();
    void AltEnter(void);
    void DateTime(void);
    void ShowTime(void);
    void menu(void);
    
    
    int main(int argc, char *arvg[])
    {
        ShowTime();
        DateTime();
        menu();
    
    
        return 0;
    
    
    }
    
    
    void menu()
    
    
    {   system("CLS");
        int choice;
    
    
        system("cls");
    
    
        while(1)
        {
            DateTime();
            printf("\n\n\n");
            printf(" \t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 ADMIN MENU \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 \n\n");
            printf(" \t \xDB\xDB\xDB\xDB\xB2 1. Add A New Book Record \n \n ");
            printf(" \t \xDB\xDB\xDB\xDB\xB2 2. Delete A Book Record \n \n");
            printf(" \t \xDB\xDB\xDB\xDB\xB2 3. View All Book Records \n \n");
            printf(" \t \xDB\xDB\xDB\xDB\xB2 4. Close Application \n\n ");
            printf(" \t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 \n\n");
    
    
            printf(" \t ENTER YOUR CHOICE: ");
            scanf("%d", &choice);
    
    
            fflush(stdin);
    
    
            switch(choice)
            {
                case 1:
                addBook();
    
    
                break;
    
    
                case 2:
                deleteBook();
    
    
                break;
    
    
                case 3:
                disBook();
    
    
                break;
    
    
                case 4:
                exit(0);
            }
        }
    
    
    }
    
    
    void addBook()
    {
        system("CLS");
    
    
        struct BookDetails *add;
    
    
        char answer ='y';
    
    
        while (answer =='y')
        {
            add=(struct BookDetails*)malloc(sizeof(struct BookDetails));
    
    
            printf("\n Enter ID: ");
            scanf("%d", &add->bookID);
            fflush(stdin);
    
    
            printf(" Enter Title: ");
            gets(add->bookName);
            fflush(stdin);
    
    
            printf(" Enter Author: ");
            gets(add->bookAuthor);
            fflush(stdin);
    
    
            printf(" Enter Publisher: ");
            gets(add->bookPub);
            fflush(stdin);
    
    
            printf(" Enter Num of Copies: ");
            scanf("%d", &add->bookQuan);
            fflush(stdin);
    
    
            if(head==NULL)
            {
                head=add;
                add->nextBook=NULL;
    
    
                temp=add;
            }
            else
            {
                temp->nextBook=add;
                add->nextBook=NULL;
                temp=add;
            }
            printf("\n\n Would you like to enter another book record (y/n)? ");
            fflush(stdin);
            answer = getchar();
    
    
    
    
        }
    
    
           char screenOpen;
                screenOpen=getchar(); //keeps screen open untill a key is pressed
    
    
                menu();
    
    
            }
    
    
    
    
    
    
        void deleteBook()
        {
            system("CLS");
    
    
            struct BookDetails *delete1;
            int bbID, present=0;
    
    
            if(head==NULL)
            {
                printf("\n No records of books to delete! \n");
                return;
            }
    
    
            printf("\n When a book is purchased is deleted. Enter the ID of the book you wish to delete: ");
            scanf("%d", &bbID);
            fflush(stdin);
    
    
            for(delete1=head; delete1!=NULL; delete1=delete1->nextBook)
            {
                if(delete1->bookID==bbID)
                {
                    if(head->bookID==bbID)
                    {
                        delete1=head;
                        head=head->nextBook;
                        printf("\n\n The book: %s with ID: %d has been DELETED!", head->bookName,head->bookID);
                        free(delete1);
                        return;
                    }
                }
                temp=delete1;
            }
    
    
            if(present==0)
            {
                char answer;
                printf("\n ERROR: There is not such book present in this bookstore.");
                printf("\n\n Try again (y/n)? ");
                answer = getchar();
                if(answer == 'y')
                    deleteBook();
                else menu();
            }
    
    
                char screenOpen;
                screenOpen=getchar(); //keeps screen open untill a key is pressed
    
    
                menu();
    
    
            }
    
    
    
    
    
    
     void disBook()
     {
    
    
         system("CLS");
    
    
         if(head==NULL)
         {
             printf("\n Currently the bookstore is building.There are no records of books to view.");
             return;
         }
         for(disp=head; disp!=NULL; disp=disp->nextBook)
         {
             printf("\n\n ID: %d", disp->bookID);
             printf("\n\n Title: %s", disp->bookName);
             printf("\n\n Publisher: %s", disp->bookPub);
             printf("\n\n No. Copies: %d", disp->bookQuan);
    
    
         }
    
    
                char screenOpen;
                screenOpen=getchar(); //keeps screen open untill a key is pressed
    
    
                menu();
    
    
     }
    
    
    
    
    
    
    
    
    void ShowTime(void)
    {
        AltEnter();
        system("COLOR 1F");
    
    
        return;
    }
    
    
    void AltEnter(void)
    {
        keybd_event(VK_MENU,
                    0x38,
                    0,
                    0);
        keybd_event(VK_RETURN,
                    0x1c,
                    0,
                    0);
        keybd_event(VK_RETURN,
                    0x1c,
                    KEYEVENTF_KEYUP,
                    0);
        keybd_event(VK_MENU,
                    0x38,
                    KEYEVENTF_KEYUP,
                    0);
         return;
    }
    
    
    void DateTime(void)
    {
        printf("\n");
        printf("\n");
        printf("\n");
        printf("                ");
        printf(__DATE__);
        printf("       ");
        printf(__TIME__);
        printf("\n");
        printf("\n");
    
    
        return;
    
    
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you are being graded on this code, then you have no excuse not to format the code properly.
    Whitespace: Use only one blank line occasionally to emphasise parts which naturally belong together. Two lines very sparingly, in extreme cases. Never six!
    I believe that Visual Studio can fix up some formatting for you if you select all and press ctrl+K, then ctrl+F.

    Line 20, 261: disp should be a local variable, in disBook, not a global variable.
    Lines 48 & 52: Do you spot any redundancy here? In any case the statement should not be on the same line as the opening brace.
    Line 71: Don't do this. Read the FAQ entry for alternatives.
    Line 119: Don't cast malloc. Read the FAQ for why.
    Lines 114, 117: Setting a variable to a specific value just so that a while loop will execute at least once indicates that you should be using a do..while loop instead.
    Line 128: You are using the forbidden function "gets". This function's existence is a mistake and it should never be used. It is impossible to avoid damaging buffer overruns with it. Read the FAQ for an alternative.
    Line 213: You're changing the value of head here, which you should know is only necessary if you are deleting the first item in the list. So where's the code for handling deletion of an item that is not at the start of the list?
    Line 236: You might want to watch your spelling even in the comments.
    Line 294: a return at the end of a void-returning function is redundant.
    Lime 298: What system are you running this on? Windows doesn't normally let you set fullscreen mode for a console window.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Thank you very much. I will see what I can do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-19-2012, 07:15 PM
  2. Replies: 7
    Last Post: 05-21-2009, 05:50 PM
  3. Extremely strange "delete" problem
    By dr_jaymahdi in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2007, 09:06 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM

Tags for this Thread