Thread: Avoid duplicate input issue

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Avoid duplicate input issue

    I'm creating a system for my school assignment. In order to create a file, I need to input data.. The data input cannot be same if it already exist in the record. How to avoid it? Please correct my code.. Thanks..

    Code:
    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);
         
        // avoid duplicate input
         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();
         
    }

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    This doesn't create a file. It merely creates a pointer to a file descriptor.
    Code:
    FILE *flightFile; // create a file
    This could create a file, but you should check for it.
    Code:
    flightFile=fopen("flights.dat","a+");
    Don't use fflush() on input streams. What it does is only defined for output streams.
    Code:
    fflush(stdin);
    Code:
    if (flightNo == flightNo)
    The following code makes just about as much sense as yours.
    Code:
    // check if this program is running in our Universe
    if( !( 1 == 1 ) )
    {
        panic();
    }

    To check if an entry doesn't already exist, you need to build a list of existing entries, and then check all new entries against that list. It is not the best solution, but it is easily doable and very likely what is expected of you.

    The advantage of this approach would be that you only need to read the input file once. You can add as many new entries as needed after that. As long as you remember to add the new entries to the list of existing entries in order to correctly check consecutive new entries, that is.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Avoid duplicate input issue

    could you please correct the coding for me? :/

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I could and I won't. This is your assignment.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    I've been thinking since morning and it's due tomorrow.. Thanks anyway..

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Anyone else is willing to help me.. please..

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. invalid input
    By sworc66 in forum C Programming
    Replies: 8
    Last Post: 11-11-2003, 05:41 AM
  5. input validation - new to programming
    By bigzeppelin2k in forum C Programming
    Replies: 2
    Last Post: 10-31-2003, 06:44 PM