Horizontal histogram won't accept input after the first input

Posted by vincentbelkin on Stack Overflow See other posts from Stack Overflow or by vincentbelkin
Published on 2012-03-26T11:24:28Z Indexed on 2012/03/26 11:30 UTC
Read the original article Hit count: 255

Filed under:

So I'm making a program which is supposed to print a horizontal histogram of the lengths of words in its input. I don't know if most of it is OK since the main problem is it won't accept any input after the first one. Oh I also put comments on the parts I'm having some trouble with, like how to print "-" multiple times in order to represent histogram. I've tried making other versions of the code but I couldn't check if I'm close to getting it because again it won't accept another input after the first input.

/*Write a program to print a histogram of the lengths of words in its input. 
It is easy to draw the histogram with the bars horizontal*/
#include <stdio.h>
#define MAX 30
#define IN 1
#define OUT 0
int main()
{
    int a,c,i,k,state,word[MAX];
    a=0;
    k=0;
    state=OUT;
    for(i=0;i<MAX;i++)
        word[i]=0;
    while((c=getchar())!=EOF)
    {
        if(c==' '||c=='\t'||c=='\n')
            state=OUT;
        else
            state=IN;
        while(state==IN)
            a++;
        if(state==OUT)
            {
            word[i]=a;
            i++;
            }

    /*This part is hard for me, I don't know how to print X multiple times!*/
    if((c==getchar())&&c==EOF)
    {
    for(i=0;i<MAX;i++)
        {
            for(i=0;i<=word[i];i++)
                 putchar('-'); 
                putchar('\n');
    }
}
}
}

© Stack Overflow or respective owner

Related posts about c