//File: justify.cpp //Date: 3/2019 //Programmer: Dr. J. Maletic //Description: Text justifcation program. #include #include #include ///////////////////////////////////////////////////// 1 void open_input_file(ifstream &in) 2 { char filename[80]; 3 cout << "Enter Input File Name: "; cin >> filename; in.open(filename); 4 } ///////////////////////////////////////////////////// 5 void open_output_file(ofstream &out) 6 { char filename[80]; 7 cout << "Enter Ouput File Name: "; cin >> filename; out.open(filename); 8 } ///////////////////////////////////////////////////// //PRE: Assigned(input) && Assigned(output) && // Assigned(line_size) //POST: Contents of input is written to output and // justified to line_size. 9 void justify_file(ifstream &input, ofstream &output, int line_size) { 10 string word, 11 buffer; 12 string leading_blanks(" "); 13 while (input >> word) { 14 if ((buffer.length() + 15 word.length() + 1) <= line_size) 16 if (buffer == "") 17 buffer = word; 18 else 19 buffer = buffer + " " + word; 20 else { 21 buffer = buffer.justify(line_size); 22 output << leading_blanks << buffer << endl; 23 buffer = word; 24 } 25 } 26 output << leading_blanks << buffer << endl; 27 } ///////////////////////////////////////////////////// 28 int main () { 29 int line_size; 30 ifstream input; 31 ofstream output; 32 open_input_file(input); 33 open_output_file(output); 34 cout << "Enter line size: "; 35 cin >> line_size; 36 justify_file(input, output, line_size); 37 return 0; 38 }