#C issue can't print out anything after compiling program within usage character input and output

15 messages · Page 1 of 1 (latest)

outer totem
#

Recently, I was learning C programming just a hobby and followed the instruction inside of a book by B.K. & Dennis Ritchie 2nd edition. in many section I've just encountered about character input and output, up to 1.6 section which explains arrays also counting a special character in many ways. Before jumped into arrays section specifically 1.5.2 up to 1.5.4 that is explaining getchar() and putchar() usage. however, some cases had been finished and it returns its input to output and never terminate a.out after putting character that returns which means output can't capture an input into printf(). This is my code which is followed in the book.

#include <stdio.h>

/* count digits, white space, others */

int main()
{
  int c, i, nwhite, nother;
  int ndigit[10];

  nwhite = nother = 0;
  for (i = 0; i < 10; ++i) {
    ndigit[i] = 0;
  }

  while ((c = getchar()) != EOF){
    if (c >= '0' && c <= '9'){
      ++ndigit[c-'0'];
    } else if (c == ' ' || c == '\n' || c == '\t') {
      ++nwhite;
    } else {++nother;}
  }
  printf("digits =");
  for (i = 0; i < 10; ++i){
    printf("%d", ndigit[i]);
  } printf(", white space = %d, other = %d\n", nwhite, nother);

   return 0;
}

the code is a part of arrays section example to count the number of occurrences of each digit, white space characters and all characters. but when I try it that's not returning a value which is included within printf()

my output:

$./a.out
for examplee

the output from my file can't be terminating its self except forcibly and doesn't take anything. its output should be like this.

digits = 9 3 0 0 0 0 0 0 0 1 white space = 123, other = 345
cursive duneBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hollow hull
#

however, some cases had been finished and it returns its input to output and never terminate
what do you mean it returns its input to output

please give input data that you had problems with

echo 'problem input
data here
1234' | ./your_program

outer totem
#

I mean when I try to put character. The input just return a looping input many times that terminates without capture a value of printf() execution.

hollow hull
#

it gets stuck in the loop?
run your program
you input your data
and you press control+D (which means EOF)
right?

#

cause if there is EOF in your input it would be impossible for this program to enter in infinite loop

outer totem
hollow hull
#

m8

#

you pressed Control+C

#

press Control+D

#

also nice sound

outer totem
#

my apologize:D

#

thanks for your help it solved 🙂

#

!solved