Can someone please help deadline tomorow

This is a discussion on Can someone please help deadline tomorow within the C Programming forums, part of the General Programming Boards category; The search is working great now MK27 thanks. Regarding fflush(stdin) my lecturer does use this but he isnt the most ...

  1. #31
    Registered User
    Join Date
    May 2010
    Posts
    24
    The search is working great now MK27 thanks. Regarding fflush(stdin) my lecturer does use this but he isnt the most experienced C user (his words) so i think i should include it to make him happy.

    Now all i've got to do is implement error handling.

    This is my attempt which doesnt work as usual lol

    Code:
    void search_by_title()
    {
    	system("cls");
    	
    	printf ("Please enter the title of the book you are searching for: ");
    	scanf("%[^\n]%*c", searchtitle);
    
    	record_file = fopen("C:\\bookfile.bin","rb");
    
    	while (fread(&tmp,sizeof(record_structure),1,record_file)) {
    		
    		if (strcmp(searchtitle,tmp.Title) == 0)
    			printf("\n\n\nTitle: %s \t\tAuthor: %s \t\tISBN: %s\n", tmp.Title, tmp.Author, tmp.ISBN);
    		
            else 
                printf("No book with this title exists in our database,please re-enter:	");
    	        scanf("%[^\n]%*c", searchtitle);
        }  
    	fclose(record_file);
    I think i need a loop so when the user enters a non existent title, it says no title exists please re-enter and it goes back to the start. I dont know how to do this though any ideas?

  2. #32
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Cool, you read my whole repost. G.J.

    I want to address fflush (stdin); before anything: well, appease your teacher, but now that you know it is wrong, don't develop a habit. In a strictly standard implementation, it's not defined to work with input streams. You can actually 'fflush' stdin by reading characters and ignoring them until you find a '\n'.

    Movin' on.

    Code:
    while (fread(&tmp,sizeof(record_structure),1,record_file)) {
    		
    		if (strcmp(searchtitle,tmp.Title) == 0)
    			printf("\n\n\nTitle: %s \t\tAuthor: %s \t\tISBN: %s\n", tmp.Title, tmp.Author, tmp.ISBN);
    		
            else 
                printf("No book with this title exists in our database,please re-enter:	");
    	        scanf("%[^\n]%*c", searchtitle);
        }
    Your if-then-else blocks wont work the way you expect. Formatted indentation is not required, so C denotes blocks with curly braces.

    Nevertheless I don't think that alone will fix the problem. Your hunch is right: it involves rewind (record_file); when you restart, and a loop around the loop that searches which continues until a successful search happens. Or, you could just let the user explicitly start a new search.

    Anyway, today is the day. If you actually read this scott, I hope you start your homework earlier in the future.

  3. #33
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    No 'tmp' is an instance of record_structure. If you are using a plain C compiler you may need to do:
    Code:
    struct record_structure tmp; // create a working variable
    bool bFoundIt = false; // create a success flag and init to false for now..
    char searchTitle[SIZE+1]; // create a buffer to hold the search string
    memset(searchTitle, 0, SIZE+1);// initialize is to zero
    strcpy(searchTitle, "My Book"); // or whatever
    //open the file in binary/read mode
    while(fread(&tmp, sizeof(struct record_structure), 0, record_file))
    {
          if(strcmp(searchTitle, tmp.Title) == 0)
          {
               // we have a winner, print out the rest of tmp's fields
               bFoundIt = true;
          }
          else
          {
               //keep searching
               // no actual code here
          }
    }
    if( bFoundIt == true)
    {
        printf("Success!\n");
    }
    else
    {
         printf("Type more carefully next time idiot\n"); // I was always hard on users.
    }
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #34
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Sorry to answer so late in the thread; to be honest it was such a joy to read a call for help from someone actually willing to try to help themselves. Regardless of the grade you get on this I would give you an A (or whatever they do these days) and expect great things from you in the future. Maybe I am just cranky but after reading all the "gimmecodez!", this is SO refreshing...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #35
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by scott1990 View Post
    I think i need a loop so when the user enters a non existent title, it says no title exists please re-enter and it goes back to the start. I dont know how to do this though any ideas?
    Place a flag inside the while loop. As whiteflags implies, you can't do this check until after the loop is finished:

    Code:
    	int found = 0;  // boolean flag   
    [....]
    	while ((fread(&tmp,sizeof(record_structure),1,record_file))) {
    		if (strcmp(searchtitle,tmp.Title) == 0) {
    			printf("Title: %s Author %s ISBN %s\n", tmp.Title, tmp.Author, tmp.ISBN);
    			flag = 1;
    			break;
    		}
    	}
    	fclose(record_file);
    	if (!flag) // no title was found
    Quote Originally Posted by scott1990 View Post
    Regarding fflush(stdin) my lecturer does use this but he isnt the most experienced C user (his words) so i think i should include it to make him happy.
    Egads. Well, I guess it's fine for now. It can work, just it does not have to -- there is no "proper" way to use fflush(stdin) because the C standard refers to it as undefined behavior. Depending on your instructor's personality, it might be worthwhile to draw attention to this fact:

    Quote Originally Posted by ISO C Standard
    fflush

    If stream points to an output stream or an update
    stream in which the most recent operation was not input
    , the
    fflush function causes any unwritten data for that stream to
    be delivered to the host environment to be written to the
    file; otherwise, the behavior is undefined.
    since s/he's teaching people a bad habit -- at best, it is a way of "tricking" the compiler, and it may not produce the same results everywhere -- another compiler could just as easily do something completely different (and equally "undefined"). This is the beginning of section 7.19.5.2 in the C99 standard or 7.13.5.2 in the C89 standard.

    Of course, not everyone in authority is interested in criticism from below
    Last edited by MK27; 06-02-2010 at 09:02 AM.
    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

  6. #36
    Registered User
    Join Date
    May 2010
    Posts
    24
    Hey MK27 i've placed the flag into my code but when i type a non existent title in it just does nothing basically :S, also in your last post you've put this in the corner of your code [. . . .] what does this mean ??


    Code:
    void search_by_title()
    {
    	system("cls");
    	
    	printf ("Please enter the title of the book you are searching for: ");
    	scanf("%[^\n]%*c", searchtitle);
    
    	record_file = fopen("C:\\bookfile.bin","rb");
    
    	int flag = 0;
        
        while (fread(&tmp,sizeof(record_structure),1,record_file)) 
    		
    		if (strcmp(searchtitle,tmp.Title) == 0){
    			printf("\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: ");
        }    	
             fclose(record_file);

  7. #37
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    I am having a bit of trouble reading what your while loop is supposed to be doing because you are missing braces to go with it. Maybe you can get away with it because only the if statement is supposed to be inside it (?) but it's good to get in the habit of using braces even when you don't need to, I find.

    To clarify your bug: when you enter a nonexistent title, does it print the cue for the user to re-enter? Or does it just exit?

  8. #38
    Registered User
    Join Date
    May 2010
    Posts
    24
    Quote Originally Posted by KBriggs View Post
    I am having a bit of trouble reading what your while loop is supposed to be doing because you are missing braces to go with it. Maybe you can get away with it because only the if statement is supposed to be inside it (?) but it's good to get in the habit of using braces even when you don't need to, I find.

    To clarify your bug: when you enter a nonexistent title, does it print the cue for the user to re-enter? Or does it just exit?
    It does print the cue for the cursor to re-enter but when i type in an existent title it doesnt show anything, just goes to a newline.

  9. #39
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    That would be because there is nothing after the print line telling it to go back and search again with the new title.

    What you could do is put another while loop around all of that, like so:

    Code:
    while (!flag)
    {
           while (fread(&tmp,sizeof(record_structure),1,record_file)) 
           {		
    		if (strcmp(searchtitle,tmp.Title) == 0)
                    {
    			printf("\n\n\nTitle: %s \t\tAuthor: %s \t\tISBN: %s\n", tmp.Title, tmp.Author, tmp.ISBN);
    			flag = 1;
    			break;
                   }
           }  
    
           printf("\nThe title you entered does not exist please re-enter: ");
           [do whatever you need to do here to make the newly entered title enter the  variable searchtitle]
     }
    Then, if the search title is not found, flag will remain 0 and it will start the loop over again. You may need to rewind the file back to the beginning before you enter the second while loop. I am sorry for the non-code in brackets there, but I have only just started following the thread and i am not sure how you are storing in searchtitle, so I'll leave that detail to you.

  10. #40
    Registered User
    Join Date
    May 2010
    Posts
    24
    Quote Originally Posted by KBriggs View Post
    That would be because there is nothing after the print line telling it to go back and search again with the new title.

    What you could do is put another while loop around all of that, like so:

    Code:
    while (!flag)
    {
           while (fread(&tmp,sizeof(record_structure),1,record_file)) 
           {		
    		if (strcmp(searchtitle,tmp.Title) == 0)
                    {
    			printf("\n\n\nTitle: %s \t\tAuthor: %s \t\tISBN: %s\n", tmp.Title, tmp.Author, tmp.ISBN);
    			flag = 1;
    			break;
                   }
           }  
    
           printf("\nThe title you entered does not exist please re-enter: ");
           [do whatever you need to do here to make the newly entered title enter the  variable searchtitle]
     }
    Then, if the search title is not found, flag will remain 0 and it will start the loop over again. You may need to rewind the file back to the beginning before you enter the second while loop. I am sorry for the non-code in brackets there, but I have only just started following the thread and i am not sure how you are storing in searchtitle, so I'll leave that detail to you.
    Hi KBriggs i have made your changes but i just feel i've not entered it correctlycan you look over it please.

    Code:
    while (!flag)
    {
        while (fread(&tmp,sizeof(record_structure),1,record_file)) {
    		
    		if (strcmp(searchtitle,tmp.Title) == 0){
    			printf("\n\n\nTitle: %s \t\tAuthor: %s \t\tISBN: %s\n", tmp.Title, tmp.Author, tmp.ISBN);
    			flag = 1;
    			break;
        }  
               printf("\nThe title you entered does not exist please re-enter: ");
               scanf("%[^\n]%*c", searchtitle);
            	
        }    
    }     
             fclose(record_file);

  11. #41
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    If it does not seem to be reading the file over the second time, you will have to rewind it to the beginning each iteration of the while(!flag) loop.

    Code:
    while (!flag)
    {
        rewind(record_file);
        while (fread(&tmp,sizeof(record_structure),1,record_file)) {
    		
    		if (strcmp(searchtitle,tmp.Title) == 0){
    			printf("\n\n\nTitle: %s \t\tAuthor: %s \t\tISBN: %s\n", tmp.Title, tmp.Author, tmp.ISBN);
    			flag = 1;
    			break;
        }  
               printf("\nThe title you entered does not exist please re-enter: ");
               scanf("%[^\n]%*c", searchtitle);
            	
        }    
    }     
             fclose(record_file);
    Try that, and see if it works.


    OOPS: Sorry I made a mistake. You do need that second if, because otherwise if will ask you to enter a new search title regardless of whether it found one already: here is the correction:

    Code:
    while (!flag)
    {
        rewind(record_file);
        while (fread(&tmp,sizeof(record_structure),1,record_file)) {
    		
    		if (strcmp(searchtitle,tmp.Title) == 0){
    			printf("\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);
            } 
    }     
             fclose(record_file);
    Last edited by KBriggs; 06-02-2010 at 06:06 PM.

  12. #42
    Registered User
    Join Date
    May 2010
    Posts
    24
    I've tried your suggestion KBriggs but no matter what i enter it just says "The title you entered does not exist please re-enter" and it just keeps looping through that.

  13. #43
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Did you see my correction to the code I posted in the post above this?

  14. #44
    Registered User
    Join Date
    May 2010
    Posts
    24
    Quote Originally Posted by KBriggs View Post
    Did you see my correction to the code I posted in the post above this?
    Yes this is the code i am using, thanks for your help btw

  15. #45
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Hum...

    Well if it is never finding a title match there may be something wrong with the way you are reading in your titles.

    Trying printing out searchtitle, and the title in the strcture that you expect it to match, right after you read it in and see what it says. If it is never matching then there must be some difference between them, and printing them both out and looking is probably the best way to find out what ^_^
    Last edited by KBriggs; 06-02-2010 at 04:19 PM.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21