RE:程序错误,无限循环 [英] RE: error in program , infinite loop

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

问题描述

以下程序执行时没有错误,但在输入文本时给出了无限循环..



我尝试了什么:



  #include   <   iostream.h  >  
#include < conio.h >
#include < string.h >
#include < stdio.h & gt;
int 更改( char []);

void main()
{
int i,y,j = 0 ,w;
char str [ 100 ],word [ 1000 ];
clrscr();
cout<< 输入文字<< endl;
得到(字);
cout<< 块之前的字符串<< endl;
puts(word);
cout<< 块后面的字符串<< endl;
for (i = 0 ; word [i]!= ' \0'; ++ i)
{
if (word [i]!= ' '
{
str [j] = word [i];
j ++;
}
if (word [i] == ' '
{
w = change(str);
if (w == 1
{
for (i = 1 ; i< j- 1 ; ++ I)GT;
{
str [i] = ' *';
}
cout<< str<< ;
j = 0 ;
}
如果(w == 0
{
cout<<< str<< ;
j = 0 ;
}
}
}
getch();
}

int 更改( char str [])
{

if ((strcmp(str, poop)== 0 )||(strcmp(str, bad)== 0 ))
return 1 ;
else return 0 ;
}

解决方案

缩进确实非常有用,当您编码时:它可以更简单地发现正在发生的事情:

  for (i =  0  ; word [i]!= '  \0'; ++ i)
{
if (word [i]!= ' '
{
str [j] = word [i];
j ++;
}
if (word [i] == ' '
{
w = change(str);
if (w == 1
{
的类=code-keyword>(i = 1 ; i< j - 1 ; ++ i)
{
str [i] = ' *';
}
cout<< str<< ;
j = 0 ;
}
如果(w == 0
{
cout<<< str<< ;
j = 0 ;
}
}
}



在这种情况下,显然你会使用相同的变量对于两个嵌套循环...这意味着在第二个循环之后,i的值将始终相同。

如果您使用了调试器,那也会使它变得非常明显!


the following program executes without bug but give an infinite loop on entering text..

What I have tried:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
int change(char[]);

void main()
{
	int i,y,j=0,w;
	char str[100],word[1000];
	clrscr();
	cout<<" enter text"<<endl;
	gets(word);
	cout<<"The string before block"<<endl;
	puts(word);
	cout<<"The string after block"<<endl;
	for(i=0;word[i]!='\0';++i)
	{
		if(word[i]!=' ')
		{
			str[j] = word[i];
			j++;
		}
		if(word[i]==' ')
		{
			w=change(str);
			if(w==1)
			{
				for(i=1;i<j-1;++i)>
				{
					str[i]='*';
				}
				cout<<str<<" ";
				j=0;
			}
			if(w==0)
			{
				cout<<str<<" ";
				j=0;
			}
		}
	}
	getch();
}

int change(char  str[])
{

	if((strcmp(str,"poop")==0)||(strcmp(str,"bad")==0))
	return 1;
	else return 0;
}

解决方案

Indentation really, really helps when you code: it makes it a lot simpler to spot what is going on:

for (i = 0; word[i] != '\0'; ++i)
    {
    if (word[i] != ' ')
        {
        str[j] = word[i];
        j++;
        }
    if (word[i] == ' ')
        {
        w=change(str);
        if (w == 1)
            {
            for (i = 1; i < j - 1; ++i)
                {
                str[i] = '*';
                }
            cout<<str<<" ";
            j = 0;
            }
        if (w == 0)
            {
            cout<<str<<" ";
            j = 0;
            }
        }
    }


In this case, it would make it obvious that you are using the same variable for two nested loops...which means that after the second loop, the value of i will always be the same.
And if you'd used the debugger, that would also have made it pretty obvious!


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

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