Ok, so for this assignment, we were given a program and a class. We have to find where the program code needs modified and make the changes necessary to use the class.

/******************************************************************************
Programmer: 
Date: November 6, 2011
Compiler: Cisual C++.NET
Source File: hw7.cpp
Action: Reads words from stdin and displays them in 3 columns. See function
ReadWord for the definition of a "word". Reading from stdin input need to 
manual enter end of file marker, ctrl-zx, then enter.  NOTES: The program does
not handle correctly wirds longer than MAX_WORD_LENGTH. If such words are
encountered, they are truncated.  Only as many as MAX_WORD_COUNT words can be
stored in the array holding the words.
******************************************************************************/

#include <iostream>
#include <ctype.h>
#include <iomanip>
#include "string2.cpp"
using namespace std;

const int MAX_WORD_LENGTH = 254;	//The type definition below permits much
				//easier prototyping of functions that pass
				//arrays of strings.

typedef char String[MAX_WORD_LENGTH+1]; //+1 for null terminator

void ReadWord(String Word, int MaxLength);
void WriteWords(String Word[], int Count[], 
			    int TotalWordCount, int DistinctWordCount);
void StoreWord(String NewWord, String Word[], int Count[], int &TotalWordCount,
			   int &DistinctWordCount, int MaxWordCount);

/************************** main *********************************************/

void main()
{
	const int MAX_WORD_COUNT = 130;

	String NewWord, Word[MAX_WORD_COUNT+1] = {"",""}; //ALWAYS allow 1 extra

	int Count[MAX_WORD_COUNT+1] = {0},			 //Initialize array to zeros
		DistinctWordCount = 0, TotalWordCount = 0;

	ReadWord(NewWord, MAX_WORD_LENGTH);			//Get the first word

	while (NewWord[0] != 0)				//while NewWord is not null string
	{
		StoreWord(NewWord, Word, Count, TotalWordCount, DistinctWordCount,
				  MAX_WORD_COUNT);

		ReadWord(NewWord, MAX_WORD_LENGTH);		//Get new word
	}

	WriteWords(Word, Count, TotalWordCount, DistinctWordCount);
}

/************************ReadWord**********************************************
DESCRIPTION:	
	Reads a word from standard input and stores in the array Word. For the 
	purposes of this routine, a "word" is any contiguous sequence of non-blank
	characters.

PARAMETERS:
	Word		An array of characters
	MaxWordLength	The maximum number of chars to store in NewWord 
			(not including the null terminator). Chars beyond this 
			number are discarded.

CALLS:
	cin.get and cin.good, both in the iostram library. isspace from ctype file.

NOTE:
	Words longer than MaxWordLength are truncated.

-----------------------------------------------------------------------------*/

void ReadWord (String NewWord, int MaxWordLength)
{
	char Ch;
	int K;

	cin >> Ch;		//skip blanks and get the first letter of the word

	K = 0;

	while (cin.good() && K< MaxWordLength && !isspace(Ch)) //stores chars
	{
		NewWord[K++] = Ch;
		cin.get(Ch);
	}

	NewWord[K] = 0;	//adds null terminator

	while (cin.good() && !isspace(Ch)) //discards tail end of long words
		cin.get(Ch);
}

/***********************StoreWord**********************************************
DESCRIPTION:
	Adds a string to NewWord (holding up to MAX_WORD_LENGTH characters) to the
	end of the array Word.

PARAMETERS:
	NewWord				The string (array of char) to be added.
	Word				An array of String (char[MAX_WORD_LENGTH).
	DistinctWordCount	The number of different words in the array.
	MaxWordCount		The max number of words that can be stored in the array
						Word. If DistinctWordCount == MaxWordCount, the function
						is exited.
-----------------------------------------------------------------------------*/

