Click here to Skip to main content
15,881,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i compiled the following program in ubuntu 10.04, but it went into an infinite loop.
please help me find the problem.
and also tell what EOF is..
C
#include<stdio.h>
void main()
{
	int nwords[20];
	int count=0, i, c;
	for(i=0;i<20;i++)
	{
		nwords[i]=0;
	}
	while((c=getchar())!='\n')
	{
		while((c!=' ')||(c!='\t'))
		{
			nwords[count]++;
			c=getchar();
		}
		count++;
	}
	for(i=0;i<20;i++)
	{
		printf("word no. %d : %d letters", (i+1), nwords[i]);
	}
}
Posted
Updated 21-Dec-11 21:56pm
v3

Your infinite loop is because the inner loop is ended only when c==' ' and c=='\t'. It will never happen...
 
Share this answer
 
Comments
kr_harsha 22-Dec-11 3:54am    
even when i give spaces between words, this condition won't be satisfied? for instance, if the input is-
my name is sam

please explain..
Shmuel Zang 22-Dec-11 8:48am    
When your input is space (c==' '), the c!='\t' condition is true. So, your loop's condition is always true. Maybe you want to change your condition to c!=' ' && c!='\t', like Richard wrote.
CPallini 22-Dec-11 3:58am    
Well spotted, my 5.
Shmuel Zang 22-Dec-11 8:48am    
Thanks
Albert Holguin 22-Dec-11 11:11am    
+5
put a condition on the variable count in the while clause :

count < 20
 
Share this answer
 
C++
while(c!=' ' && c!='\t')

It must be not equal to both of these to be a valid character.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900