Infinite Loop?

I am trying to make a function that trims a body of text to a max of 80 characters (for console printing) per line. If the line is > 80 characters it should change the last three characters to ... and put a new line character. Here is the code I have for the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
char console_format(char string[])
{
	int l = 0; //Line position
	int i = 0; //Offset
	char strout[sizeof(string)];

	while ( i+l <= sizeof(string) )
	{
		for (l = 0; string[i+l] != '\n'; l++)
		{
			if (string[i+l] == '\n')
			{
				strout[i+l] = '\n';
				break;
			}
			else if (l == 80)
			{
				strout[i+l-0] = '.';
				strout[i+l-1] = '.';
				strout[i+l-2] = '.';
				strout[i+l+1] = '\n';
				break;
			}
			else
				strout[i+l] = string[i+l]; 

		}
		i += l; // Add the length of the last line to the offset
	}
	return (*strout);

}
It never reaches the 'i += l' line.
Thanks,
Frankie
Last edited on
*Infinite
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void console_format(char* string)
{
  int i = 0, j = 0;

  for (; i < strlen(string); i++, j++)
  {
    if (string[i] == '\n' && j == 80)
    {
      string[i] = '\n';
      string[i - 1] = '.';
      string[i - 2] = '.';
      string[i - 3] = '.';
      j = 0;
    }
  }
}

You're using a linear array, so don't use more than one position variable.
Last edited on
Your code doesn't work for me. This function has to handle text with lines that can be more, less or exactly 80 characters. Only if it is >80 should it do any replacing.
If it is more than 80 characters, after it finds the 80th character shouldn't it skip the rest of the line by moving i to the start of the next line? Here is my attempt at showing what I mean with code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char* console_format(char* string)
{
  int i = 0, j = 0;

  for (; i < strlen(string); i++, j++)
  {
    if (string[i] == '\n' || j == 80) //Shouldn't this be or?
    {
      string[i] = '\n';
      string[i - 1] = '.';
      string[i - 2] = '.';
      string[i - 3] = '.';
	  for (; string[i] != '\n'; i++) //Go to end of line
		  continue;
      j = 0;
    }
  }
  return string; //I want the string to be returned for use with cout
}

This code here just crashes when I try to run it. It compiles okay though.
With your code it runs fine, but doesn't make any change to the input. It just returns the same string I passed. This was after changing the return type and the return statement.
Last edited on
Bump!
That's a really hard way to accomplish what you want. Are you able to use std::string?
If by std::string you mean the string library, yes. I don't mind including it.
Then use std::string::find() to find the first \n. If it is more than 80 characters
out, then copy just the first 80 characters, append the ellipsis, and repeat.
Note that find() has a parameter that allows you to tell it where to start searching.
Obviously you'd do the second search beginning where the last one left off.
Topic archived. No new replies allowed.