Newbie question: Kernighan-Ritchie C book- problem

This is a discussion on Newbie question: Kernighan-Ritchie C book- problem within the C Programming forums, part of the General Programming Boards category; Hi this is my first post here... I am learning C right now(through the book on the Title), so my ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    6

    Newbie question: Kernighan-Ritchie C book- problem

    Hi this is my first post here...
    I am learning C right now(through the book on the Title), so my knowledge about the language is very small...

    So, in the first chapter of the book there is a programm about tables, which counts how many times every digit, every "white" space(" ","\n" etc) and other "letters" appear in a text.

    So I wrote down the code exactly as in the book:
    Code:
    #include<stdio.h>
    main()
    {
    int c,i,nwhite,nother;
    int ndigit[ 10];
    
    nwhite = nother=0;
    for (i=0 ; i<10 ; ++i)
            ndigit[i]=0;
    while((getchar()) !=EOF)
                    if (c>='0' && c<='9')
                     ++ndigit[c-'0'];
    
                    else
                            if (c==' ' || c=='\n' || c=='\t')
                     ++nwhite;
    
                            else
                     ++nother;
    
                      printf("Ta psifia einai");
                    for( i = 0 ; i < 10 ; ++i)
                      printf("%d",ndigit[i]);
                      printf(",ta leyka diastimata einai %d, kai oi alloi xaraktires %d", nwhite, nother);
                      printf(".Sas eyxaristoume pou mas kalesate.\n");
    
                      }
    According to the book when I run the programm, I should take something like that:
    digits 9300000001
    white spaces 124
    other 330
    ----------------
    Now my problem:
    I get always digits: 0000000000
    white spaces 0
    and other: the number of the characters in the text...
    no matter what text I use...
    ------------------
    I would be very glad if someone could find the error...
    Thanks a lot,
    plutonas

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You ain't puttin nuthin in c! Your while loop is also wrong and you are not initializing your variables. Try this:

    Code:
    #include<stdio.h>
    
    int main() {
        int c,i,nwhite=0,nother=0;
        int ndigit[ 10];
    
        nwhite = nother=0;
        for (i=0 ; i<10 ; ++i)
            ndigit[i]=0;
        while((c = getc(stdin))!=EOF){
            if (c>='0' && c<='9')
                ++ndigit[c-'0'];
            else if (c==' ' || c=='\n' || c=='\t')
                ++nwhite;
            else
                ++nother;
        }
        printf("Ta psifia einai");
        for( i = 0 ; i < 10 ; ++i)
            printf("%d",ndigit[i]);
        printf(",ta leyka diastimata einai %d, kai oi alloi xaraktires %d", nwhite, nother);
        printf(".Sas eyxaristoume pou mas kalesate.\n");
        return 0;
    }

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    With all due respect to Mitakeet's post (he correctly pointed out your problems), I would add that looking in my copy of K&R2, it seems that your code should look like the following:

    Code:
    #include<stdio.h>
    main()
    {
    int c,i,nwhite,nother;
    int ndigit[ 10];
    
    nwhite = nother=0;
    for (i=0 ; i<10 ; ++i)
            ndigit[i]=0;
    while((c = getchar()) !=EOF)
                    if (c>='0' && c<='9')
                     ++ndigit[c-'0'];
    
                    else
                            if (c==' ' || c=='\n' || c=='\t')
                     ++nwhite;
    
                            else
                     ++nother;
    
                      printf("Ta psifia einai");
                    for( i = 0 ; i < 10 ; ++i)
                      printf(" %d",ndigit[i]);
                      printf(", ta leyka diastimata einai %d, kai oi alloi xaraktires %d", nwhite, nother);
                      printf(".Sas eyxaristoume pou mas kalesate.\n");
    
                      }
    edit:: Dave, you type too fast!
    Last edited by kermit; 06-29-2005 at 01:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 11:27 AM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  4. Problem with code (newbie question)
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2002, 01:39 AM
  5. newbie with an old book
    By Joseph in forum C Programming
    Replies: 13
    Last Post: 08-28-2001, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21