Thread: Can someone please help deadline tomorow

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    24

    Can someone please help deadline tomorow

    I need to implement two searches into this program, a search for an ISBN and a search for a title but i have no idea how to,o can someone please do it for me or set me off in the correct direction???

    Thanks

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define SIZE 40
    /* global variables */			
    typedef struct
    {
      char ISBN[20];
      char Author[SIZE];
      char Title[SIZE];
    }record_structure;  
    
    FILE *record_file;
    int choice;
    record_structure record_array[8];
    int num_records;
    int searchresult;
    char searchtitle[SIZE];
    
    /* function prototypes */
    void intro();
    void menu();
    void close();
    void add_book();
    void option2();
    void option3();
    void display_all_records();
    void screen_pause();
    
    /*******************************************************************************************/
    
    int main()
    {
    	intro();
    	do
    	{
    		menu();
    	}
    	while (choice!=5);
    	close();
        return 0;
    }/* end of main function */
    
    /*******************************************************************************************/
    
    void intro()
    {
    	system("cls");
    	printf("\n\n\n\n\n\n\tWelcome to Forth Valley's Collection of Technical Books");
    	screen_pause();
    }/*end of intro function*/
    
    /*******************************************************************************************/
    
    void add_book()
    {
    	record_file = fopen("C:\\bookfile.bin","wb");
        
        system("cls");
        
        printf( "\n\nAdd book details");
        
        for(num_records=0;num_records<=9;num_records++)
             {
               printf( "\n\nISBN: ");
               fflush(stdin);
               fgets(record_array[num_records].ISBN, 20, stdin);
               printf( "\n\nAuthor: ");
               fflush(stdin);
               fgets(record_array[num_records].Author, SIZE, stdin);
               printf( "\n\nTitle: ");
               fflush(stdin);
               fgets(record_array[num_records].Title, SIZE, stdin);
               printf( "------------------------------------" );
               
               fwrite(&record_array[num_records],sizeof(record_structure),1,record_file);
             
              } 
               printf( "\n\nYou have succesfully inputed the details of 8 books" );
               fclose(record_file);
        
        screen_pause();
    }/* end of dummy menu option 1 function */
    
    /*******************************************************************************************/
    
    void option2()
    {
    	system("cls");
    	printf("Search for ISBN ");
    	screen_pause();
    }/* end of dummy menu option 2 function */
    
    /*******************************************************************************************/
    
    void option3()
    {
    	printf ("Please enter the title of the book you are searching for: ");
        // Declare searchresult as int and searchisbn as string:
        searchresult = strcmp(record_array[num_records].ISBN,searchtitle); //string compare
        if (searchresult ==0)
    
    {	
    /* display details */
       printf("\n Title Author ISBN ");
       printf("\n %4i %-30s %s",num_records,record_array[num_records-1].Author,
       record_array[num_records-1].Title);	
    }
       screen_pause();
        num_records++;
    /* end of else */
    /* end of while */
    /* end of display_one_record */ 
    	
    }
    
    /*******************************************************************************************/
    
    void display_all_records()
    {
    	record_file = fopen("C:\\bookfile.bin","rb");
        
        system("cls");
        
        printf("\n\t\t\tDetails of all books");
    	printf("\n\n\n ID NO\t\tISBN\t\tAuthor\t\tTitle");
    	
    	num_records=0;
    	fread(&record_array[num_records],sizeof(record_structure),1,record_file);
    	
    	while(!feof(record_file))
    	{	
    		printf("\n%3i  %18s  %35s %55s ",num_records,record_array[num_records].ISBN,
    			record_array[num_records].Title,record_array[num_records].Author);
    		fflush(stdin);
    		num_records++;
    		fread(&record_array[num_records],sizeof(record_structure),1,record_file);
    	}
    	
    	screen_pause();
    	
        fclose(record_file);
    }
    
    /*******************************************************************************************/
    
    void close()
    {
    	system("cls");
    	printf("\n\n\n\n\n\n\tThank you for using this program");
    	screen_pause();
    }/* end of close function */
    
    /*******************************************************************************************/
    
    void menu()
    {
    	system("cls");
    	printf("\n\nMENU");
    	printf("\n\n1 - Add new entry: ");
    	printf("\n2 - Book search by ISBN: ");
    	printf("\n3 - Book search by title: ");
    	printf("\n4 - Show details off all books stored: ");
    	printf("\n5 - Exit Program:");
    	
        printf("\n\nEnter your choice ");
        scanf("%i", &choice);
        while((choice<1)||(choice>5))
        {
         printf("\n\nYou eneterd an invalid number please enter a valid number");
         printf("\n\n\tEnter your choice: ");
         scanf("%i", &choice);
         }
           
    	switch(choice)
    	{
    		case 1 : add_book();break;
    		case 2 : option2(); break;
    		case 3 : option3(); break;
    		case 4 : display_all_records() ; break;
    		case 5 :  break;
    	}
    }/* end of menu function */
    
    /*******************************************************************************************/
    
    void screen_pause()
    {
    	printf("\n\n\tPress any key to continue.....");
    	getch();
    }/* end of screen_pause function */

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
        for(num_records=0;num_records<=9;num_records++) {
    
     
                   }
                                               /* 8 books count carefully! ??? */
                 printf( "\n\nYou have succesfully inputed the details of 8 books" );
    Open the file just like in display_all_records() function.
    Read record and compare with the value to search for.
    report result(found/not found)
    close file

    You might find strcmp() and functions from string.h useful.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    24
    Something like this ???

    Code:
    {
    	record_file = fopen("C:\\bookfile.bin","rb");
    	
    	system("cls");
        
        printf ("Please enter the title of the book you are searching for: ");
        scanf("%s", &searchtitle);
    
        searchresult = strcmp(record_array[num_records].Title,searchtitle); //string compare
        if (searchresult ==0){
    
       printf("\n Title Author ISBN ");
       printf("\n %4i %-30s %s",num_records,record_array[num_records-1].ISBN,
       record_array[num_records-1].Author,record_array[num_records-1].Title);	
    }
       else {
             printf("\n\nYou eneterd an invalid Title please enter again: ");
             scanf("%s", &searchtitle);
             }
       screen_pause();
       num_records++;
       fclose(record_file);
    }

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by scott1990 View Post
    i have no idea how to,o can someone please do it for me
    That's against board policy so don't expect it to happen. Regarding your last post, you open the file, but did not read anything from it. You want to get the title to search for first, then open the file, then loop thru the file one line at a time and do the strcmp as appropriate.

    I'd recommend that before you try that, you write some code to open the file and read it line by line, separating the title from the author from the ISBN number and printing those out as you go. When then code works, you can then add the searching in.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    24
    Thanks for your reply MK27 and sorry for breaking the boards policy i should have read it before i posted my bad.

    Regarding your last suggestion, i know how to open the file but i don't know how to read it line by line, would i use a while loop to do it by any chance??

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    put a loop around a call to fgets

    fgets - C++ Reference

    and then do stuff with the result

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    For example:
    Code:
    char buffer[256]; // is this long enough?
    while (fgets(buffer,256,record_file) != NULL) {
           /* extract per line data */
    This works because at the end of the file fgets will return NULL, ending the loop according to the while condition.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    24
    Would this be what mine would look like ??

    Code:
    char searchtitle[SIZE]; 
    while (fgets(searchtitle,SIZE,record_file) != NULL) {
           /* extract per line data */

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That could be a start. The next step would be to just print the buffer out so you can see how the entire file is processed, eg:
    Code:
    char searchtitle[SIZE]; 
    while (fgets(searchtitle,SIZE,record_file) != NULL) {
           printf("%s",searchtitle);
    }
    Recognize this is simply a line (ending with \n) from the file, which I don't know how that is structured -- if there is one book per line, then you'll want to extract the data you are searching for from the line. If each field is on it's own line, eg. the file looks like:
    Code:
    title:  somebook
    author: Some Author
    ISBN:  XXX-XXX-XXXX
    then how you deal with this will be slightly more complicated, because you may have to collect all the information for the book before you can strcmp the title and possibly print the information out.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    24
    Im not seeing how this is gona fit into my program MK27, its confusing me, i just dont get it . It must be a bit too advanced for me but thanks for your help.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by scott1990 View Post
    Im not seeing how this is gona fit into my program MK27, its confusing me, i just dont get it . It must be a bit too advanced for me but thanks for your help.
    Hmmm. Well let's be honest scott -- if it seems that way it is because you have perhaps left your assignment a bit late and underestimated the amount of learning that might be required

    I might recommend you get as much as you can done by tomorrow, take it to your prof and ask for an extension so that you can finish it properly and not have to flub it.

    Anyway, I just noticed you are writing to the file this way:
    Code:
               fwrite(&record_array[num_records],sizeof(record_structure),1,record_file);
    Which is fine, it just means that file is not human readable, so the line by line of text approach is inappropriate -- BUT it does make reading the file from the program easier. You should be able to do something like this:
    Code:
    record_structure tmp;
    [...put title to search for into a string named "wanted"...]
    while (fread(&tmp, sizeof(record_structure), 1, record_file)) {
         if (strcmp(wanted,tmp.Title) == 0)  // a match
    Last edited by MK27; 05-31-2010 at 03:25 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What's your status now, Scott?

    Seems like you were quite close. What are you stuck on, and would you post your latest code?

    Let's see what's up with it.

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, don't fflush stdin. Ever.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    24
    Quote Originally Posted by Adak View Post
    What's your status now, Scott?

    Seems like you were quite close. What are you stuck on, and would you post your latest code?

    Let's see what's up with it.
    Basically just the same as before struggling with the searches, just cant seem to get it at all even with MK27's help. My latest code is in my first post.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    24
    Is tmp some sort of function? MK27
    Code:
    record_structure tmp;
    [...put title to search for into a string named "wanted"...]
    while (fread(tmp, sizeof(record_structure), 1, record_file)) {
         if (strcmp(wanted,tmp.Title) == 0)  // a match

Popular pages Recent additions subscribe to a feed