Thread: exit program

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    exit program

    Exit function will not work. Can't figure out why? Here is the code, any suggestions are welcome.

    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define clrscr() system("cls")

    //structure for writing to and reading from file**
    struct filebook
    {
    int code;
    char title[20];
    char author[20];
    int pages;
    }ebook;

    //************************************************

    //structure for creating the linked list**********
    struct book
    {
    int code;
    char title[20];
    char author[20];
    int pages;
    int deleted;
    struct book*next;
    }*head,*work,*node,*previous,*found,libry;

    FILE*library;
    //VAR declarations
    char title[20];
    char ans;
    int ad;
    int buffer;
    char author;
    int ctr;
    int choice;
    int code;

    //************************************************

    /* FUNCTION PROTOTYPES*********************/
    int main_menu();
    void add_record(void);
    void delete_record(void);
    void search(void);
    void display(void);
    void exit_prog(void);
    /*******************************************/

    void main()
    {
    FILE *library;
    int choice;

    clrscr();
    library = fopen("library.dat", "r+b"); //opening in binary mode for read/write

    if (library == NULL)
    {
    printf("\n\nFile does not exist: Creating file");
    library = fopen("library.dat", "w+b"); //creation in binary mode
    head=NULL;//empty list
    //key_wait();
    getch();
    }
    else
    {
    /*Load the file contents in a linked list*******************/
    head=work=(struct book *)malloc(sizeof(struct book));
    fread(work,sizeof(struct filebook),1,library);
    while(!feof(library))
    {
    work->deleted=0;
    work->next=(struct book *)malloc(sizeof(struct book));
    fread(work->next,sizeof(struct filebook),1,library);
    if(!feof(library))
    {
    work=work->next;
    }
    else
    {
    work->next=NULL;//end of list
    }
    }

    }
    fclose(library);

    do
    {
    choice=main_menu();
    clrscr();
    switch (choice)
    {
    case 1: add_record(); break;
    case 2: delete_record(); break;
    case 3: search(); break;
    case 4: display(); break;
    case 5: exit_prog(); break;
    }
    }
    while (choice != '6');

    }
    /************************************************** **********/
    int main_menu()
    /************************************************** **********/
    {
    system("cls");
    printf ("\t\t\tTusket Municipal Library\n\n\n");
    printf ("\tPlease make a selection...\n\n");
    printf ("\t1.) Enter a book in the inventory\n");
    printf ("\t2.) Delete a book from the inventory\n");
    printf ("\t3.) Search the inventory for a book\n");
    printf ("\t4.) Display the book inventory\n");
    printf ("\t5.) Exit the program\n\n");
    printf ("\tEnter your selection:1,2,3,4 or 5: ");
    scanf ("%d", &choice);
    return choice;
    }
    /************************************************** **********/
    void add_record(void)
    /*
    Task: Read from keyboard and add the record in the list
    Note: The code must be unique and < 10000
    */
    /************************************************** **********/
    {
    another:
    system("cls");
    printf ("\t\t\tTusket Municipal Library\n\n\n");
    printf("Enter code:");
    scanf("%d",&libry.code);
    printf("\nEnter Title:");
    scanf("%s",&libry.title);
    printf("\nEnter Author:");
    scanf("%s",&libry.author);
    printf("\nEnter Number of Pages:");
    scanf("%d",&libry.pages);


    choice:
    printf("\n\nDo you want to add another book?:");//answer must not be a capital
    scanf("%s",&ans);
    if(ans=='n')
    {
    work->next=NULL;
    library=fopen("library.dat","a+b");
    fclose(library);
    main_menu();
    }
    else
    {
    if(ans=='y')
    {
    work->next=(struct book*)malloc(sizeof(ebook));
    work=work->next;
    goto another;
    }
    else
    {
    if(ans!='n')
    {
    printf("Please Enter lower case y or n");
    printf ("\n\nPress a key to continue...");
    getch();
    goto choice;
    }
    else
    {
    if(ans!='y')
    {
    printf("Please Enter lower case y or n");
    printf ("\n\nPress a key to continue...");
    getch();
    goto choice;
    }
    }
    }
    }
    library=fopen("library.dat","a+b");
    if((library=fopen("library.dat","a+b"))!=NULL)
    {
    fwrite (library,sizeof(struct book),(size_t)3,library);
    fclose (library);
    }
    main_menu();
    }

    /************************************************** **********/
    void delete_record(void)
    /*
    Tasks: Read the code input by the user
    Search for that code in the list
    Mark the record as DELETED or
    Advise user of its non-existence
    Note: Must check the value of DELETED field before
    */
    /************************************************** **********/
    {
    // system("cls");
    // printf ("\t\t\tTusket Municipal Library\n\n\n");
    // printf ("Enter code of the book to be deleted: ");
    // scanf ("%d", &code);


    }

    /************************************************** **********/
    void search(void)
    /*
    Tasks: Read the code input by the user
    Search for that code in the list
    Display the record or advise user of its non-existence
    Note: Must check the value of DELETED field
    */
    /************************************************** **********/
    {
    int key;
    system("cls");
    printf ("\t\t\tTusket Municipal Library\n\n\n");
    printf("Which book code do you want to search for?: ");
    scanf("%d", &key);
    printf("\n");

    found=head;
    while(found!= NULL && found->code!= key) //DO NOT reverse condition
    {
    found=found->next;
    }
    if (found == NULL)
    {
    printf ("Sorry, not in the list.");
    printf("\n");
    getch();
    main_menu();
    }
    else
    {
    printf ("Your book has been found in the list.");
    printf("\n");
    getch();
    main_menu();
    }


    }



    /************************************************** **********/
    void display(void)
    /*
    Task: Ask for the order (ASC or DESC)
    CREATE a SORTED list
    Display all records from the SORTED list
    Release RAM taken by SORTED LIST
    Note: Must check the value of DELETED field
    */
    /************************************************** **********/
    {
    system("cls");
    printf ("\t\t\tTusket Municipal Library\n\n\n");
    printf("Press 1 for ascending; 2 for descending: ");
    scanf("%d", ad);
    if (head!=NULL)
    {
    if (ad==1)
    {
    work->next=(struct book*)malloc(sizeof(ebook));
    work=work->next;
    work->next=NULL;
    fread(library,sizeof(ebook),1,library);
    node=work;
    work=head;
    while(work->next!=NULL)
    {
    if (node->code<work->code)
    {
    buffer=work->code;
    work->code=node->code;
    node->code=buffer;
    }
    work=work->next;
    }
    }
    work=head;
    printf("\nContents of List:\n\n");
    do
    {
    printf("%d\t",work->code);
    work=work->next;
    }while (work!=NULL);
    }
    if(head!=NULL)
    {
    if (ad==2)
    {
    work->next=(struct book*)malloc(sizeof(ebook));
    work=work->next;
    work->next=NULL;
    fread(library,sizeof(ebook),1,library);
    node=work;
    work=head;
    while(work->next!=NULL)
    {
    if (node->code>work->code)
    {
    buffer=work->code;
    work->code=node->code;
    node->code=buffer;
    }
    work=work->next;
    }
    }
    work=head;
    if (head!=NULL)
    {

    printf("\n\nContents of List (Reversed order):\n\n");
    do
    {
    printf("%d\t",work->code);
    work=work->next;
    }while (work!=NULL);
    }
    }
    }

    /************************************************** **********/
    void exit_prog(void)
    /*
    Task: Pause until user presses a key
    */
    /************************************************** **********/
    {
    library = fopen("library.dat", "wb");
    work=head;
    while(work!=NULL)
    {
    //if(work->deleted==0) commented out because delete function not implemented yet
    //{
    fwrite(work,sizeof(struct filebook),1,library);
    // }
    work=work->next;
    }
    fclose(library);
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    A few remarks I've found when looking through the code:

    Don't use void main, but use int main. There are some other threads about that.

    Don't use too much globals, they make code harder to read.

    FILE*library is global and local in main?

    This is not a usual linked implementation. A usual implementation of a structure in linked lists would be:

    Code:
    struct node
    {
        int data;
        struct node *nextnode;
        struct node *previous;
    };
    Function malloc does not need to be casted, it is safer not to cast it.

    Do a little bit more error-checking, might prevent you from bugs.



    Not meant to be offending, but please edit your post and add code tags. It's very hard to read the code the way you posted it.

    [ code ]
    /* your code */
    [ /code ]

    Note that the spaces between [ and ] must be left away.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    For the LOVE of god, please use CODE tags...

    Anyway, I found this:
    while (choice != '6');

    Where you compare the integer choice with the character 6, which is NOT the same as the number 6...

    Use while (choice != 6) instead!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Unregistered
    Guest
    void exit_prog(void)
    /*
    Task: Pause until user presses a key
    */
    /
    Your exit function doesn't make any sense to me. Don't you normally just say exit(1)?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fwrite (library,sizeof(struct book),(size_t)3,library);
    Better check those parameters

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with Program!
    By afnitti in forum C Programming
    Replies: 9
    Last Post: 04-15-2009, 08:06 PM
  2. exit a program at any point.
    By slightofhand in forum C Programming
    Replies: 5
    Last Post: 03-02-2008, 09:08 AM
  3. Program Terminating With Error On Exit
    By chriscolden in forum C Programming
    Replies: 19
    Last Post: 01-14-2006, 04:40 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM