I have a bug with fgets that I can't figure out

This is a discussion on I have a bug with fgets that I can't figure out within the C Programming forums, part of the General Programming Boards category; The code is located at: C code - 79 lines - codepad The problem I'm having is in askUserForAmount(); http://i.imgur.com/Ic4XcXJ.png ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    I have a bug with fgets that I can't figure out

    The code is located at: C code - 79 lines - codepad

    The problem I'm having is in askUserForAmount();

    http://i.imgur.com/Ic4XcXJ.png is my output.

    I use "Title here" for title.

    I use "Description Here" for description.

    I use the example for numeric amount (12.5)

    Then when I hit enter, it prints out "Known Quantity Types include:"
    and "Please enter the quantity type for this item:"

    And instead of waiting for my input, it goes onto display my qtype incorrectly as 5, and then display my amount and qtype.

    Amount is apparently "12." and qtype is "5".

    Why is this happening? What's in the 12.5 string that breaks this? If I do 12 instead of 12.5, it asks me for the quantity type correctly. BUT, if I have any spaces in quantity type (ex: fl oz), the fgets treats the space as a new line(?) and drops the oz.

    I must be doing something wrong, but can't figure out what. Thank you for any help.

    Output with broken quantity type http://i.imgur.com/Xvs52Wn.png

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,067
    Quote Originally Posted by kbfirebreather View Post
    And instead of waiting for my input, it goes onto display my qtype incorrectly as 5, and then display my amount and qtype.
    I think it's better to paste your code here. Anyway, it looks like a buffer overflow problem. The problem is that sizeof works in the way you are using it only in the same scope of the array. You can see this clearly by trying the following:

    Code:
    void how_big_is_it(char *arr) {
        printf("%d\n", sizeof(arr));
    }
    
    int main(void) {
       char buf[100];
       printf("%d\n", sizeof(buf));  // prints 100
       how_big_is_it(buf);            // prints sizeof(void*)
       return 0;
    }
    EDIT: In other words, the other function doesn't "know" how big the array is. The solution is to modify your function to take in this information as extra parameters. E.g. you should make this call instead:
    Code:
     askUserForAmount(amount, amount_numeric, sizeof(amount_numeric), qtype, sizeof(qtype));
    Last edited by c99tutorial; 01-27-2013 at 10:38 AM.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Thank you! I can't believe I spend 12 - 3 last night and didn't figure this out. Rusty Rusty C. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't figure this out
    By Roflapiggy in forum C Programming
    Replies: 0
    Last Post: 02-19-2011, 07:05 PM
  2. Cant figure it out...
    By FingerPrint in forum C++ Programming
    Replies: 6
    Last Post: 09-19-2006, 03:39 PM
  3. Cant figure it out.
    By Coder87C in forum C++ Programming
    Replies: 6
    Last Post: 05-20-2005, 01:47 PM
  4. can't figure it out HELP
    By cemock in forum C Programming
    Replies: 14
    Last Post: 02-06-2003, 01:09 PM
  5. Can't Figure this out
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-08-2002, 03:33 PM

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