void StoreWord (String NewWord, String Word[], int Count[], int &TotalWordCount
				, int &DistinctWordCount, int MaxWordCount)
{
	int i, k =0;

	while (strcmp(NewWord, Word[k]) > 0 && k < DistinctWordCount)
		++k;	//Assert: k is NewWord's correct position in the ordered array

	if (strcmp(NewWord, Word[k]) == 0)	//NewWOrd is already there
	{
		++Count[k];
		++TotalWordCount;
	}
	else 
		if (DistinctWordCount < MaxWordCount)	//room for a new word
		{
			++DistinctWordCount;  //if this line reached, found new word
			++TotalWordCount;

			for (i = DistinctWordCount-1; i > k; --i) //Make room for NewWord
			{
				 strcpy (Word[i], Word[i-1]);
				 Count[i] = Count[i-1];
			}

		strcpy (Word[k], NewWord);		//Store NewWord in array
		Count[k] = 1;
		}
}

/*******************************WriteWords*************************************
DESCRIPTION:	
	Writes the strings in the array Word to stadard output. The words are left
	justified. After WORDS_PER_ROW words have been written a new line is 
	started.

PARAMETERS:
	Word			An array of String(char[MAX_WORD_LENGTH])
	WordCount		The number of words in the array

-----------------------------------------------------------------------------*/

void WriteWords (String Word[], int Count [], int TotalWordCount, 
				 int DistinctWordCount)
{
	const char *Header1 = "Word             Count ", *Header2 = "|",
				*Seperator = " | ";

	const int WORD_FW = 17, COUNT_FW = 4, WORDS_PER_ROW = 3;

	int C;

	cout << "\n\n";

	if (TotalWordCount == 0)
		return;

	for (C = 1; C <= WORDS_PER_ROW; ++C)
		cout << Header1;
		
	cout << endl;

	for (C = 1; C <= WORDS_PER_ROW; ++C)
		cout << Header2;

	cout << endl;

	int N = 0;

	while (N < DistinctWordCount)
	{
		for (C = 1; C <= WORDS_PER_ROW && N < DistinctWordCount; ++C)
		{
			cout << setiosflags(ios::left) << setw(WORD_FW) << Word[N];
			cout << setiosflags(ios::right) << setw(COUNT_FW) << Count[N];
			cout << Seperator;
			cout << resetiosflags (ios::right);
			++N;
		}

		cout << endl;
	}

	cout << "\nTotal Word Count : " << TotalWordCount << endl;
	cout << "\nDistinct Word Count : " << DistinctWordCount << endl;
}
/*                                   string2.cpp

A C++ class definition that imporoves on the String type defined in 
String1.CPP. Recall that implementation if the class String used an 
array of char to hold the characters in the String and a length field to keep 
track of the number of characters in the string. Several new member functions
are now implemented, including 

=		: Overloaded "=" that replaces Assign().
<<		: Overloaded "<<" that replaces Write().
>>		: Overloaded ">>" that reads a word from standard input.
<		: Compares two strings to see if first is less than second.
!=		: Tests to see if two strings are different.
==		: Tests to see if two strings are the same. 
+		: Concatenates the characters in two strings.

NOTE: This is clearly a partial implementation of String, since many important
operators remain to be written. These include >, <=, >=, +=, and pattern 
matching.
=============================================================================*/

#include <iostream>
#include <ctype.h>
using namespace std;

class String
{
	enum {MAX_LENGTH = 255};		//max value of an unsigned char

	unsigned char Chr[MAX_LENGTH];	//Holds the chars in a string

	unsigned char Len;				//Current length of a string

public:

	int Length() const				//"const means data not changed by call
	{					//when placed here is inline function
		return Len;
	}

	int MaxLength() const			//Gives access to max string length
	{
		return MAX_LENGTH;
	}

	String()				//constructor that initializes strings to null
	{				//called automatically when string is declared
		Len = 0;
	}

	String (const char P[]);
	
	String &operator=	(const char Source[]);

	int operator!=		(const String& Rhs);

	int operator==		(const String& Rhs);

	int operator<		(const String& Rhs);

	String operator+	(const char Rhs[]);

	String operator+	(const String& Rhs);

	friend ostream& operator<<	(ostream& Sout, const String &S);

	friend istream& operator>> (istream& In, String &S);
};


String::String (const char P[])
{
	int k = 0;

	while (k < MaxLength() && P[k] != 0)
	{
		Chr[k] = P[k];		//copy characters from P
		k++;
	}

	Len = k;
}

