sh?tpile of errors

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

Would anyone be interested in giving this a quick look. The compiler
gave me so many errors I don't know where to start. This is my first use of
isalpha.

#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main(int argc, char *argv[])
{
if (argc != 4) {
puts("print usage error");
ex;
}
if (isalpha(argv[1]) || isalpha(argv[2])) {
puts("name is 3rd arg");
ex;
}
double x, y;
FILE *fp;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
if (fp = fopen(argv[3], "a"))
== NULL) {
puts("fopen error");
ex;
}
fprintf(argv[3], "%.2f", x, y);
if (fclose(fp))
== NULL) {
puts("fclose error");
ex;
}
return 0;
}

I dunno sigh..

Bill
 
D

Doug Miller

Would anyone be interested in giving this a quick look. The compiler
gave me so many errors I don't know where to start. This is my first use of
isalpha.

Perhaps you should consider posting the errors you received...
 
O

osmium

Bill Cunningham said:
Would anyone be interested in giving this a quick look. The compiler
gave me so many errors I don't know where to start. This is my first use
of isalpha.

It is not unusual to see a huge number of errors. Start with the first
error and work downward, that's the way the compiler encountered them.
Focus, focus, focus!

I have marked the first one I found after fixing the usual internet munging.
#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main(int argc, char *argv[])
{
if (argc != 4) {
puts("print usage error");
ex;
}
if (isalpha(argv[1]) || isalpha(argv[2])) {

*** extra ')' above
puts("name is 3rd arg");
ex;
}
double x, y;
FILE *fp;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
if (fp = fopen(argv[3], "a"))
== NULL) {
puts("fopen error");
ex;
}
fprintf(argv[3], "%.2f", x, y);
if (fclose(fp))
== NULL) {
puts("fclose error");
ex;
}
return 0;
}

I dunno sigh..

Bill
 
B

Bill Cunningham

osmium said:
Bill Cunningham said:
Would anyone be interested in giving this a quick look. The compiler
gave me so many errors I don't know where to start. This is my first use
of isalpha.

It is not unusual to see a huge number of errors. Start with the first
error and work downward, that's the way the compiler encountered them.
Focus, focus, focus!

I have marked the first one I found after fixing the usual internet
munging.
#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main(int argc, char *argv[])
{
if (argc != 4) {
puts("print usage error");
ex;
}
if (isalpha(argv[1]) || isalpha(argv[2])) {

*** extra ')' above

[snip]

I got that one too. I don't see the extra ) myself.

Bill
 
B

Bill Cunningham

Doug Miller said:
Perhaps you should consider posting the errors you received...

I would like to but in dealing with bash I don't know the shell commands
to print to stderr. It's something like 2&1 or such.

Bill
 
J

Jens Thoms Toerring

Bill Cunningham said:
Would anyone be interested in giving this a quick look. The compiler
gave me so many errors I don't know where to start. This is my first use of
isalpha.
#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)
int main(int argc, char *argv[])
{
if (argc != 4) {
puts("print usage error");
ex;
}
if (isalpha(argv[1]) || isalpha(argv[2])) {

A) There is no prototype for isalpha() in scope, you forgot to
include <ctype.h>
B) isalpha() expects an int (typically a char, cast to int) as
its argument, but you pass it a pointer to a string.
puts("name is 3rd arg");
ex;
}
double x, y;
FILE *fp;

At least C89 doesn't allow mixing of code and declarations, all
declarations have to come first.
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
if (fp = fopen(argv[3], "a")) == NULL) {

You're missing '(' directly in front of 'fp'.
puts("fopen error");
ex;
}
fprintf(argv[3], "%.2f", x, y);

fprintf() expects a FILE* as it's first argument, here you pass
it a pointer to a string. I guess you meant to use 'fp' here.
And you have a format specifier for only a single double, but
then pass fprintf() two of them.
if (fclose(fp)) == NULL) {

First of all, there's a ')' before '==' that's wrong. And then
fclose() returns an int, not a pointer, with 0 indicating success.
puts("fclose error");
ex;
}
return 0;
}
Regards, Jens
 
R

