6 Replies - 624 Views - Last Post: 03 December 2010 - 12:13 PM Rate Topic: -----

#1 Eyesight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 10

Comparison problem in C

Posted 02 December 2010 - 07:59 PM

Hey, i'm new here and i have this assignment which is actually due tomorrow and i haven't figure out this problem. hope you guys can help..

Problem 1: how to add a record without duplicating it if it already exist in the file? here's my code.. but the comparison part is totally wrong..

 void addFlight(void)
{
     system("cls");
     // declare variables
     char flightNo[5];
     char way;
     char departDate[12];
     char departTime[5];
     char origin[15];
     char destination[15];
     char target[10];
     int found = 0;
     float fare;
     FILE *flightFile; // create a file
     
     // open file for writing and reading
     flightFile=fopen("flights.dat","a+");
     
     printf("\n * ADD NEW AIRLINE FLIGHT *\n\n");
     
     fflush(stdin);
     printf("\n Flight Number               : ");
     gets(flightNo);
     
    // check if record is available in the file

     while(!feof(flightFile))
     {
         fscanf(flightFile,"%[^,]%*c %c%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %f%*c \n",flightNo,&way,departDate,departTime,origin,destination,&fare);
         if (strcmp(target,flightNo) != 0)
         {
            printf("\n\n Flight Number               : \n"); 
            gets(flightNo);
         }
          else
              if (flightNo == flightNo)
              {
            printf("\n\n Flight Number is taken, enter a new Flight Number! \n");
            printf("\n\n Flight Number               : \n");
            gets(flightNo);   
              }
     }

     printf("\n One Way=O / Return=R        : ");
     scanf("%c",&way);
     fflush(stdin);
     
     printf("\n Departure Date [dd-mm-yyyy] : ");
     gets(departDate);
     fflush(stdin);
     
     printf("\n Departure Time [HH.MM]      : ");
     gets(departTime);
     fflush(stdin);
     
     printf("\n Origin                      : ");
     gets(origin);
     fflush(stdin);
     
     printf("\n Destination                 : ");
     gets(destination);
     fflush(stdin);
     
     printf("\n Ticket Fare                 : B$ ");
     scanf("%f",&fare);
     fflush(stdin);

     fprintf(flightFile,"%s, %c, %s, %s, %s, %s, %.2f \n",flightNo,way,departDate,departTime,origin,destination,fare);
     fclose(flightFile); // close the file
     
     printf("\n\n");
     system("pause");
     flightMenu();
     
} 



Problem 2: When i view records, it works fine. But then, after i edit the record, then view it again, it turns out different.. i don't know where did i go wrong..

void viewFlight(void) // to view flights
{
     system("cls");
     // declare variables
     char flightNo[20];
     char way;
     char departDate[12];
     char departTime[5];
     char origin[15];
     char destination[15];
     float fare;
     FILE *flightFile; // create a file
     
     // open file for reading only
     flightFile=fopen("flights.dat","r");
     
     printf("FLIGHT NO., WAY, DEPART DATE, DEPART TIME, ORIGIN, DESTINATION, FARE \n\n");
     
     // read all data from flight file
     while (!feof(flightFile))
     {
           // read from file
           fscanf(flightFile,"%[^,]%*c %c%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %f%*c \n",flightNo,&way,departDate,departTime,origin,destination,&fare);
           // print the file
           printf("%s, %c, %s, %s, %s, %s, B$%.2f \n",flightNo,way,departDate,departTime,origin,destination,fare);
     }
     
     fclose(flightFile); // close ticket file
     system("pause");
     flightMenu(); 
}

