Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the following program executes without bug but give an infinite loop on entering text..

What I have tried:

C++
#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;
}
Posted
Updated 22-Jun-16 10:40am
v3
Comments
phil.o 22-Jun-16 7:13am    
Correct identation of your code would make your life, and ours, so much easier :)

1 solution

Indentation really, really helps when you code: it makes it a lot simpler to spot what is going on:
C++
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!
 
Share this answer
 
Comments
nv3 22-Jun-16 18:19pm    
+5

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