Robert Gamble

    Would anyone be interested in giving this a quick look. The compiler
gave me so many errors I don't know where to start. This is my first use of
isalpha.

[snip]

It looks like you have been working on learning C for about a year now
and don't have much to show for it. You still seem to lack a basic
understanding of the fundamentals and you don't seem to be able to
meaningfully incorporate the answers you have received in this group
into your education. If you have made a sincere effort over the last
year to learn C then my advice is to recognize that programming just
isn't your thing, stop wasting your time and find something you are
good at. If you haven't made a real effort then stop wasting
everyone's time with elementary questions whose answers you can't seem
to benefit from anyway and either make an honest attempt on your own
or find something else to keep you busy.
 
D

Default User

Robert said:
    Would anyone be interested in giving this a quick look. The
compiler gave me so many errors I don't know where to start. This
is my first use of isalpha.

[snip]

It looks like you have been working on learning C for about a year now
and don't have much to show for it.

Ha ha! A YEAR? More like eight years.



Brian
 
I

Ian Collins

Bill said:
Would anyone be interested in giving this a quick look. The compiler
gave me so many errors I don't know where to start. This is my first use of
isalpha.
Why don't you learn to compile a few lines at a time?
 
B

Bill Cunningham

.... and you don't seem to be able to
meaningfully incorporate the answers you have received in this group
into your education.

I strongly disagree. I've never use ctype or even talked about for example.

Bill
 
B

Bill Cunningham

A) There is no prototype for isalpha() in scope, you forgot to
include <ctype.h>
B) isalpha() expects an int (typically a char, cast to int) as
its argument, but you pass it a pointer to a string.

[snip]

Thanks Jen. I've never used of thought of ctype.h.

Bill
 
B

Bill Cunningham

\However
1) you need to include ctype.h for the isalpha prototype
2) the function works on a single character, not a string, so you need to
pass
it argv[1][0], presuming that you want to check the first character of
argv[1].
Ok I see thanks. strchar is probably needed too. I guess that means
string.h is needed. I've never got so many errors from a compiler before.

Bill
 
B

Bill Cunningham

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> /* mha: added */
/* mha: removed silly macro */

int main(int argc, char *argv[])
{
if (argc != 4) {
fputs("print usage error\n", stderr);
exit(EXIT_FAILURE);
}
if (isalpha(argv[1]) || isalpha(argv[2])) {
fputs("name is 3rd arg\n", stderr);
exit(EXIT_FAILURE);
}
double x, y;
FILE *fp;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
if (!(fp = fopen(argv[3], "a"))) { /* mha: fixed parenthesis
error */
fputs("fopen error\n", stderr);
exit(EXIT_FAILURE);
}
fprintf(fp, "%.2f %.2f\n", x, y); /* mha: fixed multiple errors
in arguments to fprintf.
Since there is no way to be
sure what you are trying to
do, I had to guess. */
if (fclose(fp) == EOF) { /* mha: fixed parenthesis error and
completely bogus comparison to
NULL */
fputs("fclose error\n", stderr);
exit(EXIT_FAILURE);
}
return 0;
}

Now I see that strchr is going to be needed here. I'm using headers too
I've never used.

Bill
 
B

Bill Cunningham

Ian Collins said:
Why don't you learn to compile a few lines at a time?

How would I learn to do that? I've never received so many errors from a
compiler before. I have surmised on my own strchr is going to be needed.
Others say ctype.h which I've never used. Nothing like debugging with a few
others more experienced ;)

Bill
 
B

Bill Cunningham

Bill Cunningham said:
... and you don't seem to be able to
meaningfully incorporate the answers you have received in this group
into your education.

I am also writing small utilities now. Debugging with others helps you
see your own errors also. I am seeing ways now that I can improve the code
that I would never have been able to see without the involvement of others.

clc is a good place for group debugging.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Would anyone be interested in giving this a quick look. The compiler
gave me so many errors I don't know where to start. This is my first use of
isalpha.

#include <stdio.h>
#include <stdlib.h>
#define ex exit(EXIT_FAILURE)

