-3
struct info
{
    float cond_cost, vat,insure_total, insure_cost;
    char insure_name[10],insure_cond[10], registration[10], date[10],surname[20],name[20],car_num[10];
    int num;
};  


displayinfo()//this function searches for the record using the registration number
{
    char carnum[10];
    int foundit;
    struct info i={0.0,0.0,0.0,0.0,"","","","","","","",0};
    system("cls");

    foundit = 0;

    FILE *insure;
    if((insure= fopen("insurance.txt","r"))==NULL)
    {
        printf("File cannot be opened");
        system("pause");
    }
    else
    {
        FILE *reg;
        if((reg= fopen("search.txt","a"))==NULL)
        {
            printf("File cannot be opened");
            system("pause");
        }
        else
        {
            printf("Enter car registration number: ");
            scanf("%s",carnum);

            while(!feof(reg))  
            {           
                fread(&i,1,sizeof(struct info),insure);
                if ((strcmp(carnum,i.registration)==0))
                {
                    //this section will print the searched record via registration number
                    printf("|---------------------------------|\n");
                    printf("|            INSURANCE            |\n");
                    printf("|---------------------------------|\n");
                    printf("| Car Registration number    :%s\n", i.registration);
                    printf("| Insurance holder first name:%s\n", i.name);
                    printf("| Insurance holder last name :%s\n", i.surname);
                    printf("| Insurance car covered      :%s\n", &i.insure_name);
                    printf("| Insurance car coverage cost:$%7.2f\n", i.insure_cost);
                    printf("| Car class                  :%s\n", i.insure_cond);
                    printf("| Car class cost             :$%7.2f\n", i.cond_cost);
                    printf("| Vat                        :$%7.2f\n", i.vat);
                    printf("| Total insurance cost       :$%7.2f\n", i.insure_total);
                    printf("| Date                       :%s\n", i.date);
                    printf("|---------------------------------|\n");
                    foundit=1;
                    system("pause");
                    break;
                }//endif

                if (foundit !=1)
                {
                     printf("Record cannot be found\n");
                     system("pause");
                     return 0;
                }                               
            }//ends while loop

            fclose(reg);
            fclose(insure);                     
        }
    }
}

The registration number is used to find the record of the person's first and last name, car, car cost, car class etc. But all of the variables are printed except the surname variable.

pzp
  • 6,249
  • 1
  • 26
  • 38

1 Answers1

0

This example reads and writes your structs from file and can perform the matching string. You see there is no problem with printing the surname.

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

struct info {
    float cond_cost, vat, insure_total, insure_cost;
    char insure_name[10], insure_cond[10], registration[10], date[10], surname[20], name[20], car_num[10];
    int num;
};

char carnum[10];

struct info i = {0.0, 0.0, 0.0, 0.0, "", "", "", "", "", "", "", 0};

FILE *fp;

int main() {
    struct info insurances[2];

    fp = fopen("output.dat", "ab");

    for (int i = 0; i < 2; i++) {
        printf("Enter name:");
        scanf("%s", insurances[i].name);
        printf("Enter surname:");
        scanf("%s", insurances[i].surname);
        printf("Enter registration number");
        scanf("%s", insurances[i].registration);
    }

    fwrite(insurances, sizeof(struct info), 2, fp);
    fclose(fp);

    fp = fopen("output.dat", "rb");
    fread(insurances, sizeof(struct info), 2, fp);

    printf("Enter car registration number: ");
    scanf("%s", carnum);

    for (int i = 0; i < 2; i++) {
        if ((strcmp(carnum, insurances[i].registration) == 0)) {
            printf("|---------------------------------|\n");
            printf("|            INSURANCE            |\n");
            printf("|---------------------------------|\n");
            printf("| Car Registration number    :%s\n", insurances[i].registration);
            printf("| Insurance holder first name:%s\n", insurances[i].name);
            printf("| Insurance holder last name :%s\n", insurances[i].surname);
            printf("| Insurance car covered      :%s\n", insurances[i].insure_name);
            printf("| Insurance car coverage cost:$%7.2f\n", insurances[i].insure_cost);
            printf("| Car class                  :%s\n", insurances[i].insure_cond);
            printf("| Car class cost             :$%7.2f\n", insurances[i].cond_cost);
            printf("| Vat                        :$%7.2f\n", insurances[i].vat);
            printf("| Total insurance cost       :$%7.2f\n", insurances[i].insure_total);
            printf("| Date                       :%s\n", insurances[i].date);
            printf("|---------------------------------|\n");
        }
    }
}


FILE *fp;

int main2()//this function searches for the record using the registration number
{
    char carnum[10];
    int foundit;
    struct info i = {0.0, 0.0, 0.0, 0.0, "Superman", "A", "ABC123", "Sunday", "Kent", "Clark", "SuperCar", 0};
    fp = fopen("output.dat", "ab");
    fwrite(&i, sizeof(struct info), 1, fp);
    fclose(fp);

    system("cls");

    foundit = 0;

    FILE *insure;
    if ((insure = fopen("output.dat", "r")) == NULL) {
        printf("File cannot be opened");
        system("pause");

    }
    else {
        FILE *reg;
        if ((reg = fopen("search.txt", "a")) == NULL) {
            printf("File cannot be opened");
            system("pause");
        }
        else {

            printf("Enter car registration number: ");
            scanf("%s", carnum);

            while (!feof(reg)) {
                fread(&i, 1, sizeof(struct info), insure);


                printf("Name %s\n", i.name);
                //printf("%d", data[i].age)


                if ((strcmp(carnum, i.registration) == 0)) {

                    // fread(data, sizeof(struct chris), 10, fp);
                    ;

                    //this section will print the searched record via registration number
                    printf("|---------------------------------|\n");
                    printf("|            INSURANCE            |\n");
                    printf("|---------------------------------|\n");
                    printf("| Car Registration number    :%s\n", i.registration);
                    printf("| Insurance holder first name:%s\n", i.name);
                    printf("| Insurance holder last name :%s\n", i.surname);
                    printf("| Insurance car covered      :%s\n", i.insure_name);
                    printf("| Insurance car coverage cost:$%7.2f\n", i.insure_cost);
                    printf("| Car class                  :%s\n", i.insure_cond);
                    printf("| Car class cost             :$%7.2f\n", i.cond_cost);
                    printf("| Vat                        :$%7.2f\n", i.vat);
                    printf("| Total insurance cost       :$%7.2f\n", i.insure_total);
                    printf("| Date                       :%s\n", i.date);
                    printf("|---------------------------------|\n");
                    foundit = 1;
                    system("pause");
                    break;
                }//endif

                if (foundit != 1) {
                    printf("Record cannot be found\n");
                    system("pause");
                    return 0;
                }
            }//ends while loop

            fclose(reg);
            fclose(insure);
        }
    }
}

Test

Enter name:Donald
Enter surname:Duck
Enter registration numberABC123
Enter name:Winnie
Enter surname:Pooh
Enter registration numberQWE234
Enter car registration number: ABC123
|---------------------------------|
|            INSURANCE            |
|---------------------------------|
| Car Registration number    :ABC123
| Insurance holder first name:Donald
| Insurance holder last name :Duck
| Insurance car covered      :
| Insurance car coverage cost:$   0.00
| Car class                  :
| Car class cost             :$   0.00
| Vat                        :$   0.00
| Total insurance cost       :$136716115968.00
| Date                       :
|---------------------------------|

Process finished with exit code 0
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424