Using fgets() within a function returning an error.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aomighty

    #1

    Using fgets() within a function returning an error.

    Hi, I've been creating a simple calculator program that asks whether
    you want to add, subtract, multiply or divide, asks you for input, and
    then performs the calculation. Things seem to be going well enough, but
    I've run into one problem. It seems when I use fgets() or gets() within
    a function I wrote, it gives me no errors, but when run, doesn't pause
    for input, but skips past it. I've tried running it inside main() and
    it runs fine. Compiled using gcc 3.4.3.

    Here's the code snippet involved. It's in a header file which is
    included from main.c. If you run it, I believe that's what will happen.[color=blue]
    >From main, it simply calls add with add().[/color]

    char bigbuff[101];

    /* The rest of the code.*/

    void add(void)
    {
    printf("What numbers do you wish to add?\n");
    setupinput();
    fgets(bigbuff, sizeof(bigbuff) , stdin);
    }

    Thanks all.

  • Michael Mair

    #2
    Re: Using fgets() within a function returning an error.

    Aomighty wrote:[color=blue]
    > Hi, I've been creating a simple calculator program that asks whether
    > you want to add, subtract, multiply or divide, asks you for input, and
    > then performs the calculation. Things seem to be going well enough, but
    > I've run into one problem. It seems when I use fgets() or gets() within
    > a function I wrote, it gives me no errors, but when run, doesn't pause
    > for input, but skips past it. I've tried running it inside main() and
    > it runs fine. Compiled using gcc 3.4.3.[/color]

    Best forget that gets() exists.
    Note: fgets() is not necessarily the best function for the job, either
    -- unfortunately, the standard library contains no good function for
    that job.
    [color=blue]
    > Here's the code snippet involved. It's in a header file which is
    > included from main.c. If you run it, I believe that's what will happen.[color=green]
    >>From main, it simply calls add with add().[/color][/color]

    Please give us a fighting chance: Strip your code down to a minimal
    example i.e. the minimum program that still exhibits your problem.
    Throwing us a snippet which may or may not contain the _actual_
    error is not helpful.
    BTW: function definitions are _not_ for header files.
    [color=blue]
    > char bigbuff[101];[/color]

    Unnecessary magic number:
    #define INPUTSTRLEN 100
    char bigbuff[inputlen + 1];
    [color=blue]
    > /* The rest of the code.*/
    >
    > void add(void)
    > {[/color]

    Note: This interface tells me that the function relies on global
    data. This is an indicator for not-so-good (vulgo: bad) design.
    [color=blue]
    > printf("What numbers do you wish to add?\n");
    > setupinput();[/color]

    What does setupinput() do? Can the error be there?
    Is it possible that you leave some '\n' out there on
    stdin?
    [color=blue]
    > fgets(bigbuff, sizeof(bigbuff) , stdin);[/color]

    sizeof bigbuff is sufficient.
    You did not check
    - the return value of fgets()
    - call ferror() to find out whether there were problems

    You may want to check strlen(bigbuff) and maybe fputs(bigbuff, stdout)
    to start finding the error.

    Back to setupinput(): It should contain something along the
    lines of
    int c;
    while ((c = getchar()) != EOF)
    if (c == '\n')
    break;
    [color=blue]
    > }
    >
    > Thanks all.[/color]

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    Working...