tags:

views:

186

answers:

5

I ran the following code, but nothing came up on the console....

#include <stdio.h>

#define MAXWORDLEN 10

int main(void)
{
  int c;
  int inspace = 0;
  long lengtharr[MAXWORDLEN + 1];
  int wordlen = 0;

  int firstletter = 1;
  long thisval = 0;
  long maxval = 0;
  int thisidx = 0;
  int done = 0;

  for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
  {
    lengtharr[thisidx] = 0;
  }

  while(done == 0)
  {
    c = getchar();

    if(c == ' ' || c == '\t' || c == '\n' || c == EOF)
    {
      if(inspace == 0)
      {
        firstletter = 0;
        inspace = 1;

        if(wordlen <= MAXWORDLEN)
        {
          if(wordlen > 0)
          {
            thisval = ++lengtharr[wordlen - 1];
            if(thisval > maxval)
            {
              maxval = thisval;
            }
          }
        }
        else
        {
          thisval = ++lengtharr[MAXWORDLEN];
          if(thisval > maxval)
          {
            maxval = thisval;
          }
        }
      }
      if(c == EOF)
      {
        done = 1;
      }
    }
    else
    {
      if(inspace == 1 || firstletter == 1)
      {
        wordlen = 0;
        firstletter = 0;
        inspace = 0;
      }
      ++wordlen;
    }
  }

  for(thisval = maxval; thisval > 0; thisval--)
  {
    printf("%4d  | ", thisval);
    for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
    {
      if(lengtharr[thisidx] >= thisval)
      {
        printf("*  ");
      }
      else
      {
        printf("   ");
      }
    }
    printf("\n");
  }
  printf("      +");
  for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
  {
    printf("---");
  }
  printf("\n       ");
  for(thisidx = 0; thisidx < MAXWORDLEN; thisidx++)
  {
    printf("%2d ", thisidx + 1);
  }
  printf(">%d\n", MAXWORDLEN);

  return 0;
}
A: 

I am curious if you mean getch() as being a problem.

4 mths ago there was a discussion regarding getch() that may be of use: http://stackoverflow.com/questions/814975/getch-is-deprecated

In the second answer getchar() is suggested, so it shouldn't be returning an error, but that also depends on which compiler you are using.

James Black
+1  A: 

getchar() is a macro function. Maybe you're missing the right include file.

In a pinch,

#ifndef getchar
#define getchar() getc(stdin)
#endif
Joshua
Isn't it `fgetc`?
strager
Joshua
+3  A: 

getchar is still an int by the POSIX and C standards. Did you forget to include stdio.h or include something that redefines it? This example works for me:

#include <stdio.h>
int main()
{
  int c;
  c = getchar();
}
Greg Smith
+1  A: 

http://codepad.org/Fp8kmzbv

It compiles and runs fine @codepad. What is your compiler, your compile options, and how are you running it?

And running it on my home machine:

$ gcc-trunk --version
gcc-trunk (GCC) 4.5.0 20090907 (experimental)

$ gcc-trunk -Wall -ansi -Wall -W -pedantic tt.c
tt.c: In function ‘main’:
tt.c:73:5: warning: format ‘%4d’ expects type ‘int’, but argument 2 has type ‘long int’

$ ./a.out
This is a test of your histogram program.
   3  |          *
   2  |    *     *
   1  | *  *     *           *  *
      +---------------------------------
        1  2  3  4  5  6  7  8  9 10 >10

So it should still do what you want it to do, assuming you're running it correctly (as a console application within a console).

Nick Presta
A: 

maybe you have to call fflush(stdout) at the end of your program to flush console output.

codymanix