Formatting Text

we have to read text from an input file and format it by putting so many characters on a line (keeping words together). my problem is that my program counts a space after each character and doesn't put up the right amount of characters.

the test file we use has the alphabet's in order with a space between each letter. when I say that I need three letters on a line it only outputs one but when I say i need four characters on a line it outputs two.

here is what i have:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream input_file;
	ofstream output;
	char input_name [101], word [51];
	int length=0, max_length, length_count;

	cout << "What is the name of the file you want formated? ";
	cin >> input_name;

	input_file.open (input_name);

	if (input_file.fail ())
	{
		cout << "Unable to find the file\n";
		exit (1);
	}

	cout << "How many characters in each line? ";
	cin >> max_length;

	length_count = max_length;

	while (input_file >> word)
	{	
		length = strlen (word) +1;

		
		if (length <= length_count)
		{
			cout << " "<< word;
			length_count = length_count - length;
		}
		
		else if (length > length_count)
		{
			cout << endl << word;
			length_count = max_length;	
			length_count = length_count - length;
		}
	}
	

	input_file.close ();

	cout << endl;

	return 0;
}


thank you
Topic archived. No new replies allowed.