Re: Why won't this code work?

Discussion in 'C Programming' started by Joona I Palaste, Aug 19, 2003.

  1. Eirik <> scribbled the following:
    > <BADLY DESIGNED C CODE>


    Syntax error. A C statement can't begin with the < token.

    > #include <stdio.h>


    > void combo(int *, int *, int *, int);
    > void div(int *, int *, float *);


    > int main(int argc, char *argv[])
    > {
    > int n1;
    > int n2;
    > char type[100];
    > int result;
    > float dres;


    > printf("Number 1.\n");
    > scanf("%d", &n1);


    > printf("Number 2.\n");
    > scanf("%d", &n2);


    > printf("Addition, subtraction, multiplication, division.\n");
    > gets(type);


    Never use gets, unless you're Dan Pop, or know for sure no one will
    ever overflow your buffer. Check out fgets, or that ggets thingy
    that keeps getting mentioned here.

    > if(type=="addition")


    Typical newbie mistake. The == operator doesn't know about the
    contents of the strings, it compares the raw memory locations.
    Check out strcmp.

    > {
    > combo(&n1, &n2, &result, 1);
    > printf("%d + %d = %d\n", n1, n2, result);
    > }
    > if(type=="subtraction")


    Likewise.

    > {
    > combo(&n1, &n2, &result, 2);
    > printf("%d - %d = %d\n", n1, n2, result);
    > }
    > if(type=="multiplication")


    Likewise.

    > {
    > combo(&n1, &n2, &result, 3);
    > printf("%d * %d = %d\n", n1, n2, result);
    > }
    > if(type=="division")


    Likewise.

    > {
    > div(&n1, &n2, &dres);
    > printf("%d / %d = %f\n", n1, n2, dres);
    > }
    > return 0;
    > }


    > void combo(int *nO, int *nT, int *R, int m)
    > {


    Check out the switch statement, it saves some space here.

    > if(m == 1)
    > {
    > *R = *nO + *nT;
    > }
    > if(m==2)
    > {
    > *R = *nO - *nT;
    > }
    > if(m==3)
    > {
    > *R = *nO * *nT;
    > }
    > }


    > void div(int *nO, int *nT, float *R)
    > {
    > *R = (float) *nO / *nT;
    > }


    Remember to check for a divide by zero.

    > </BADLY DESIGNED C CODE>


    Another syntax error.

    > Explanation of silly variable names:
    > *R = Result
    > *nO = Number One
    > *nT = Number Two
    > m = Method
    > n1 = Number 1
    > n2 = Number 2
    > dres = Division Result


    > Stupid function names:
    > combo = Combination of add, subtract and multiply
    > div = Division


    > I've tried using scanf() instead of gets, but that doesn't work either.
    > It compiles with no errors or warnings whatsoever when I don't use -Wall.
    > The problem is that the program stops when it is supposed to ask me
    > if I want it to add, subtract, multiply or divide.
    > I have tried these approaches:


    > 1 "char *type;"&"scanf("%s", &type);"
    > 2 "char type[100];"&"scanf("%s", &type);"
    > 3 "char type[100];"&"scanf("%c", &type);"
    > 4 "char *type;"&"gets(type);"
    > 5 "char type[100];"&"gets(type);"


    Forget approaches 1 and 4. scanfing into a char *, that you haven't
    malloced, is bad. scanfing into a char[100] is much better. But scanf
    is a tricky function to use anyway.

    > gcc calc.c -o calc -Wall produces the following output:


    > 1 warning: char format, pointer arg (arg 2)
    > 2 warning: char format, different type arg (arg 2)
    > 3 warning: char format, different type arg (arg 2)
    > 4 : the 'gets' function is dangerous and should not be used.
    > 5 : the 'gets' function is dangerous and should not be used.


    gets should not be used, because it provides *NO WAY WHATSOEVER* to
    prevent undefined behaviour (could result in a segmentation fault) if
    someone writes more text than your buffer can hold.

    > I use Mandrake GNU/Linux 9.1 with gcc 3.3 and glibc 2.3.1.


    Irrelevant.

    > I hope this question is not to stupid. Flame me if you want at
    > . Replies are also welcome at the _VERY_ same
    > address!


    > Eirik


    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
    and vow excitedly zooming."
    - JIPsoft
    Joona I Palaste, Aug 19, 2003
    #1
    1. Advertising

  2. Arthur J. O'Dwyer <> scribbled the following:

    > On Tue, 19 Aug 2003, Joona I Palaste wrote:
    >>
    >> > printf("Addition, subtraction, multiplication, division.\n");
    >> > gets(type);

    >>
    >> Never use gets, unless you're Dan Pop, or know for sure no one will
    >> ever overflow your buffer. Check out fgets, or that ggets thingy
    >> that keeps getting mentioned here.


    > Dan Pop doesn't use gets(); he uses scanf().


    Oops, I stand corrected. Looks like gets() is so bad that even Dan Pop
    can't use it correctly. Not even if he over-engages his brain... =)

    > Different degree of
    > wing-walking altogether -- scanf() is tricky, but gets() is downright
    > deadly. Using scanf(), one might write


    > scanf("%99[^\n]", type);


    > (with maybe some whitespace before or after the specifier; I'm not
    > sure of how whitespace goes in those format strings).


    This would not accept more than 99 characters of input, yesno? Any
    further input would be discarded and sent to the bit bucket?

    >> > void div(int *nO, int *nT, float *R)
    >> > {
    >> > *R = (float) *nO / *nT;
    >> > }

    >>
    >> Remember to check for a divide by zero.


    > Use 'double' instead of 'float', too.
    > 'float' is only good for confusing the
    > maintainer.


    >> > 5 : the 'gets' function is dangerous and should not be used.

    >>
    >> gets should not be used, because it provides *NO WAY WHATSOEVER* to
    >> prevent undefined behaviour (could result in a segmentation fault) if
    >> someone writes more text than your buffer can hold.
    >>
    >> > I use Mandrake GNU/Linux 9.1 with gcc 3.3 and glibc 2.3.1.

    >>
    >> Irrelevant.


    > (Hey, remember that thread about sscanf() where it turned out
    > that some platforms' libc had buggy scanf() implementations?
    > Maybe it *is* relevant, for this sort of thing... ;-))


    Not necessarily. Having been in Lapland for a week and a half, I
    might have missed that thread.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "I am not very happy acting pleased whenever prominent scientists overmagnify
    intellectual enlightenment."
    - Anon
    Joona I Palaste, Aug 19, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marina

    Re: WHY, WHY WON'T IT WORK???

    Marina, Jun 29, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    344
    Marina
    Jun 29, 2004
  2. Chad
    Replies:
    4
    Views:
    8,314
  3. learningjava
    Replies:
    8
    Views:
    466
    John C. Bollinger
    Dec 12, 2003
  4. John Bode

    Re: Why won't this code work?

    John Bode, Aug 20, 2003, in forum: C Programming
    Replies:
    2
    Views:
    358
    Dave Thompson
    Sep 1, 2003
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,766
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page