Thread: Comparing two elements in different arrays of structs?

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

    Comparing two elements in different arrays of structs?

    I have two files, `cars.txt` and `reservation.txt`, both of the files have `resID` in common.


    I want the user to enter a date, use that date to see the cars that are **unavailable**, then print them (through `resID`).


    car.txt:
    (From left to right: `reservationID`, `carID`, `carYOM`, `carMake`, `carModel`, `carFuel`, `catagory`.)


    Code:
        R001;V001;2003;Toyota;Camry;Petrol;Budget
        R002;V002;2005;Toyota;Prius;Petrol;Economy
        R003;V003;1999;Ford;Falcon;Petrol;Midsize
        R004;V004;2007;Ford;Territory;Diesel;Fullsize
        R005;V005;2010;Ferrari;599;Petrol;Fullsize
        R006;V006;1998;Holden;Comadore;Diesel;Midsize
        R007;V007;2008;Honda;Civic;Petrol;Budget
        R008;V008;2000;Mazda;MX5;Petrol;Economy

    reservation.txt:
    (From left to right: `reservationID`, `customerID`, `reservationStartDate`, `reservationStartTime`, `reservationEndDate`, `reservationEndTime`.)


    Code:
        R001;C005;2012/02/12;09:15A.M;2012/03/15;05:00P.M
        R002;C002;2012/04/15;10:00A.M;2012/04/22;10:30A.M
        R003;C003;2012/01/16;02:11P.M;2012/04/15;12:00P.M
        R004;C004;2012/05/05;03:00P.M;2012/05/08;10:40A.M
        R005;C005;2012/05/15;10:00A.M;2012/04/23;05:00P.M
        R006;C006;2012/04/11;05:30P.M;2012/04/15;10:00A.M
        R007;C008;2012/05/15;03:15P.M;2012/05/18;11:00A.M
        R008;C007;2012/04/15;11:40P.M;2012/04/23;09:00A.M

    The code in question:

    Code:
        #include <stdio.h>
        #include <string.h>
        #define MAX_CAR 100
        #define MAX_RES 100
        
        int main(){
            
            typedef struct{                    //car struct
                char reservationID[20];
                char carID[20];
                char carYOM[20];
                char carMake[20];
                char carModel[50];
                char carFuel[20];
                char catagory[20];
            } car_t;
            
            typedef struct{                    //res struct
                char reservationID[20];
                char customerID[20];
                char reservationStartDate[20];
                char reservationStartTime[20];
                char reservationEndDate[50];
                char reservationEndTime[20];
            } res_t;
            
            car_t car[MAX_CAR];                //car array
            res_t reservation[MAX_RES];        //res array
            FILE *carHandle;
            FILE *resHandle;
            char line[100];
            char *item;
            int rescount = 0;
            int carcount =0;
            int k;
            int i;
            int option;
            char choice[20];    
            
            resHandle = fopen("reservation.txt","r");    
            
            while (fgets(line, 99, resHandle)){
              //cut up the reservation file line by line and put the bits into the res array.
                item = strtok(line,";");
                strcpy(reservation[rescount].reservationID,item);
                item = strtok(NULL,";");
                strcpy(reservation[rescount].customerID,item);
                item = strtok(NULL,";");
                strcpy(reservation[rescount].reservationStartDate,item);
                item = strtok(NULL,";");
                strcpy(reservation[rescount].reservationStartTime,item);
                item = strtok(NULL,";");
                strcpy(reservation[rescount].reservationEndDate,item);
                item = strtok(NULL,"\n");
                strcpy(reservation[rescount].reservationEndTime,item);
                rescount++;
            }
            
            fclose(resHandle);
            
            carHandle = fopen("car.txt","r");    
            
            while (fgets(line, 99, carHandle)){
                //cut up the car file line by line and put the bits into the car array.
                item = strtok(line,";");
                strcpy(car[carcount].reservationID,item);
                item = strtok(NULL,";");
                strcpy(car[carcount].carID,item);
                item = strtok(NULL,";");
                strcpy(car[carcount].carYOM,item);
                item = strtok(NULL,";");
                strcpy(car[carcount].carMake,item);
                item = strtok(NULL,";");
                strcpy(car[carcount].carModel,item);
                item = strtok(NULL,";");
                strcpy(car[carcount].carFuel,item);
                item = strtok(NULL,"\n");
                strcpy(car[carcount].catagory,item);
                carcount++;
            }
            
            fclose(carHandle);
            
            printf("Enter todays date (in YYYY/MM/DD format):");
            scanf("%s", choice);
            for (k=0;k<=rescount; k++){
                if (strcmp(choice,reservation[k].reservationStartDate)>=0 && strcmp(choice,reservation[k].reservationStartDate)>=0){
                    for (i=0;i<=carcount; i++){
                        if (strcmp(car[i].reservationID,reservation[i].reservationID)==0){
                            printf("\nreservationID: %s\nreservationStartTime: %s\ncustomerID: %s\ncarid: %s\nyom: %s\nmake: %s\nmodel: %s\nfueltype: %s\ncategory: %s\n\n", car[k].reservationID, reservation[i].reservationStartTime, reservation[i].customerID, car[k].carID, car[k].carYOM, car[k].carMake, car[k].carModel, car[k].carFuel, car[k].catagory);
                            goto outofloop;
                        }
                    }
                }else printf("\nall the cars are available\n");
                break;
            }
            outofloop:
        
            return(0);
        }

    Currently the code only works with input strings '2012/02/12' to '2012/03/15' (i.e the first line of the reservation file)


    Any other input string will not get past the first if statement.


    Any help would be very appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You've already got a thread making progress here -> Foreign Keys Using Array Of Structs In C. - C And C++ | Dream.In.Code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    Quote Originally Posted by Salem View Post
    You've already got a thread making progress here -> Foreign Keys Using Array Of Structs In C. - C And C++ | Dream.In.Code
    Heh, small world

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing Elements of 2 arrays
    By SupraMan in forum C Programming
    Replies: 28
    Last Post: 11-23-2010, 02:43 PM
  2. comparing vector elements
    By l2u in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2008, 12:23 PM
  3. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-14-2007, 12:20 AM
  4. Comparing 2 elements from 2 different arrays
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 11-15-2006, 02:43 PM
  5. Comparing array elements
    By Tride in forum C Programming
    Replies: 8
    Last Post: 09-13-2003, 12:10 PM