[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with STREAM INPUT for C Program

Posted on 2006-07-20
3
Medium Priority
?
252 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

872 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