Solved

Problem with STREAM INPUT for C Program

Posted on 2006-07-20
3
201 Views
Last Modified: 2010-04-15
I'm having a problem with a program I wrote that should show the average number of letters per word after EOF (Ctrl 'Z') is entered. I think the program is reading the input as a stream of characters OK. My average is not working. I'd appreciate if someone could identify where I'm going wrong.

----------------------------------------HERE IS MY CODE-----------------------------------------------

#include <stdio.h>

int main()
{

    int ch; /* some character */
    int count=0; /* count letters */

    printf("Enter input ending with ctrl 'Z' or ctrl 'D':\n");

    ch = getchar(); /* get first input */

    /* loop till eof entered */
    while(ch != -1)
    {

       // check for now space
       if(ch > ' ')
         count++; /* increment count */

       ch = getchar(); /* get next input */
    }

    /* report number of letters */
    printf("Number of letters %d\n",count);


    printf("\n");
    getchar(); /* pause */

    return 0;
}


0
Comment
Question by:computerese
  • 2
3 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Hi computerese,

What doesnt work?

Paul
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 500 total points
Comment Utility
Hi computerese,

There's no code for averaging here. To find the average letters per word you need to count the number of words AND count the letters in all the words. Divide one by the other to get the average.

You could do it like this:

       if(ch == ' ')
         spaceCount++; /* increment space count */
       else
         letterCount++;



Paul
0
 

Author Comment

by:computerese
Comment Utility
Thanks Mr. Caswell, that fixed my problem!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now