无限循环问题 [英] infinite loop problem

查看:76
本文介绍了无限循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ubuntu 10.04中编译了以下程序,但进入了无限循环.
请帮助我找到问题.
并告诉你什么是EOF..

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..

#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]);
	}
}

推荐答案

您的无限循环是因为内部循环仅在c=='' ''c==''\t''时才结束.永远不会发生...
Your infinite loop is because the inner loop is ended only when c=='' '' and c==''\t''. It will never happen...


while 子句中的变量 count 施加条件:

计数< 20
put a condition on the variable count in the while clause :

count < 20


while(c!=' ' && c!='\t')


不能同时等于这两个字符都是有效字符.


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


这篇关于无限循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