int main(int argc, char *argv[])
{
if (argc != 4) {
puts("print usage error");
ex;
}
if (isalpha(argv[1]) || isalpha(argv[2])) {
puts("name is 3rd arg");
ex;
}
double x, y;
FILE *fp;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
if (fp = fopen(argv[3], "a"))
== NULL) {
puts("fopen error");
ex;
}
fprintf(argv[3], "%.2f", x, y);
if (fclose(fp))
== NULL) {
puts("fclose error");
ex;
}
return 0;
}

I dunno sigh..

Starting with the first error your compiler reports. Ignore all the
other errors until you've fixed the first one. Recompile. Later,
rinse, repeat.

Style point: Drop the "ex" macro. When you want to write
exit(EXIT_FAILURE), just write exit(EXIT_FAILURE); it's *much*
clearer, since you don't force the reader to figure out what "ex"
means. Don't use macros to save typing; that's not what they're for.

And I'll discuss just one of several errors you've made. You write

if (fclose(fp) == NULL) { ...

(Well, almost; I've deleted an extra ')'.) What type does fclose()
return? Why are you comparing its result to NULL?

You know that fclose(fp) is the way to close a file; that's good. But
you should have the documentation in front of you, and understand the
types and meanings of any arguments and of the return type, before you
even type the 'f'. The same applies to any standard function (for
example, your incorrect fprintf call in the previous line).

You obviously guessed that fclose() returns a pointer. You guessed
wrong. That's not necessarily a bad thing, but rather than checking
your guess, you went ahead and wrote the call and posted it here.

And you can't depend on the compiler to catch all your errors for you.
For reasons I won't go into right now, a compiler won't necessarily
flag the error of comparing (fclose(fp) == NULL); you could silently
get code that just does the wrong thing.

Take the extra time to check the documentation. It will save you (and
us!) time and effort in the long run.
 
M

Martien Verbruggen

It is not unusual to see a huge number of errors. Start with the first
error and work downward, that's the way the compiler encountered them.

That is good advice.

In the below, I'm only going to correct your other advice, and won't try
to play compiler for Bill. All diagnostics needed to 'fix' this program
are provided by his compiler, assuming he allows it to.
int main(int argc, char *argv[])
{
if (argc != 4) {
puts("print usage error");
ex;
}
if (isalpha(argv[1]) || isalpha(argv[2])) {

*** extra ')' above

Not above. Below.
puts("name is 3rd arg");
ex;
}
double x, y;
FILE *fp;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
if (fp = fopen(argv[3], "a"))
Here.
== NULL) {
puts("fopen error");
ex;
}
fprintf(argv[3], "%.2f", x, y);
if (fclose(fp))

And here.

Martien
 
B

Bill Cunningham

Richard Heathfield said:
Mark Pappin gave you a great chance to rationalise your learning process
and gain a good teacher at the same time, but you threw that chance away.

Maybe he's right from a point of view. Richard I don't think the
tutorial from Mark as well meant as it was was a good idea. Not for him or
me. I'm suprised we made it to question 4. I sometimes need to be almost fed
by the spoonful on this stuff. Maybe hard learning group debugging and k&r2
is the way for me. Then I will eventually learn C. I really don't think Mark
had the patience needed for one like me. What I need is an "LD" teacher.
Anyway from now on I would only like to ask for debugging advice when
necessary a little learning and for the most part we can go our ways.

Bill
 
B

Bill Cunningham

Take the extra time to check the documentation. It will save you (and
us!) time and effort in the long run.
I confuse the return types quite a bit. I was going from memory. I need
to do just what you said basically on this one. ctype.h is a new one to me.

Bill
 
V

vippstar

Jens Thoms Toerring said:
A) There is no prototype for isalpha() in scope, you forgot to
include <ctype.h>
B) isalpha() expects an int (typically a char, cast to int) as
its argument, but you pass it a pointer to a string.

[snip]

Thanks Jen. I've never used of thought of ctype.h.

Speak english?
 

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
code 50
Command Line Arguments 0
I'm facing a run-time error 14
seg fault 76
pow type problem 6
How can I view / open / render / display a pdf file with c code? 0
percentage 8

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top