Line Justify a input and output file

So with the help of you guys earlier in the week a program was written to "line justify" an input text. Now I have to use an input file and justify that by making every line exactly 75 characters long (keep this value in a named constant). To get to this line length, extra space is first added after punctuation marks. You need to consider only these marks: .,!?; That is, every punctuation mark may be followed by two spaces. If the spaces after punctuation marks are added and the line is still less than 75 characters, additional spaces are added after random words in the line.

The output file should be named "Justified.txt"
1
2
3
4
5
6
7
8
9
10
  //input file
Is Lorem Ipsum just dummy text of the printing and typesetting 
industry? Lorem  Ipsum has been the industry's standard dummy text 
ever since the 1500s, when an unknown printer took a galley of type 
and scrambled it to make a type specimen book. It has survived not 
only five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged. It was popularised in the 1960s 
with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus 
PageMaker using Lorem Ipsum. 

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
54
55
//What I have so far but I don't know how to get the file to input into program to edit each line
#include <string>
#include <iostream>
#include <limits>  
#include <fstream>
using std::cout; using std::cin; using std::endl; using std::string; using std::ifstream; using std::ofstream;
const int maxwidth = 75;
const int minwidth = 40;

int main() {
	string s;
	int pos = -1;

	string unjustified;
	ifstream myFile("unjustified.txt");
	myFile.open("unjustified.txt");
	if(myFile.is_open()) 
	{
		getline(myFile, unjustified);
		unjustified = s;
	}
	myFile.close();


	if (s.length() < minwidth || s.length() > maxwidth) {
		cout << "Incorrect, The string is not between 40 and 75 characters.";
		cout << "\nit was: '" << s.length() << "' characters long";
	}
	else {
		pos = s.find_first_of(".,?!;");
		while (pos != string::npos) {
			s.insert(pos, " ");
			pos = s.find_first_of(".,?!;", pos + 2);
		}
		if (s.length() < maxwidth && s.length() > minwidth) {
			while (s.length() < 75) {
				pos = s.find_first_of(' ');
				while (pos != string::npos && s.length() < 75) {
					if (!(isspace(s[pos - 1])))
						s.insert(pos, "  ");
					pos = s.find_first_of(' ', pos + 2);
				}
			}
		}
		ofstream outFile("justified.txt");
		outFile.open("justified.txt");
		if (outFile.is_open())
		{
			outFile << s;
			cout<<s;
		}
		outFile.close();
	}
	cin.get();
}

Have a closer look at line 20.
Do you really want to replace your input with an empty string?
On line 25 the condition will always be false since you never put anything into the s.
Also lines 16 and 46 are redundant, the constructor will open the file automatically.
Topic archived. No new replies allowed.