void editFlight(void)  // to edit flight
{
     system("cls");
     // declare variables
     char flightNo[20];
     char way;
     char departDate[12];
     char departTime[5];
     char origin[15];
     char destination[15];
     float fare;
     int found = 0;
     char target[10];
     char answer;
     char choice;
     FILE *flightFile; // create a file
     FILE *flightempFile;  // create temporary file
     
     printf("EDIT FLIGHT INFORMATION \n\n");
     
     flightFile=fopen("flights.dat","a+");
     flightempFile=fopen("flightemp.dat","a+");
     
     printf("Enter FLIGHT NO. you want to edit : ");
     fflush(stdin);
     gets(target);
     
     while(!feof(flightFile))
     { // open WHILE
       fscanf(flightFile,"%[^,]%*c %c%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %f%*c \n",flightNo,&way,departDate,departTime,origin,destination,&fare);
       if (strcmp(target,flightNo) != 0)
              
       fprintf(flightempFile,"%s, %c, %s, %s, %s, %s, %.2f \n",flightNo,way,departDate,departTime,origin,destination,fare);
       else 
       { // open statement
            found = 1;
            printf("\n\n");
            printf("\n Flight No.          : %s \n",flightNo);
            printf("\n Way [One Way/Return]: %c \n",way);
            printf("\n Departure Date      : %s \n",departDate);
            printf("\n Departure Time      : %s \n",departTime);
            printf("\n Origin              : %s \n",origin);
            printf("\n Destination         : %s \n",destination);
            printf("\n Ticket Fare         : B$%.2f \n",fare);
            printf("\n\n");
            printf("Do you want to edit the ticket? Y=Yes, N=No : ");
            scanf("%c",&answer);
            
            if (answer == 'Y' || answer == 'y')
            { // open IF
                printf("\n\n");
                printf("\n [1] Flight No.       : %s \n",flightNo);
                printf("\n [2] One Way/Return   : %c \n",way);
                printf("\n [3] Departure Date   : %s \n",departDate);
                printf("\n [4] Departure Time   : %s \n",departTime);
                printf("\n [5] Origin           : %s \n",origin);
                printf("\n [6] Destination      : %s \n",destination);
                printf("\n [7] Ticket Fare      : B$%.2f \n",fare);
                
                printf("Please choose which one to edit : ");
                scanf("%d",&choice);
                
                switch (choice)
                { // open switch
                       case 1: printf("\n\n");
                               printf("\n Flight No.     : ");
                               fflush(stdin);
                               gets(flightNo);
                               break;
                               
                       case 2: printf("\n\n");
                               printf("\n One way/Return : ");
                               fflush(stdin);
                               scanf("%c",&way);
                               break;
                               
                       case 3: printf("\n\n");
                               printf("\n Departure Date : ");
                               fflush(stdin);
                               gets(departDate);
                               break;
                               
                       case 4: printf("\n\n");
                               printf("\n Departure Time : ");
                               fflush(stdin);
                               gets(departTime);
                               break;
                               
                       case 5: printf("\n\n");
                               printf("\n Origin         : ");
                               fflush(stdin);
                               gets(origin);
                               break;
                               
                       case 6: printf("\n\n");
                               printf("\n Destination    : ");
                               fflush(stdin);
                               gets(destination);
                               break;
                               
                       case 7: printf("\n\n");
                               printf("\n Ticket Fare    : ");
                               fflush(stdin);
                               scanf("%f",&fare);
                               break;
                               
                       default: printf("\n\n Invalid Input! ");
                                break;
                } // end switch
            fprintf(flightempFile,"%s, %c, %s, %s, %s, %s, %.2f \n",flightNo,way,departDate,departTime,origin,destination,fare);
       
            } // close IF
            else
            
            if (answer == 'N' || answer == 'n')
               flightMenu();
            else
            {
                printf("\n\n Invalid Character! \n");
                system("pause");
                flightMenu();
            }
            
       } // close statement
     } // close WHILE
     
     fclose(flightempFile);
     fclose(flightFile);
    
    if (found == 1)
    {
       remove("flights.dat");
       rename("flightemp.dat","flights.dat");       
    }
    else
        remove("flightemp.dat");
    
    system("pause");  // close the file
    flightMenu();
     
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Comparison problem in C

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,575
  • Joined: 19-February 09

Re: Comparison problem in C

Posted 02 December 2010 - 08:14 PM

Hi, welcome to the site. :)
You open the file in append mode, therefore the file pointer/cursor is at the end of file.

  // open file for writing and reading
  flightFile=fopen("flights.dat","a+");


