#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    How come this character counting histogram won't execute?


    Hi

    I am a beginner at C programming, and I am going through the K&R book page by page.

    Currently I'm stuck at exercise 1-13, where I'm supposed to make a program that counts the number of letters in each word in a sentence, and print the results on a horizontal histogram. In this code I'm supposed to use getchar() and arrays.

    When I run my code and type a sentence, I get an exact copy of that sentence printed out. For some reason, my array is not being printed out.

    Any help would be greatly appreciated.

    ========================================


    #include <stdio.h>

    main()
    {
    int c, position, i;
    int length[10]; //array declared

    position = 0;
    for (i = 0; i < 10; ++i) //sets all the values in the array to zero
    length[i] = 0;

    while ((c = getchar()) != EOF){
    if (c != ' ' || c != '\n' || c != '\t'){ //if getchar() does not read a space, tab, or newline (aka, reads a letter), print the character
    putchar(c);
    ++position;
    length[position - '0'] = '*'; //array counts letter by replacing an 0 with a *.
    }
    else { //if getchar() encounters a space, tab, or newline, print the array beside the previous word.
    printf(" ");
    for (i = 1; i < 11; ++i)
    printf("%d", length[i]);
    printf("\n");
    for (i = 0; i < 10; ++i) //set all values in array to zero again
    length[i] = 0;
    position = 0;
    }
    }
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Your code is an unreadable mess! Indent it and use code tags!

    Also, please abandon K&R indent style, which hides open braces at the ends of lines and even in the middle of lines.

    Here is your code using code tags and the Allman style:
    Code:
    #include <stdio.h>
    
    main()
    {
        int c, position, i;
        int length[10]; //array declared
    
        position = 0;
        for (i = 0; i < 10; ++i) //sets all the values in the array to zero
            length[i] = 0;
    
        while ((c = getchar()) != EOF)
        {
            if (c != ' ' || c != '\n' || c != '\t') //if getchar() does not read a space, tab, or newline (aka, reads a letter), print the character
            {
                putchar(c);
                ++position;
                length[position - '0'] = '*'; //array counts letter by replacing an 0 with a *.
            }
            else  //if getchar() encounters a space, tab, or newline, print the array beside the previous word.
            {
                printf(" ");
                for (i = 1; i < 11; ++i)
                    printf("%d", length[i]);
                printf("\n");
                for (i = 0; i < 10; ++i) //set all values in array to zero again
                    length[i] = 0;
                position = 0;
            }
        }
    }
    Much more readable, wouldn't you agree?

    Look at this line and you will see two mistakes:
    if (c != ' ' || c != '\n' || c != '\t'){ //if getchar() does not read a space, tab, or newline (aka, reads a letter), print the character

    1. Do you see that open brace highlighted in red? Do you not see how it is hidden in the middle of the line? If you expect everybody to go off on a frustrating easter-egg hunt every time they try to read your code, then you will find fewer and fewer programmers willing to suffer that idiocy. I don't care that K&R quite literally wrote the book; K&R style is just plain stupid! The only hope it ever has to work is for you to be very diligent about indenting your code, which you're not doing!

    2. Please present to me one input character would make that if condition evaluate to false. Just one, that's all I ask. You can't do it, can you? That condition will always evaluate to true, which means that the else-block, the code that prints your array, will never ever execute.

    You need to re-think that condition.

    Comments on this post

    • ThePrince agrees : thanks for sorting out my code and finding the mistake
    Last edited by dwise1_aol; May 28th, 2013 at 06:31 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Ohhh yeaa... :chomp: Thanks for helping me out dwise!!!

IMN logo majestic logo threadwatch logo seochat tools logo