Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 527667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:54:26+00:00 2026-05-13T08:54:26+00:00

I’m a total C n00b trying to teach myself C off K&R. My question

  • 0

I’m a total C n00b trying to teach myself C off K&R. My question is kind of embarrassingly elementary. OK, here goes: I can’t get programs using getchar to give the kind of output I expected. If you happen to have K&R on hand, I’m stuck on exercise 1.13. The question goes, “Write a program to print a histogram of the lengths of words in its input. ” and I can’t even tackle the horizontal version because of this issue I’m having.

I’m on XP using Dev-C++ (mingW compiler) and running programs off the command line. My issue is, when I try to run my program, it waits for me to enter characters to scan from, but when I’m done inputting and hit Enter, it doesn’t do anything. I expect it to go ahead and print the histogram as I expected. In reality, it doesn’t even seem to count up word lengths, because as you can see in the code, when I try to print what’s in the ctr array just to see if it contains anything, nothing prints.

I’m so n00b that I have no idea if it’s my code or the command line that’s at fault. But I suspect it’s something with the system, because when I try to compile and run a model program, the same thing happens. Type in input, hit Enter, nothing happens. If I Ctrl-C, sometimes it spits out an asterisk or two that looks nothing like the model output. Other times, it doesn’t do anything (just goes right back to the prompt).

Here’s my code for the exercise. I’ve spent an entire day on this and am questioning my ability to carry on with programming. I’d really, really appreciate it if anyone could get me out of this hole!

Also, I have another question about the model program I mentioned above, but I think I should post it in its own question. Thanks all 🙂

#include <stdio.h>

//#define 1 IN
//#define 0 OUT
int main () {
    //start w/ state = OUT
    int c = 0;
//    int state = OUT;
    int len = 0;
    int ctr[12];
    int i, j;
    i = j = 0;

    for (i = 0; i <12; i++)
        ctr[i] = 0;
    while ((c = getchar()) != EOF)
       if (c != ' ' && c != '\t' && c != '\n') {
//            state = IN;
            len++;
            printf("%d", len);
            }
       else {
            ctr[len]++;
            len = 0;
            }            
    for (i = 0; i <12; i++) 
        printf("%d\n", ctr[i]);
    for (i = 0; i <12; i++) {
        printf("%d\n", i);   
        for (j = 0; j <= ctr[i]; j++)
            printf("-");
        printf("\n");
        }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:54:26+00:00Added an answer on May 13, 2026 at 8:54 am

    Your while loop is looking for EOF which stands for end-of-file, not end-of-line.

    On Windows, you need to type ctrl-z to simulate end-of-file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.