1

Why doesn't it print the result? I've already tapped Ctrl+Z after the input.

code:

#include <stdio.h>
/*histogram of frequences of chars*/
main()
{
    int charIn, numa, numb;
    numa = numb = 0 ;
    while ((charIn == getchar()) != EOF)
    {
        if (charIn =='a')
            ++numa;
        if (charIn =='b')
            ++numb; 
    }
    printf("A:%d\n",numa);
    printf("B:%d\n",numb);
}
e0k
  • 6,961
  • 2
  • 23
  • 30
pjpj
  • 441
  • 1
  • 4
  • 20

4 Answers4

2

Loop is running infinitely. Change

while ((charIn == getchar()) != EOF)

to

while ((charIn = getchar()) != EOF)  

Also note that you are using charIn before initialization in (charIn == getchar()) and it will invoke undefined behavior.

haccks
  • 104,019
  • 25
  • 176
  • 264
1
  • It is not printing output because your while loop is not ending.Try below code.

    #include <stdio.h>
    /*histogram of frequences of chars*/
    int main()
    {
       int  numa, numb, i;
       numa = numb = i = 0 ;
       int charIn;
       while(1)
       {
          while ((charIn = getchar()) != EOF && charIn != '\n')
          {
             if (charIn =='a')
                ++numa;
             if (charIn =='b')
                ++numb;
          }
          printf("A:%d\n",numa);
          printf("B:%d\n",numb);
          numa = numb = 0;
       }
    }
    
Sagar Patel
  • 864
  • 1
  • 11
  • 22
0

First off, change (ch==getchar()) != EOF to (ch=getchar()) != EOF. The first one is comparing ch, which is never initialized, to the value returned by getchar(). And the second one is giving the value returned by getchar() to ch, which is what you actually mean.

Next, note that you can never end an unnested loop using (ch=getchar()) != EOF. see getchar() != EOF. Instead, use (ch=getchar()) != '\n', and it will work.

BTW: Your program will be better if you bother to write int main() and return 0;.

Community
  • 1
  • 1
nalzok
  • 14,965
  • 21
  • 72
  • 139
  • Of course you can. And this isn't the OP's problem, it is a simple typo, == instead of =. – Lundin Jan 25 '16 at 07:58
  • @Lundin Yeah, I notice that now. At least on my machine, I've never ended a loop successfully using `(ch=getchar()) != EOF`. There will always be an extra '\n'. – nalzok Jan 25 '16 at 08:03
  • @Lundin Oh, I know. You are right. – nalzok Jan 25 '16 at 08:04
  • @Lundin Is there any method to end an unnested loop using `(ch=getchar()) != EOF` then? – nalzok Jan 25 '16 at 08:10
0
(charIn == getchar())

Is a condition. == is a comparison operator. You want to set your char though, so use

while ((charIn = getchar()) != EOF)

Additionally, you should initialize your charIn with the rest of your numbers, as failing to do so invoked Undefined behavior.

Now, if you want to stop on newlines and not the EOF char (which is actually not even a char really) then you need to check against newlines, or '\n', so like this:

#include <stdio.h>
/*histogram of frequencies of chars*/
main()
{
    int charIn, numa, numb;
    numa = numb = charIn = 0;

    while ((charIn = getchar()) != '\n')
    {
        if (charIn =='a')
            ++numa;
        if (charIn =='b')
            ++numb; 
    }

    printf("A:%d\n",numa);
    printf("B:%d\n",numb);
}
sehe
  • 374,641
  • 47
  • 450
  • 633
Magisch
  • 7,312
  • 9
  • 36
  • 52