Thread: help...

  1. #1
    Unregistered
    Guest

    help...

    the delete function in the following gives me an error everytime i run the program, but no error's appear when i compile it...

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

    struct carData {
    int carNum;
    char make[15];
    char model[15];
    int year;
    double price;
    char regNum[10];
    long int mileage;
    struct carData *next;
    };

    typedef struct carData CarData;
    typedef CarData *carDataPtr;

    void Add();
    //void Update();
    char Delete(carDataPtr *);
    //void List();

    int main()
    {
    int decision;

    FILE *in;
    //struct carData details = {0, "", "", 0, 0.0, "", 0};
    carDataPtr details = NULL;

    if((in = fopen("cardetails.", "rb+")) == NULL) {
    printf("File <cardetails.dat> cannot be opened.\n");
    exit(1);
    }
    /*for(i=1;i<=100;i++)
    fwrite(&details, sizeof(struct carData), 1, in);
    fclose(in);*/

    printf("** Al Hayes Motors Processing System **\n\n");
    printf("Choose an option:");

    do {
    printf("\n1. Add a new record.\n");
    printf("2. Update a record.\n");
    printf("3. Delete a record\n");
    printf("4. List all records on a formatted file for printing\n");
    printf("5. Exit\n>> ");
    scanf("%d", &decision);

    switch(decision) {
    case 1:
    Add();
    break;
    /*case 2:
    Update();
    break;*/
    case 3:
    Delete(&details);
    break;
    /*case 4:
    List();
    break;*/
    default:
    if(decision!=5) {
    printf("Invalid option - Choose Again!\n\n");
    }
    }

    }
    while(decision != 5);

    printf("Goodbye!\n");

    return 0;

    }

    void Add()
    {
    char ans;
    FILE *in;
    struct carData details = {0, "", "", 0, 0.0, "", 0};

    if((in = fopen("cardetails.dat", "rb+")) == NULL)
    printf("File <cardetails.dat> cannot be opened.\n");
    else {
    printf("Enter Car Number<1-100; 0 - back to main screen>: ");
    scanf("%d", &details.carNum);

    while(details.carNum != 0) {

    fflush(stdin);
    printf("Enter Make: ");
    scanf("%s", details.make);
    printf("Enter Model: ");
    scanf("%s", details.model);
    printf("Enter Year: ");
    scanf("%d", &details.year);
    printf("Enter Price: ");
    scanf("%lf", &details.price);
    printf("Enter Reg Num: ");
    scanf("%s", details.regNum);
    printf("Enter Mileage: ");
    scanf("%ld", &details.mileage);
    printf("Would you like to write this data<y/n>");
    scanf("%c", &ans);
    scanf("%c", &ans);
    if(ans=='y') {
    fseek(in, (details.carNum - 1) * sizeof(struct carData), SEEK_SET);
    fwrite(&details, sizeof(struct carData), 1, in);
    printf("File written succesfully\n\n");
    }
    printf("Enter Car Number<1-100; 0 - back to main screen>: ");
    scanf("%d", &details.carNum);
    }

    fclose(in);

    }

    }

    char Delete(carDataPtr *sPtr)
    {
    char delNum;
    carDataPtr prev, curr, temp;

    printf("Enter Car Number to delete<1-100>: ");
    scanf("%c", &delNum);

    if(delNum == (*sPtr)->carNum) {
    temp = *sPtr;
    *sPtr = (*sPtr)->next;
    free(temp);
    printf("Car Number %c has been deleted\n", delNum);
    }
    else {
    prev = *sPtr;
    curr = (*sPtr)->next;

    while(curr != NULL && curr->carNum != delNum) {
    prev = curr;
    curr = curr->next;
    }

    if(curr != NULL) {
    temp = curr;
    prev->next = curr->next;
    free(temp);
    printf("Car Number %c has been deleted\n", delNum);
    }
    }

    return '\0';

    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Cannot delete

    Dear Unregistered Guest

    Please can you supply the dat file that this program access.

    I have compiled using Borland's C++ V 3 DOS and got a warning about 'in' being assigned a value that is never used.

    When I run the programme it aborts at the file open stage because I do not have the file.

    FILE *in;
    //struct carData details = {0, "", "", 0, 0.0, "", 0};
    carDataPtr details = NULL;

    if((in = fopen("cardetails.", "rb+")) == NULL)
    {
    printf("File <cardetails.dat> cannot be opened.\n");
    exit(1);
    }

    Have you managed to open the file using the code supplied ?

    Wait to hear

    Stephanos

    ([email protected])

Popular pages Recent additions subscribe to a feed