Was This Post Helpful? 1
  • +
  • -

#3 Eyesight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 10

Re: Comparison problem in C

Posted 02 December 2010 - 08:22 PM

Thank you. Umm, I don't really get it.. :/
Was This Post Helpful? 0
  • +
  • -

#4 Eyesight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-December 10

Re: Comparison problem in C

Posted 03 December 2010 - 04:20 AM

Can somebody please just help me what's the code to avoid duplicate input in this situation.. because my code doesn't work. This is to add new records. The flight number shouldn't be the same if it already exist in the file. Please correct my code for me..

void addFlight(void)
{
     system("cls");
     // declare variables
     char flightNo[5];
     char way;
     char departDate[12];
     char departTime[5];
     char origin[15];
     char destination[15];
     char target[10];
     int found = 0;
     float fare;
     FILE *flightFile; // create a file
     FILE *flightempFile;
     
     // open file for writing and reading
     flightFile=fopen("flights.dat","a+");
     printf("\n * ADD NEW AIRLINE FLIGHT *\n\n");
     
     
     // avoid duplicate input
     while(!feof(flightFile) && found == 0)
     {
          fflush(stdin);
          printf("\n Flight Number               : ");
          gets(target);
          
         fscanf(flightFile,"%[^,]%*c %c%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %f%*c \n",flightNo,&way,departDate,departTime,origin,destination,&fare);
         if (strcmp(target,flightNo) != 0) // if FN match
            {
            
            found = 1;
            }
         else // if FN match
         {
             printf("\n FILE EXIST!");
         }
     } 
     
     if (found)
     {
     system("cls");
     fflush(stdin);
     printf("\n Flight Number               : ");
     gets(flightNo);
     
     fflush(stdin);
     printf("\n One Way=O / Return=R        : ");
     scanf("%c",&way);
     fflush(stdin);
     
     printf("\n Departure Date [dd-mm-yyyy] : ");
     gets(departDate);
     fflush(stdin);
     
     printf("\n Departure Time [HH.MM]      : ");
     gets(departTime);
     fflush(stdin);
     
     printf("\n Origin                      : ");
     gets(origin);
     fflush(stdin);
     
     printf("\n Destination                 : ");
     gets(destination);
     fflush(stdin);
     
     printf("\n Ticket Fare                 : B$ ");
     scanf("%f",&fare);
     fflush(stdin);
     
     fprintf(flightFile,"%s, %c, %s, %s, %s, %s, %.2f \n",flightNo,way,departDate,departTime,origin,destination,fare);
     fclose(flightempFile);
     fclose(flightFile); // close the file
     }
     
     printf("\n\n");
     system("pause");
     flightMenu();
     
}

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Comparison problem in C

Posted 03 December 2010 - 04:44 AM

Merged duplicates. Do NOT create duplicate topics, And no...we aren't just going to "fix your code" for you. You've been given a hint by #define: open your file in READ mode with "r" rather than "a+".
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,575
  • Joined: 19-February 09

Re: Comparison problem in C

Posted 03 December 2010 - 11:25 AM

View Post#define, on 03 December 2010 - 04:14 AM, said:

You open the file in append mode, therefore the file pointer/cursor is at the end of file.

  // open file for writing and reading
  flightFile=fopen("flights.dat","a+");



My bad!

Decide to check this, when the file is opened with "a" the file pointer starts at the end of the file.

When the file is opened with "a+" the file pointer starts at the beginning of the file. If a write to the file is called, the pointer is moved to the end of the file.


Unusually for C, the strcmp() function returns 0 when the strings are equal. Positive and negative values are used to indicate which string has the greater value.

So this is wrong.
  if (strcmp(target, flightNo) != 0) // if FN match
  {
    found = 1;
  }


Was This Post Helpful? 0
  • +
  • -

#7 budfox  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 03-December 10

Re: Comparison problem in C

Posted 03 December 2010 - 12:13 PM

Write a function for your test like
int contains(FILE *f,const char *fnum)
{
  your work here
}


and then refactore your code please.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1