Solved

Problem with STREAM INPUT for C Program

Posted on 2006-07-20
3
221 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
ID: 17150504
Hi computerese,

What doesnt work?

Paul
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 500 total points
ID: 17150522
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
ID: 17150870
Thanks Mr. Caswell, that fixed my problem!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

816 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

9 Experts available now in Live!

Get 1:1 Help Now