question

B

Ben Bacarisse

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

OK but why not close? It looks so much simpler than
_close and is easier to type.
My compiler said nothing. I can try -Wall

It is a constraint violation for the code you wrote (it is C99 because
you declare variables after a statement). You compiler must complain
if you use it in the right "mode".

-Wall is, none the less, also a good idea. It will tell you about
printf format problems. Your compiler could have solved both these
problems for you.
 
C

CBFalconer

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?

#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;

These 2 lines should have generated errors. Put them before the
code generating line "if (argc) ...". In addition, don't use
identifier names with leading '_' characters; they are usually
limitied for use by the implementation, not you.
_close = strtod(argv[1], NULL);
_high = strtod(argv[2], NULL);
_low = strtod(argv[3], NULL);
vol = atoi(argv[4]);

You are already using some of the strto* functions. Why use atoi
here?
 
B

Bill Cunningham

CBFalconer said:
You are already using some of the strto* functions. Why use atoi
here?

Volume is type int. In my file not strtod. That's really all I know to
use.

Bill
 
R

Richard

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

Please tell me you are joking? Please.....
 
L

Lew Pitcher

OK but why not close? It looks so much simpler than
_close and is easier to type.

Perhaps the namespace in /his/ environment already recognizes "close" as a
valid name (perhaps defined in one of the #include files that he specifies
in his code). Can functions and lvalues share names? If not, then
double close;
might conflict with
int close(int fd);


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
K

Keith Thompson

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;
}

If argc is not equal to 5, you print an error message *and then
continue with the rest of the program, using garbage input*.

strtod is capable of telling you about errors. Use that capability.
Don't ask us how; look it up.

atoi() is *not* capable of telling you about errors. Use strtol()
instead. And check for errors.

For every standard function you use, you must have a #include
directive for the corresponding header.
 
K

Keith Thompson

Bill Cunningham said:
Sorry never heard of an identifier in C.

I'm astonished that you've never heard of an identifier. But even if
you haven't, what's the point of telling us that? Don't tell us you
don't know what it is, *find out*. Look it up in your copy of K&R2.
 
B

Bill Cunningham

If argc is not equal to 5, you print an error message *and then
continue with the rest of the program, using garbage input*.

strtod is capable of telling you about errors. Use that capability.
Don't ask us how; look it up.

atoi() is *not* capable of telling you about errors. Use strtol()
instead. And check for errors.

For every standard function you use, you must have a #include
directive for the corresponding header.
Ah Keith good to hear from you. I have changed that if statement to include
an exit(EXIT_FAILURE) call so the program exits and there's no seg fault.
I am just used to using atoi. So there's no error checking. I haven't
included that at all in this program I usually do that with streams and
such. It would be a good thing to start. I'll check out strtol.

Bill
 
B

Bill Cunningham

These two functions work similarly to strtod. See pp.
251-252 in K&R2.

I see on p 252

long strtol(const char *s, char **endptr,int base)

In my example with all I want to do with atoi() should those last 2
parameters be NULL ?

Bill
 
B

Bill Cunningham

I'm astonished that you've never heard of an identifier. But even if
you haven't, what's the point of telling us that? Don't tell us you
don't know what it is, *find out*. Look it up in your copy of K&R2.

The data type I think. I get the picture read kandr2. It's hard for me
to understand sometimes particular concepts kandr2 is saying. The reference
section is the best though. The bitwise operators for example. Someday I may
just stumble into knowing how to use those. And what their purposes are for.
It depends on the compactness and my ability to concentrate on a given time
whether I understand much or not.

Bill
 
K

Keith Thompson

Bill Cunningham said:
These two functions work similarly to strtod. See pp.

I see on p 252

long strtol(const char *s, char **endptr,int base)

In my example with all I want to do with atoi() should those last 2
parameters be NULL ?

What you're doing with atoi() can fail drastically in case of error.
You should detect and recover from errors.

base is of type int; it can't be NULL anyway.

RTFM.
 
K

Keith Thompson

Bill Cunningham said:
The data type I think. I get the picture read kandr2. It's hard for me
to understand sometimes particular concepts kandr2 is saying. The reference
section is the best though. The bitwise operators for example. Someday I may
just stumble into knowing how to use those. And what their purposes are for.
It depends on the compactness and my ability to concentrate on a given time
whether I understand much or not.

I haven't a clue what you mean by "The data type I think.". An
identifier is not a data type. Look up the word "identifier" in the
index of K&R2. Please don't waste your time and ours posting here
about it until and unless you have read what K&R2 has to say.
 
C

CBFalconer

Bill said:
Volume is type int. In my file not strtod. That's really all I
know to use.

Following is a portion of the C standard description. Note that
you can have an int object and load it with strtol, as in:

intobject = strtol(s, *endptr, 10);

without needing casts. So forget atoi.

7.20.1.4 The strtol, strtoll, strtoul, and strtoull functions

Synopsis
[#1]
#include <stdlib.h>
long int strtol(
const char * restrict nptr,
char ** restrict endptr,
int base);
long long int strtoll(
const char * restrict nptr,
char ** restrict endptr,
int base);
unsigned long int strtoul(
const char * restrict nptr,
char ** restrict endptr,
int base);
unsigned long long int strtoull(
const char * restrict nptr,
char ** restrict endptr,
int base);
Description

[#2] The strtol, strtoll, strtoul, and strtoull functions
convert the initial portion of the string pointed to by nptr
to long int, long long int, unsigned long int, and unsigned
long long int representation, respectively. First, they
decompose the input string into three parts: an initial,
possibly empty, sequence of white-space characters (as
specified by the isspace function), a subject sequence
resembling an integer represented in some radix determined
by the value of base, and a final string of one or more
unrecognized characters, including the terminating null
character of the input string. Then, they attempt to
convert the subject sequence to an integer, and return the
result.
 
N

Nick Keighley

    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;

}

you must have breached one or more of these posting rules


0. fix your layout
1. post your code
2. post your input data
3. post your output
4. explain why you don't like 3
5. check return values
6. RTFM
7. don't guess (see 6)


ah! 1 and 5 (at least)
 
K

Keith Thompson

Bill Cunningham said:
[snip]

I better ask. RTFM.

What does RTFM mean ?

You see, this is exactly the kind of question you shouldn't have to
ask here.

For something like this, your first thought should be www.google.com,
not comp.lang.c.

For anything relevant to C, your first thought should be K&R2, not
comp.lang.c.
 
K

Kenny McCormack

Bill Cunningham said:
[snip]

I better ask. RTFM.

What does RTFM mean ?

You see, this is exactly the kind of question you shouldn't have to
ask here.

For something like this, your first thought should be www.google.com,
not comp.lang.c.

For anything relevant to C, your first thought should be K&R2, not
comp.lang.c.

Serious question: fill in the blanks below:

For ________ ________ __ _, your first thought should be comp.lang.c.
That's what we're here for!

(You probably already know how I'd fill in those blanks. And I
certainly applaud your admission that this newsgroup is *not* the place
to discuss and/or get help on the C programming language)
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top