String& String::operator= (const char RightSide[])
{
	int k = 0;

	while (k < MaxLength() && RightSide[k] != 0)
	{
		Chr[k] = RightSide[k];
		k++;
	}

	Len = k;

	return *this;
}

int String::operator!= (const String& Rhs)
{
	if (Len != Rhs.Len)
		return 1;

	for (int k = 0; k < Len; ++k)
		if (Chr[k] != Rhs.Chr[k])
			return 1;

	return 0;
}

int String::operator== (const String& Rhs)
{
	if (Len != Rhs.Len)
		return 0;

	for (int k = 0; k < Len; ++k)
		if (Chr[k] != Rhs.Chr[k])
			return 0;

	return 1;
}

int String::operator< (const String& Rhs)
{
	int k = 0;

	while (k < Len && k < Rhs.Len)
		if (Chr[k] < Rhs.Chr[k])
			return 1;
		else if (Chr[k] > Rhs.Chr[k])
			return 0;
		else
			++k;

	return Len < Rhs.Len;
}

istream& operator>> (istream& In, String &S)
{
	char Ch;

	In >> Ch;

	if (In.eof())
	{
		S.Len = 0;
		S.Chr[0] = 0;
		return In;
	}

	int k = 0;

	while (!isspace(Ch) && k < S.MAX_LENGTH && In.good() &!In.eof())
	{
		S.Chr[k++] = Ch;
		In.get(Ch);
	}

	S.Len = k;

	return In;
}

ostream& operator<< (ostream& Sout, const String &S)
{
	char TempStr[S.MAX_LENGTH];

	int k;

	for (k = 0; k < S.Len; ++k)
		TempStr[k] = S.Chr[k];

	TempStr[k] = 0;

	Sout << TempStr;

	return Sout;
}


String String::operator+ (const String& Rhs)
{
	String Temp;

	int I = 0, k;

	Temp = *this;

	k = Temp.Len;

	while (I < Rhs.Len && k < Temp.MAX_LENGTH)
		Temp.Chr[k++] = Rhs.Chr[I++];

	Temp.Len = k;

	return Temp;
}

String String::operator+ (const char Rhs[])
{
	String Temp;
	int I = 0, k;

	Temp = *this;
	k = Temp.Len;

	while (Rhs[I] != 0 && k < Temp.MAX_LENGTH)
		Temp.Chr[k++] = Rhs[I++];

	Temp.Len = k;

	return Temp;
}

So, that's the code. My question: I'm doing my best to understand how to use classes, but I am really struggling with them. I know I don't need to modify the Read or the Write functions. I don't know where it is I'm supposed to modify the code because I don't really understand how to use the class. If someone can explain it to me, using this code and without completely doing my homework for me, I would be grateful. The code works as is if I comment out the #include "string2.cpp" file.

A class is a kind of object. You create one, and then use it. Like this, for example:

int x; // make an object of kind int, named x
char y; // make an object of kind char, named y
String z; // make an object of kind String, named z

int and char are done already for you; they came with the C++ language. What your class definition above does is carefully define everything there is to know about this new kind of object called a "String", and in my code here I've shown how you create one of those .

String eggs;

and now we've made another one, called eggs.

What can objects of this new class called String actually do? We look at your code, and see that the following operations have been defined:

String();
String (const char P[]);
String &operator= (const char Source[]);
int operator!= (const String& Rhs);
int operator== (const String& Rhs);
int operator< (const String& Rhs);
String operator+ (const char Rhs[]);
String operator+ (const String& Rhs);
friend ostream& operator<< (ostream& Sout, const String &S);
friend istream& operator>> (istream& In, String &S);

The first two are constructors; they define how we can create objects of this class. Looks like one (the very first) is the basic constructor I just used in my code. The second, String (const char P[]);, takes a const char array, so we can also create them like this:

String beans("SomeWords");

Here we have created an object of type String, named beans. To see what gets done with the "SomeWords", you'd have to check the code for the function String (const char P[]);.

Other things this new class understands are a whole lot of operators in that list above; =, !=, ==, <, +, << and >>, all of which are to be used with an object of type String and whatever other kind of object is defined in the code above.

As an aside, void main() has been incorrect C++ for a very long time. It should be int main()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.