sh?tpile of errors

Discussion in 'C Programming' started by Bill Cunningham, Aug 2, 2008.

  1. 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
     
    Bill Cunningham, Aug 2, 2008
    #1
    1. Advertising

  2. Bill Cunningham

    Doug Miller Guest

    In article <W21lk.68$EL2.41@trnddc01>, "Bill Cunningham" <> wrote:
    > 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...
     
    Doug Miller, Aug 2, 2008
    #2
    1. Advertising

  3. Bill Cunningham

    osmium Guest

    "Bill Cunningham" wrote:

    > 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
    >
    >
     
    osmium, Aug 2, 2008
    #3
  4. "osmium" <> wrote in message
    news:...
    > "Bill Cunningham" wrote:
    >
    >> 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
     
    Bill Cunningham, Aug 2, 2008
    #4
  5. "Doug Miller" <> wrote in message
    news:3j1lk.16107$...
    > 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
     
    Bill Cunningham, Aug 2, 2008
    #5
  6. Bill Cunningham <> wrote:
    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Aug 2, 2008
    #6
  7. On Aug 2, 1:45 pm, "Bill Cunningham" <> wrote:
    >     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.

    --
    Robert Gamble
     
    Robert Gamble, Aug 2, 2008
    #7
  8. Bill Cunningham

    Default User Guest

    Robert Gamble wrote:

    > On Aug 2, 1:45 pm, "Bill Cunningham" <> wrote:
    > >     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
     
    Default User, Aug 2, 2008
    #8
  9. Bill Cunningham

    Ian Collins Guest

    Bill Cunningham wrote:
    > 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?

    --
    Ian Collins.
     
    Ian Collins, Aug 2, 2008
    #9
  10. "Robert Gamble" <> wrote in message
    news:...

    .... 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
     
    Bill Cunningham, Aug 3, 2008
    #10
  11. "Jens Thoms Toerring" <> wrote in message
    news:-berlin.de...

    > 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
     
    Bill Cunningham, Aug 3, 2008
    #11
  12. "Malcolm McLean" <> wrote in message
    news:...

    > \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
     
    Bill Cunningham, Aug 3, 2008
    #12
  13. "Martin Ambuhl" <> wrote in message
    news:...

    > #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
     
    Bill Cunningham, Aug 3, 2008
    #13
  14. "Ian Collins" <> wrote in message
    news:...
    > 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
     
    Bill Cunningham, Aug 3, 2008
    #14
  15. "Bill Cunningham" <> wrote in message
    news:qh6lk.102$mP.55@trnddc03...
    >
    > "Robert Gamble" <> wrote in message
    > news:...
    >
    > ... 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
     
    Bill Cunningham, Aug 3, 2008
    #15
  16. "Bill Cunningham" <> writes:
    > 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.

    --
    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, Aug 3, 2008
    #16
  17. On Sat, 2 Aug 2008 11:21:05 -0700,
    osmium <> wrote:
    > "Bill Cunningham" wrote:
    >
    >> 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.


    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
    --
    |
    Martien Verbruggen | +++ Out of Cheese Error +++ Reinstall
    | Universe and Reboot +++
    |
     
    Martien Verbruggen, Aug 3, 2008
    #17
  18. "Richard Heathfield" <> wrote in message
    news:...
    > 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
     
    Bill Cunningham, Aug 3, 2008
    #18
  19. "Keith Thompson" <> wrote in message
    news:...

    > 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
     
    Bill Cunningham, Aug 3, 2008
    #19
  20. Bill Cunningham

    Guest

    On Aug 3, 2:44 am, "Bill Cunningham" <> wrote:
    > "Jens Thoms Toerring" <> wrote in messagenews:-berlin.de...
    >
    > > 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?
     
    , Aug 3, 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. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    968
    Mark Goldin
    Jan 17, 2004
  2. SenthilVel
    Replies:
    0
    Views:
    949
    SenthilVel
    Jun 7, 2006
  3. George1776

    Out-of-memory errors and caching errors.

    George1776, Aug 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,300
    George1776
    Sep 14, 2006
  4. Lance Wynn
    Replies:
    1
    Views:
    1,851
    Lance Wynn
    Feb 3, 2008
  5. yawnmoth
    Replies:
    97
    Views:
    4,732
    Bent C Dalager
    Feb 27, 2009
Loading...

Share This Page