big mess

Discussion in 'C Programming' started by Bill Cunningham, Feb 23, 2009.

  1. Well I read 5.10 page 114 and didn't learn a thing. I wrote some code
    the compiled with warnings. This program is supposed to take an h or NULL as
    its first argument and if there's no h as the first arg it subtacts
    decimals. If there's an h subtracts hex numbers. The ouput I get is 0.00 or
    segmentation fault. What a mess.

    #include <stdlib.h>
    #include <stdio.h>

    int main(int argc, char **argv)
    {
    int i;
    long a, b;
    double x, y;
    if (argv[1] != 'h') {
    x = strtod(argv[2], NULL);
    y = strtod(argv[3], NULL);
    printf("%.2f\n", x - y);
    } else if (argv[1] == 'h') {
    a = strtol(argv[2], NULL, 16);
    b = strtol(argv[3], NULL, 16);
    printf("%ld\n", a - b);
    }
    return 0;
    }

    embarrassing or not I would like the 'h' to show up as any argv[] whether 1
    or 3 if hex subtraction. If it's not present decimal subtraction.

    Bill
     
    Bill Cunningham, Feb 23, 2009
    #1
    1. Advertising

  2. Bill Cunningham

    Kojak Guest

    Le Sun, 22 Feb 2009 19:13:43 -0500,
    Bill Cunningham a écrit :

    > Well I read 5.10 page 114 and didn't learn a thing. I wrote some
    > code the compiled with warnings. This program is supposed to take an
    > h or NULL as its first argument and if there's no h as the first arg
    > it subtacts decimals. If there's an h subtracts hex numbers. The
    > ouput I get is 0.00 or segmentation fault. What a mess.


    Try something like this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main(int argc, char *argv[])
    {
    long a, b;
    double x, y;

    /* Insert argument checking here */

    if (!strcmp(argv[1], "h")) {
    a = strtol(argv[2], NULL, 16);
    b = strtol(argv[3], NULL, 16);
    printf("%ld\n", a - b);
    } else {
    x = strtod(argv[1], NULL);
    y = strtod(argv[2], NULL);
    printf("%.2f\n", x - y);
    }

    return EXIT_SUCCESS;
    }


    --
    Jacques.
     
    Kojak, Feb 23, 2009
    #2
    1. Advertising

  3. Bill Cunningham

    Kojak Guest

    Le Mon, 23 Feb 2009 02:10:41 +0100,
    Kojak a écrit :

    > Le Sun, 22 Feb 2009 19:13:43 -0500,
    > Bill Cunningham a écrit :
    >
    > > Well I read 5.10 page 114 and didn't learn a thing. I wrote some
    > > code the compiled with warnings. This program is supposed to take an
    > > h or NULL as its first argument and if there's no h as the first arg
    > > it subtacts decimals. If there's an h subtracts hex numbers. The
    > > ouput I get is 0.00 or segmentation fault. What a mess.

    >
    > Try something like this:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main(int argc, char *argv[])
    > {
    > long a, b;
    > double x, y;
    >
    > /* Insert argument checking here */
    >
    > if (!strcmp(argv[1], "h")) {
    > a = strtol(argv[2], NULL, 16);
    > b = strtol(argv[3], NULL, 16);
    > printf("%ld\n", a - b);
    > } else {
    > x = strtod(argv[1], NULL);
    > y = strtod(argv[2], NULL);
    > printf("%.2f\n", x - y);
    > }
    >
    > return EXIT_SUCCESS;
    > }
    >
    >


    If you don't want 'strcmp', juste remove
    #include <string.h>
    and replace
    if (!strcmp(argv[1], "h")) {
    by
    if (*argv[1] == 'h') {

    Yours,

    --
    Jacques.
     
    Kojak, Feb 23, 2009
    #3
  4. "Bill Cunningham" <> writes:
    > Well I read 5.10 page 114 and didn't learn a thing. I wrote some code
    > the compiled with warnings. This program is supposed to take an h or NULL as
    > its first argument and if there's no h as the first arg it subtacts
    > decimals. If there's an h subtracts hex numbers. The ouput I get is 0.00 or
    > segmentation fault. What a mess.


    If you get warnings, then show us the warnings. Or, better yet, fix
    them.

    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > int main(int argc, char **argv)
    > {
    > int i;
    > long a, b;
    > double x, y;
    > if (argv[1] != 'h') {


    argv[1] is a char*. Comparing it to 'h' makes no sense. In fact,
    it's a constraint violation; if the compiler accepts the code anyway,
    the behavior is undefined.

    But the most likely result is that the test will fail (or rather, that
    the "!=" test will succeed) , and you'll execute the following lines:

    > x = strtod(argv[2], NULL);
    > y = strtod(argv[3], NULL);
    > printf("%.2f\n", x - y);


    You're not checking for failures in strtod. But if argv[2] and
    argv[3] happen to point to valid strings, this should work.

    > } else if (argv[1] == 'h') {


    You've already compared argv[1] against 'h'; why repeat the test?
    Just use a simple "else".

    > a = strtol(argv[2], NULL, 16);
    > b = strtol(argv[3], NULL, 16);
    > printf("%ld\n", a - b);


    Again, you're not checking for errors -- but since this code won't be
    executed (until you fix the test), that doesn't matter yet.

    > }
    > return 0;
    > }




    > embarrassing or not I would like the 'h' to show up as any argv[] whether 1
    > or 3 if hex subtraction. If it's not present decimal subtraction.


    You say you get "0.00" or a segmentation fault -- but for *what
    inputs*? You have plenty of information; you know what warnings you
    got (and didn't bother to correct), and how you executed the program.
    Tell us.

    I suggest you get this version working (checking just argv[1]) before
    worrying about making it more flexible.

    In the meantime, what happens if you execute the above program
    like this?

    prog foo 5 3

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 23, 2009
    #4
  5. "Keith Thompson" <> wrote in message
    news:...

    [snip]

    > In the meantime, what happens if you execute the above program
    > like this?
    >
    > prog foo 5 3


    segmentation fault
     
    Bill Cunningham, Feb 23, 2009
    #5
  6. "Kojak" <> wrote in message
    news:...

    > Try something like this:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > int main(int argc, char *argv[])
    > {
    > long a, b;
    > double x, y;
    >
    > /* Insert argument checking here */
    >
    > if (!strcmp(argv[1], "h")) {
    > a = strtol(argv[2], NULL, 16);
    > b = strtol(argv[3], NULL, 16);
    > printf("%ld\n", a - b);
    > } else {
    > x = strtod(argv[1], NULL);
    > y = strtod(argv[2], NULL);
    > printf("%.2f\n", x - y);
    > }
    >
    > return EXIT_SUCCESS;
    > }
    >
    >


    If you don't want 'strcmp', juste remove
    #include <string.h>
    and replace
    if (!strcmp(argv[1], "h")) {
    by
    if (*argv[1] == 'h') {

    Yours,

    What about looping around to find an h. For example.

    sub h 3 4
    sub 3 4 h
    sub 3 h 4

    If h is in any argv I would like the program to subtract hex numbers.
    Whithout an h decimal numbers.

    Bill
     
    Bill Cunningham, Feb 23, 2009
    #6
  7. "Bill Cunningham" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >
    > [snip]
    >
    >> In the meantime, what happens if you execute the above program
    >> like this?
    >>
    >> prog foo 5 3

    >
    > segmentation fault


    That's surprising. Other than the undefined behavior of the
    (argv[1] != 'h')
    comparison, I don't see anything that should cause a segmentation
    fault. Did you get that result from the *exact* code that you posted?

    Ok, try this:

    #include <stdlib.h>
    #include <stdio.h>

    int main(int argc, char **argv)
    {
    long a, b;
    double x, y;
    if (argc != 4) {
    fputs("Usage: $0 h num num\n", stderr);
    exit(EXIT_FAILURE);
    }
    if (argv[1][0] != 'h') {
    x = strtod(argv[2], NULL);
    y = strtod(argv[3], NULL);
    printf("%.2f\n", x - y);
    } else {
    a = strtol(argv[2], NULL, 16);
    b = strtol(argv[3], NULL, 16);
    printf("%ld\n", a - b);
    }
    return 0;
    }

    I've made the following changes:

    1. Fix the invalid comparison.
    2. Check the value of argc.
    3. Delete the unused variable i.

    Try running it as:
    prog h 5 3
    prog x 5 3

    And a note about what you're doing here. With an 'h' argument, you're
    subtracting two integer entered in hexadecimal; with anything else,
    you're subtracting two floating-point numbers. Is that really what
    you want?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 23, 2009
    #7
  8. On Sun, 22 Feb 2009 19:13:43 -0500, "Bill Cunningham"
    <> wrote:

    > Well I read 5.10 page 114 and didn't learn a thing. I wrote some code


    I give up. What is 5.10?

    >the compiled with warnings. This program is supposed to take an h or NULL as
    >its first argument and if there's no h as the first arg it subtacts
    >decimals. If there's an h subtracts hex numbers. The ouput I get is 0.00 or
    >segmentation fault. What a mess.


    Why are you executing a program that didn't compile cleanly?

    >
    >#include <stdlib.h>
    >#include <stdio.h>
    >
    >int main(int argc, char **argv)
    >{
    > int i;
    > long a, b;
    > double x, y;
    > if (argv[1] != 'h') {


    What is the type of argv[1]? What is the type of 'h'? Are they in
    any way compatible?

    > x = strtod(argv[2], NULL);
    > y = strtod(argv[3], NULL);
    > printf("%.2f\n", x - y);
    > } else if (argv[1] == 'h') {


    After you correct the first if is there any reason to include an if in
    the else?

    > a = strtol(argv[2], NULL, 16);
    > b = strtol(argv[3], NULL, 16);
    > printf("%ld\n", a - b);
    > }
    > return 0;
    >}
    >
    >embarrassing or not I would like the 'h' to show up as any argv[] whether 1
    >or 3 if hex subtraction. If it's not present decimal subtraction.


    You don't even have broken code to test argv[3] for an 'h'.

    At least in your previous thread you tested argc to see if there were
    enough arguments to use.

    --
    Remove del for email
     
    Barry Schwarz, Feb 23, 2009
    #8
  9. Bill Cunningham

    Kojak Guest

    Le Sun, 22 Feb 2009 21:44:37 -0500,
    Bill Cunningham a écrit :

    > What about looping around to find an h. For example.
    >
    > sub h 3 4
    > sub 3 4 h
    > sub 3 h 4


    Better use 'h' as an optional argument like this

    sub [-h] <x> <y>

    That said, if you want option walking around arguments, juste
    check them, argv by argv. but, I think it's a bad idea.

    > If h is in any argv I would like the program to subtract hex numbers.
    > Whithout an h decimal numbers.


    Sorry, not understood.

    yours,

    --
    Jacques.
     
    Kojak, Feb 23, 2009
    #9
  10. "Barry Schwarz" <> wrote in message
    news:...

    > I give up. What is 5.10?


    kandr2 page 114 §5.10

    Bill
     
    Bill Cunningham, Feb 23, 2009
    #10
  11. "Keith Thompson" <> wrote in message
    news:...

    > Ok, try this:
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > int main(int argc, char **argv)
    > {
    > long a, b;
    > double x, y;
    > if (argc != 4) {
    > fputs("Usage: $0 h num num\n", stderr);
    > exit(EXIT_FAILURE);
    > }
    > if (argv[1][0] != 'h') {
    > x = strtod(argv[2], NULL);
    > y = strtod(argv[3], NULL);
    > printf("%.2f\n", x - y);
    > } else {
    > a = strtol(argv[2], NULL, 16);
    > b = strtol(argv[3], NULL, 16);
    > printf("%ld\n", a - b);
    > }
    > return 0;
    > }
    >
    > I've made the following changes:
    >
    > 1. Fix the invalid comparison.
    > 2. Check the value of argc.
    > 3. Delete the unused variable i.
    >
    > Try running it as:
    > prog h 5 3
    > prog x 5 3
    >
    > And a note about what you're doing here. With an 'h' argument, you're
    > subtracting two integer entered in hexadecimal; with anything else,
    > you're subtracting two floating-point numbers. Is that really what
    > you want?


    I'm not quite sure what you mean here.

    I'll compile the code and see what happens it looks at first glance like
    what I wanted.

    prog 3 5 //subtracts 3 from 5
    prog h f ff //subtracts f from ff

    This is the output I want. Also I wanted to make the program as a learning
    experience to accept the h from anyway a do the hex subtraction. Example:

    prog f ff h
    prog f h ff
    prog h f ff

    And use decimal subtract in the absence of the h. Most would probably use -h
    as a switch for this type of thing.


    Bill
     
    Bill Cunningham, Feb 23, 2009
    #11
  12. "Bill Cunningham" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >
    >> Ok, try this:
    >>

    [snip]
    >>
    >> I've made the following changes:
    >>
    >> 1. Fix the invalid comparison.
    >> 2. Check the value of argc.
    >> 3. Delete the unused variable i.
    >>
    >> Try running it as:
    >> prog h 5 3
    >> prog x 5 3
    >>
    >> And a note about what you're doing here. With an 'h' argument, you're
    >> subtracting two integer entered in hexadecimal; with anything else,
    >> you're subtracting two floating-point numbers. Is that really what
    >> you want?

    >
    > I'm not quite sure what you mean here.
    >
    > I'll compile the code and see what happens it looks at first glance like
    > what I wanted.


    You know, you could have tried it before posting. It would have taken
    just a minute or so.

    > prog 3 5 //subtracts 3 from 5
    > prog h f ff //subtracts f from ff
    >
    > This is the output I want. Also I wanted to make the program as a learning
    > experience to accept the h from anyway a do the hex subtraction. Example:
    >
    > prog f ff h
    > prog f h ff
    > prog h f ff
    >
    > And use decimal subtract in the absence of the h. Most would probably use -h
    > as a switch for this type of thing.


    You're subtracting integers in one case, floating-point values in the
    other. Maybe that's what you want, but, for example, you have no
    option to subtract integers expressed in decimal.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 23, 2009
    #12
  13. Bill Cunningham

    Kojak Guest

    Le Mon, 23 Feb 2009 15:51:55 -0500,
    Bill Cunningham a écrit :

    > This is the output I want. Also I wanted to make the program as a
    > learning experience to accept the h from anyway a do the hex
    > subtraction. Example:
    >
    > prog f ff h
    > prog f h ff
    > prog h f ff
    >
    > And use decimal subtract in the absence of the h.


    As I had suggested, check for each argument.

    Try to code something like this

    if 1st arg is h then
    set a and b or x and y vars according to your need,
    set hex mode and continue
    else if 2nd arg then
    ... and so no ...

    It's not the best way, but to start, this should be sufficient.

    > Most would probably use -h as a switch for this type of thing.


    Yes, indeed, but don't worry about that for now.

    Just a last point, as your were advised, compile, check for compiler
    insults, edit, correct, and loop until it works and you understand
    why.

    Try to think as computer, step by step. in other words, try to
    figure out what will happen on each statement.

    That is, feel free to ask if you're stuck.

    sincerely,

    --
    Jacques.
     
    Kojak, Feb 23, 2009
    #13
    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. =?Utf-8?B?Q3JhaWc=?=

    Big Mess, what should I do?

    =?Utf-8?B?Q3JhaWc=?=, Apr 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    403
    Tom wilson
    Apr 28, 2005
  2. Shaguf
    Replies:
    0
    Views:
    562
    Shaguf
    Dec 24, 2008
  3. Shaguf
    Replies:
    0
    Views:
    511
    Shaguf
    Dec 26, 2008
  4. Shaguf
    Replies:
    0
    Views:
    279
    Shaguf
    Dec 26, 2008
  5. Shaguf
    Replies:
    0
    Views:
    260
    Shaguf
    Dec 24, 2008
Loading...

Share This Page