question

B

Bill Cunningham

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
 
R

Richard

Bill Cunningham said:
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
 
I

Ian Collins

Bill said:
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.
 
B

Bill Cunningham

Trent Josephsen said:
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
 
B

Bill Cunningham

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
 
B

Bill Cunningham

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
 
O

osmium

:

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.
 
M

Martin Ambuhl

Bill said:
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;
}
 
C

Charlton Wilbur

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
 
B

Bill Cunningham

Ian Collins said:
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
 
B

Ben Bacarisse

Bill Cunningham said:
#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.
 
B

Bill Cunningham

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
 
B

Bill Cunningham

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.
 
B

Bill Cunningham

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
 
B

Bill Cunningham

Ben Bacarisse said:
"Bill Cunningham" <[email protected]> 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
 
B

Bill Cunningham

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
 
V

vippstar

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

code question 74
error 20
code 50
average true range 21
percentage 8
oscillator 51
indentation 26
root(s) 6

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top