program freezes - structure searching.

This is a discussion on program freezes - structure searching. within the C Programming forums, part of the General Programming Boards category; Hello people, I'm trying to make a program that will simulate a software to control an library, look at my ...

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    program freezes - structure searching.

    Hello people, I'm trying to make a program that will simulate a software to control an library, look at my code:
    Code:
    struct book *findbook (struct book books[], int pcode) {
    	struct book *b;
    
    	for (b = books; b->code; b++)
    		if(b->code == pcode)
    			return b;
    
    	return NULL;
    		
    }
    This function is supposed to find a book (the structure) by his ID, in the case I don't find it, it returns NULL. Ok, it works perfectly when you send existents products, but when I send an inexistent product, it freezes my program. I'm calling this function from the other function:
    Code:
    void supply(void) {
    	int code, qnt;
    	struct book *bk;
    
    	system("cls");
    	printf("Enter the code of the product: ");
    	scanf("%d",&code);
    	
    	printf("Enter number of units to supply: ");
    	scanf("%d",&qnt);
    	
    	bk = findbook(books,code);
    	if(bk)
    	updtqnt(bk,qnt);
    }
    This always fail, someone can please help me?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>for (b = books; b->code; b++)
    This says that there must be a book that has it's code set to 0. Are you sure there is one? If there isn't, the program will loop into memory it doesn't own.

    A better way to control the loop would be to pass the size of the array to this function, and loop that many times.

    Or (maybe) better still, use qsort() and bsearch().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    This says that there must be a book that has it's code set to 0. Are you sure there is one? If there isn't, the program will loop into memory it doesn't own.
    There is sir the last is all set to 0.

    Hammer, all the codes are in order from 0 to N, so I should use bsearch(), hmm, actually what could be my real problem? why he don't stop when it returns NULL?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>There is sir the last is all set to 0.
    >>all the codes are in order from 0 to N
    Hmm, you mean 0 to N then 0

    If you suspect an infinte loop, you have to determine which function it's in. Are you *sure* it's in the one you have shown?
    Put some printf()'s in the for loop, just to see if it is that causing the problem. If this doesn't help, put more printf()'s in the program to help you find the error. Better still, use a debugger.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>Hmm, you mean 0 to N then 0
    Actually I mean from 1 to N then 0
    If it were 0 first, it wont loop.

    Hmm I'll put some printf's, try to break the head.
    Thank you.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>If it were 0 first, it wont loop.
    Yeah, stupid me
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Hmm, Ok, I putted some printf's on the findbook function, he loops from the first element, until N-1 (the zero, he exits, and should return NULL). he loops well in the function. Now, is this something correct to do:
    Code:
    {0,0,"","",0.0}
    My struct is:
    Code:
    struct book {
    	int code, inventory;
    	char name[50], author[50];
    	float price;
    };
    I don't see here any problem, I just think the function isn't returning NULL, well any value...
    Last edited by Vber; 03-20-2003 at 09:12 AM.

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ohh why I'm so stupid, If I have anyway at the end 0 (if he cant find any value) I just change my function to:
    Code:
    void supply(void) {
    	int code, qnt;
    	struct book *bk;
    
    	system("cls");
    	printf("Enter the code of the product: ");
    	scanf("%d",&code);
    	
    	printf("Enter number of units to supply: ");
    	scanf("%d",&qnt);
    	
    	bk = findbook(books,code);
    	if(bk->code)
    	updtqnt(bk,qnt);
    }
    Sometimes I work so hard, and the problem is a little bit easier
    Of course I don't return NULL, I return b.
    Thank you again.
    Last edited by Vber; 03-20-2003 at 09:20 AM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Of course I don't return NULL, I return b.
    Hmm, not according to your first post you don't. The code clearly says the loop stops when b->code is 0, and you then return NULL. Unless I'm missing your point?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    No you don't miss anything, I didn't explain well.
    My first idea was to return NULL, well, because I didn't found a serious problem, I did change my code to return B.

    If B will be 0 he will stop it and return the element B of the structure, in my function I'll check to see if the code of this element is 0 if is, it isn't a valid book

    the ofcourse wasn't for you, it was just to leave clear that now I'm returning B, and not NULL

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 01:39 AM
  2. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 07:18 PM
  3. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  4. Structure Array searching
    By ling in forum C Programming
    Replies: 4
    Last Post: 10-18-2001, 01:39 PM

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