Solved

Problem with STREAM INPUT for C Program

Posted on 2006-07-20
3
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Passing command line arguments in C 10 101
C Programming - If Statement 8 84
Raspberry Pi 3 to send text message 9 238
What is atomic operation? 6 71
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

740 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