Thread: Can someone please help deadline tomorow

  1. #46
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Scott, please post up your most current code, and attach the input file that you're using to test it with.

    The sooner, the better.

  2. #47
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I blame myself for not being clearer and more helpful in the first place.

    There are two ways you can handle what you call "error handling," scott. In the first place, these aren't errors, they are failed searches.

    If you let the user start a new search after a failed search, he would have to select the option again, provide new input, and so forth. This is the easiest way to handle a failed search.

    If you let the computer restart the search, it is more work.

    Code:
    Prompt for and get searchtitle
    
    while still searching:
       i = 0
       N = library size
       while i < N:
          if library[i].title matches searchtitle:
             stop searching
          else:
             i++
          endif
       endwhile
    
       if still searching:
          Prompt for and get searchtitle
       endif
    endwhile
    As my pseudo code implies, it is a lot easier to search when the entire library is in RAM and you don't have to keep rereading files. So, that is an important optimization to be made, because otherwise you would have to reread the whole file for every iteration of the outer loop. Lots of people missed this fact.

    Sorry. Hopefully things are clear now.

    I was bored, so I took time to implement this in C. If we continue to have problems I will post parts that I think will help.

  3. #48
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I am an idiot. It happens when you don't actually code things and just give suggestions. There was a flow isue in what I posted - it was only comparing one title before deciding it was not there. Here is a correction:


    I sincerely appologize for the mistake. If you post your whole code to date I will be happy to compile it and do this properly.

    The following code should (I think) solve the issue that I created ^_^
    Code:
    while (!flag)
    {
        printf("We are looking for %s\n",searchtitle);
        rewind(record_file);
        while (fread(&tmp,sizeof(record_structure),1,record_file)) 
        {
             printf("I just read this title from the input file: %s\n",tmp.Title);	
    	 if (strcmp(searchtitle,tmp.Title) == 0)
             {
    	      printf("We have a match! : \n\n\nTitle: %s \t\tAuthor: %s \t\tISBN: %s\n", tmp.Title, tmp.Author, tmp.ISBN);
    	      flag = 1;
    	      break;
              }  
        }
        if(!flag)
        {
             printf("\nThe title you entered does not exist please re-enter: ");
             scanf("%[^\n]%*c", searchtitle);
             printf("We are NOW looking for %s\n",searchtitle);
        }
            	
     }

    And yes, whiteflags solution is more efficient than this
    Last edited by KBriggs; 06-02-2010 at 05:07 PM.

Popular pages Recent additions subscribe to a feed