I can not figure this out

Discussion in 'Programming' started by Onceler, Feb 9, 2013.

  1. Onceler

    Onceler Golden Member

    Joined:
    Feb 28, 2008
    Messages:
    1,264
    Likes Received:
    0
    I am trying to learn C from the second edition of The C Programming Language and on two programs they won't compile, I am using GCC and code::blocks
    the first is 1.5.4 Word Counting
    #include <stdio.h>
    #define IN 1
    #define OUT 0
    main ()
    {
    int c,nl,nw,nc,state;
    state = OUT;
    nl=nw=nc=0;
    while ((c=getchar())!=EOF){
    ++nc;
    if (c== '\n')
    ++nl;
    if (c ==' ':: c=='\n'::c=='\t')
    state=OUT;
    else if(state==OUT){
    state=IN;
    ++nw;
    }
    }
    printf("%d %d %d \n",nl,nw,nc);
    }
    the compiler tells me that there is an expected ')' before ':' token on line 13
    also on 1.6 Arrays
    #include<stdio.h>
    main()
    {
    int c, i, nwhite, nother;
    int ndigit[10];
    nwhite=nother=0;
    for (i=0;i<10;++i)
    ndigit=0;
    while((c=getchar())!EOF)
    if(c>='0'&&c<='9')
    ++ndigit[c-'0'];
    else if(c==' ':: c=='\n'::c=='\t')
    ++nwhite;
    else ++nother;
    printf ("digits =");
    for (i=0; i<10;++i)
    printf(" %d",ndigit);
    printf(", white space =%d, other=%d\n",nwhite,nother);

    }
    On line 9 expected')' before '!' token then the same error as before on line 12.
    Am I doing something wrong or is my compiler at fault? I have tried retyping these from blank files and still do not get results.
    I am very frustrated and even though the code makes sense to me I am not exactly inspired with confidence from these errors.
    Code::Blocks is version 12.11
    Ia it CB or my compiler and if it is the compiler what can I do about it?
    Thanks
     
    #1 Onceler, Feb 9, 2013
    Last edited: Feb 9, 2013
  2. Crusty

    Crusty Lifer

    Joined:
    Sep 30, 2001
    Messages:
    12,688
    Likes Received:
    0
    Well the '::' operator does not exist in the C language, which is what you are using in your if statements in both programs. Did you retype these examples from a book? I'm guessing you meant to use the || operator, which is the logical OR operator.
     
  3. Onceler

    Onceler Golden Member

    Joined:
    Feb 28, 2008
    Messages:
    1,264
    Likes Received:
    0
    Thank you.
    I tried it with || and the first one works I don't know why but the book looks like::
    any thoughts on the second prog?
     
  4. sandorski

    sandorski No Lifer

    Joined:
    Oct 10, 1999
    Messages:
    61,280
    Likes Received:
    37
    '=' before "EOF" ?
     
  5. EagleKeeper

    EagleKeeper Discussion Club Moderator<br>Elite Member
    Staff Member

    Joined:
    Oct 30, 2000
    Messages:
    42,600
    Likes Received:
    0
    while ((c=getchar()) != EOF)

    As indicated above

    [quote\] Ia it CB or my compiler and if it is the compiler what can I do about it?[/quote]

    Programmer error :p
    Reading and/or typing
     
    #5 EagleKeeper, Feb 9, 2013
    Last edited: Feb 9, 2013
  6. Aluvus

    Aluvus Platinum Member

    Joined:
    Apr 27, 2006
    Messages:
    2,913
    Likes Received:
    0
    The "pipe" symbol is sometimes rendered as a vertical line with a gap in the middle, particularly on keyboards (including mine). Printing it that way in a book, especially one that would use it so frequently, seems like a poor choice.
     
Loading...