question

Discussion in 'C Programming' started by Bill Cunningham, Sep 24, 2008.

  1. This program is far from complete but I think there's something wrong in
    the mf line. Which means "money flow". Other than that I have no idea what
    is wrong here. The formula calls for tp_price X vol. Vol is an int and
    tp_price is a double and I'm multiplying the two. Is this legal?

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
    if (argc != 5)
    puts("MFI usage error, C H L Vol");
    double _close, _high, _low, tp_price, mf;
    int vol;
    _close = strtod(argv[1], NULL);
    _high = strtod(argv[2], NULL);
    _low = strtod(argv[3], NULL);
    vol = atoi(argv[4]);
    tp_price = (_close + _high + _low) / 3;
    mf = tp_price * vol;
    printf("%i\n", mf);
    return 0;
    }

    Bill
     
    Bill Cunningham, Sep 24, 2008
    #1
    1. Advertising

  2. Bill Cunningham

    Richard Guest

    "Bill Cunningham" <> writes:

    > This program is far from complete but I think there's something wrong in
    > the mf line. Which means "money flow". Other than that I have no idea what
    > is wrong here. The formula calls for tp_price X vol. Vol is an int and
    > tp_price is a double and I'm multiplying the two. Is this legal?
    >
    > #include <stdio.h>
    >
    > int main(int argc, char *argv[])
    > {
    > if (argc != 5)
    > puts("MFI usage error, C H L Vol");
    > double _close, _high, _low, tp_price, mf;
    > int vol;
    > _close = strtod(argv[1], NULL);
    > _high = strtod(argv[2], NULL);
    > _low = strtod(argv[3], NULL);
    > vol = atoi(argv[4]);
    > tp_price = (_close + _high + _low) / 3;
    > mf = tp_price * vol;
    > printf("%i\n", mf);
    > return 0;
    > }
    >
    > Bill
    >


    You forgot to apply the sky hooks to the pejorative element of the
    de-facto stack manipulator. And your use of puts is simply sublime!

    6/10
     
    Richard, Sep 24, 2008
    #2
    1. Advertising

  3. Bill Cunningham

    Ian Collins Guest

    Bill Cunningham wrote:
    > This program is far from complete but I think there's something wrong in
    > the mf line. Which means "money flow". Other than that I have no idea what
    > is wrong here. The formula calls for tp_price X vol. Vol is an int and
    > tp_price is a double and I'm multiplying the two. Is this legal?
    >

    I disagree with Richard, this is a 2/10 troll.

    Please try harder next time.

    --
    Ian Collins.
     
    Ian Collins, Sep 24, 2008
    #3
  4. Bill Cunningham

    Richard Guest

    Ian Collins <> writes:

    > Bill Cunningham wrote:
    >> This program is far from complete but I think there's something wrong in
    >> the mf line. Which means "money flow". Other than that I have no idea what
    >> is wrong here. The formula calls for tp_price X vol. Vol is an int and
    >> tp_price is a double and I'm multiplying the two. Is this legal?
    >>

    > I disagree with Richard, this is a 2/10 troll.


    No. The "puts" was worth a bonus of 4 ....

    >
    > Please try harder next time.


    --
     
    Richard, Sep 24, 2008
    #4
  5. "Trent Josephsen" <> wrote in message
    news:...
    >
    > The most obvious problem here is that you're using %i as a format
    > specifier in the first argument to printf, but the second argument isn't
    > an integer, it's a double.
    >
    > Also, I could be wrong, but I believe that variables starting with an
    > underscore aren't supposed to be used in ordinary code.


    So multiplying an interger value and a double value isn't a problem
    then? I see your point maybe I shouldn't have posted this. But then maybe
    again there is a problem here. What do you mean _ are not supposed to be in
    front of variables? What about tp_price ?

    Bill
     
    Bill Cunningham, Sep 24, 2008
    #5
  6. There's still something wrong with this code Trent. I am getting long
    numbers 12-13 digits long. I corrected I think this problem by changing %i
    to %.2f and I'm still getting huge numbers.

    For example,

    5 6.5 4 /3 = 5.17
    5.17 * 8000=41360 not 8857664958554

    These are the huge numbers I'm getting.

    Bill
     
    Bill Cunningham, Sep 24, 2008
    #6
  7. "Ian Collins" <> wrote in message
    news:...

    > I disagree with Richard, this is a 2/10 troll.
    >
    > Please try harder next time.


    Ian after long consideration, patience and listening and taking into
    account Richard's helpfulness vs sarcasm. I have finally today added him to
    my killfile. I myself don't like to do this much because you miss out on
    alot of opinions good and bad and don't get the whole story but you'll have
    to enlighten me as to Richards words. If they are of use.

    Bill
     
    Bill Cunningham, Sep 24, 2008
    #7
  8. Bill Cunningham

    Ian Collins Guest

    Bill Cunningham wrote:
    > "Trent Josephsen" <> wrote in message
    > news:...
    >> The most obvious problem here is that you're using %i as a format
    >> specifier in the first argument to printf, but the second argument isn't
    >> an integer, it's a double.
    >>


    Did you bother to read this?

    --
    Ian Collins.
     
    Ian Collins, Sep 24, 2008
    #8
  9. Bill Cunningham

    osmium Guest

    "Bill Cunningham" writes:


    >> Also, I could be wrong, but I believe that variables starting with an
    >> underscore aren't supposed to be used in ordinary code.


    >What do you mean _ are not supposed to be in front of variables? What about
    >tp_price ?


    Look up the word "starting" in a dictionary of the English language.

    Also, tp_price is what is called an "identifier". Surely, with the Internet
    at your disposal you should be able to find the rules for forming an
    identifier. Find the rules and make a note of where you found them so you
    can find them again.
     
    osmium, Sep 24, 2008
    #9
  10. Bill Cunningham wrote:
    > This program is far from complete but I think there's something wrong in
    > the mf line. Which means "money flow". Other than that I have no idea what
    > is wrong here. The formula calls for tp_price X vol. Vol is an int and
    > tp_price is a double and I'm multiplying the two. Is this legal?


    You have only two language errors, the incorrect specifier in the printf
    call and lack of declaration for strtod and atoi. Whether the program
    is wisely written is another question. And the declaration of
    identifiers with a leading underscore, even in situations where
    perfectly legal, is a bad habit that can easily lead to illegal use.

    /* mha: fixed silly use of variable names starting with underscore.
    * fixed lack of declaration for strtod and atoi.
    * fixed broken format specfier in printf().
    * for readability:
    * changed 'mf' to 'money_flow.
    * change 'vol' to 'volume'.
    * No changes in program logic, use of atoi(), absence of error
    * checking, cryptic error message without program termination,
    * or anything else. */

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

    int main(int argc, char *argv[])
    {
    if (argc != 5)
    puts("MFI usage error, C H L Vol");
    double close, high, low, tp_price, money_flow;
    int volume;
    close = strtod(argv[1], NULL);
    high = strtod(argv[2], NULL);
    low = strtod(argv[3], NULL);
    volume = atoi(argv[4]);
    tp_price = (close + high + low) / 3;
    money_flow = tp_price * volume;
    printf("%.2f\n", money_flow);
    return 0;
    }
     
    Martin Ambuhl, Sep 24, 2008
    #10
  11. >>>>> "BC" == Bill Cunningham <> writes:

    BC> So multiplying an interger value and a double value isn't a
    BC> problem then?

    Short of asking comp.lang.c, how could you find out?

    (Hint: you claim to have a copy of K&R close at hand.)

    BC> What do you mean _ are not supposed to be in front of variables?

    Does your copy of K&R offer any insight on the matter?

    BC> What about tp_price ?

    Does that have an underscore at the beginning?

    Charlton





    --
    Charlton Wilbur
     
    Charlton Wilbur, Sep 24, 2008
    #11
  12. "Ian Collins" <> wrote in message
    news:...
    > Bill Cunningham wrote:
    >> "Trent Josephsen" <> wrote in message
    >> news:...
    >>> The most obvious problem here is that you're using %i as a format
    >>> specifier in the first argument to printf, but the second argument isn't
    >>> an integer, it's a double.
    >>>

    >
    > Did you bother to read this?
    >

    Yes I did Ian and I changed the format specifier to %.2f maybe it should
    be %d ,anyway I am still getting huge 12-13 digit numbers and there's
    something wrong. Also a seg fault which could quite probably be taken care
    of with exit(EXIT_FAILURE)

    Bill
     
    Bill Cunningham, Sep 24, 2008
    #12
  13. "Bill Cunningham" <> writes:

    > #include <stdio.h>
    >
    > int main(int argc, char *argv[])
    > {
    > if (argc != 5)
    > puts("MFI usage error, C H L Vol");
    > double _close, _high, _low, tp_price, mf;
    > int vol;
    > _close = strtod(argv[1], NULL);


    Two things: (1) Why did you choose the name _close? Why did it seem to be
    a better choice than close, or close_ or _c_l_o_s_e_?

    (2) If you compiler is not complaining about strtod being undeclared
    (or some such message) your really *must* change your compiler flags
    until it does. This is one reason the program is not working and the
    compiler can tell you about it.

    > _high = strtod(argv[2], NULL);
    > _low = strtod(argv[3], NULL);
    > vol = atoi(argv[4]);
    > tp_price = (_close + _high + _low) / 3;
    > mf = tp_price * vol;
    > printf("%i\n", mf);


    OK, the %i error has been pointed out, but did you compiler not tell
    you that the type of mf does not match that expected by %i? Again,
    I'd suggest you try to the best help you can from your compiler.

    > return 0;
    > }


    --
    Ben.
     
    Ben Bacarisse, Sep 24, 2008
    #13
  14. "Martin Ambuhl" <> wrote in message
    news:gbeb10$1a3$...

    > You have only two language errors, the incorrect specifier in the printf
    > call and lack of declaration for strtod and atoi. Whether the program is
    > wisely written is another question. And the declaration of identifiers
    > with a leading underscore, even in situations where perfectly legal, is a
    > bad habit that can easily lead to illegal use.
    >
    > /* mha: fixed silly use of variable names starting with underscore.
    > * fixed lack of declaration for strtod and atoi.

    [snip]
    I don't quite understand this one above. Do you mean return values
    should be declared and used?



    Bill
     
    Bill Cunningham, Sep 24, 2008
    #14
  15. "osmium" <> wrote in message
    news:...

    > Also, tp_price is what is called an "identifier". Surely, with the
    > Internet at your disposal you should be able to find the rules for forming
    > an identifier. Find the rules and make a note of where you found them so
    > you can find them again.


    Sorry never heard of an identifier in C.
     
    Bill Cunningham, Sep 24, 2008
    #15
  16. "Charlton Wilbur" <> wrote in message
    news:...
    >>>>>> "BC" == Bill Cunningham <> writes:


    > BC> What about tp_price ?
    >
    > Does that have an underscore at the beginning?


    No but it does have an underscore. Just thought that I would inquire
    about it.

    Bill
     
    Bill Cunningham, Sep 24, 2008
    #16
  17. "Ben Bacarisse" <> wrote in message
    news:...
    > "Bill Cunningham" <> writes:


    > Two things: (1) Why did you choose the name _close? Why did it seem to be
    > a better choice than close, or close_ or _c_l_o_s_e_?


    I've seen it in a lot of code before. I didn't know it was taboo.

    > (2) If you compiler is not complaining about strtod being undeclared
    > (or some such message) your really *must* change your compiler flags
    > until it does. This is one reason the program is not working and the
    > compiler can tell you about it.


    My compiler said nothing. I can try -Wall

    Bill
     
    Bill Cunningham, Sep 24, 2008
    #17

  18. > "Ben Bacarisse" <> wrote in message
    > news:...
    >> (2) If you compiler is not complaining about strtod being undeclared
    >> (or some such message) your really *must* change your compiler flags
    >> until it does. This is one reason the program is not working and the
    >> compiler can tell you about it.


    You're right about strtod. It was out of scope. When I opened my word
    processer program I included stdlib.h. I must've removed it as my first post
    was a direct copy of my source code and stdlib.h isn't there. Now my numbers
    are working right. All leading underscores have been removed and cleaned up.

    I would like to add some capabilites here though. I might post my
    revisions.

    Thanks

    Bill
     
    Bill Cunningham, Sep 24, 2008
    #18
  19. Bill Cunningham

    Guest

    On Sep 25, 12:37 am, "Bill Cunningham" <> wrote:
    > "osmium" <> wrote in message
    >
    > news:...
    >
    > > Also, tp_price is what is called an "identifier". Surely, with the
    > > Internet at your disposal you should be able to find the rules for forming
    > > an identifier. Find the rules and make a note of where you found them so
    > > you can find them again.

    >
    > Sorry never heard of an identifier in C.



    What does your book tell you about identifiers?
     
    , Sep 24, 2008
    #19
  20. Bill Cunningham

    Guest

    On Sep 25, 12:34 am, "Bill Cunningham" <> wrote:
    > "Martin Ambuhl" <> wrote in message
    >
    > news:gbeb10$1a3$...
    >
    > > You have only two language errors, the incorrect specifier in the printf
    > > call and lack of declaration for strtod and atoi. Whether the program is
    > > wisely written is another question. And the declaration of identifiers
    > > with a leading underscore, even in situations where perfectly legal, is a
    > > bad habit that can easily lead to illegal use.

    >
    > > /* mha: fixed silly use of variable names starting with underscore.
    > > * fixed lack of declaration for strtod and atoi.

    >
    > [snip]
    > I don't quite understand this one above. Do you mean return values
    > should be declared and used?


    What does your book tell you about declarations?
     
    , Sep 24, 2008
    #20
    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. sean
    Replies:
    1
    Views:
    629
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    702
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    981
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    451
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    889
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page