Text justification

I need some guidance in this code,how can i start off with. As there is not much material on internet about this type of code so i need some help from you guys. thanks.


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
                       <<Header file>


#include <iostream>
#include <string>
#include <vector>
#include "ctype.h"

using namespace std;

// some useful type definitions
typedef vector<string> PARA;
typedef unsigned int ERROR;

#define MIN_COL_WIDTH 20
#define MAX_COL_WIDTH 60

// error codes
#define ERROR_NONE 0
#define ERROR_NO_CHARS 1
#define ERROR_ILLEGAL_WIDTH 2

// function prototype
ERROR justify(unsigned int width, string in, PARA *out);
// width is the desired width of line in output (justified) text.
// "in" is the input string.
// PARA is a vector of strings, where each element is one line
// in the output justified text.
// The first element of the PARA vector is the first line of the 
// output text, the second element is the second line, and so on.
// note that output text variable is declared in main function.
// it is passed by reference to "justify".
// The function returns one of the above error codes.

                         <<Main file>>
#include "justify.h"

int main() {
	ERROR result;
	unsigned int width;
	string input;
	PARA output;

	while (1) {
		// first clear the output string
		output.clear();
		cout << endl<< "Welcome to text justification!" << endl << endl;
		cout << "Enter the output line width (between " << MIN_COL_WIDTH << " and " << MAX_COL_WIDTH << ", or 0 to exit): ";
		cin >> width;
		if (width == 0)
			break;
		cout << "Enter the string to justify below:" << endl << endl;

		// read in the input string 
		cin.ignore();

		getline(cin, input);

		// justify the input for given column width
		result = justify(width, input, &output);

		switch(result) {
			case ERROR_NONE: 
				cout << endl << "The justified text is:" << endl << endl;
				for (unsigned int i = 0; i < output.size(); i++)
					cout << output[i] << endl;
				break;
			case ERROR_NO_CHARS:
				cout << "ERROR: No characters in input para!" << endl;
				break;
			case ERROR_ILLEGAL_WIDTH:
				cout << "ERROR: Column width out of range!" << endl;
				break;
			default:
				cout << "ERROR: Internal error!" << endl;
				break;
		}
	} 
}


                         <<Implementation file>>

#include "justify.h"

// implement the justify function below
// use push_back method of string class to insert a new character
// at the end of a string
// You can terminate a string by calling push_back on it with '\0'.
// Use the clear method of string class to remove all the
// characters from the string.

ERROR justify(unsigned int width, string in, PARA *out) {

	
	//  make sure that column width is valid

	// make sure that the input is not empty string

	// justify the text!

} // end function justify 
In your implementation file:
1. Check if width is not between minimum and maximum. If it is not, return ERROR_ILLEGAL_WIDTH
2. Check the length of the input string (in). If 0, return ERROR_NO_CHARS
3. If you know how to handle errors wit try catch (see for example http://www.cplusplus.com/doc/tutorial/exceptions/) you can use that around your main code block in the justify function. If not, just ignore this suggestion
4. Clear the out, just to make sure it does not contain anything
5. Get the length of the in string, decide how many strings will be in the out, then start a loop filling them
Topic archived. No new replies allowed.