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.
<<Header file>
#include <iostream>
#include <string>
#include <vector>
#include "ctype.h"
usingnamespace std;
// some useful type definitions
typedef vector<string> PARA;
typedefunsignedint 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(unsignedint 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;
unsignedint 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 (unsignedint 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(unsignedint 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