Text Class - HELP!

Create a class named Text whose objects store lists of words. The list of words will be represented using a dynamically allocated array of string variables where each word in the list is stored in a separate string variable array. The size of the array must always reflect the exact number of words stored in the Text object. Include the following:

Default constructor that will create a Text object with zero words.
Overloaded << operator that will output the Text object. Each word must be separated by a single space.
Overload + operator that will concatenate two Text objects.
Overload + operator that will concatenate two Text object with a string object.
Overload + operator that will concatenate two Text object with a c-string. Note: the argument type for a c-string is const char*.


Since you are using dynamic variables in this class, you must also include the required member functions to properly handle memory allocation issues.

Write a test program that includes the following lines of C++ code (at minimum):

Text text1, text2;

text1 = text1 + “The” + “quick” + “brown” + “fox”;
text2 = text2 + “jumped” + “over” + “the” + “lazy” + “do;
cout << text1 << endl;
cout << text2 << endl;
cout << text1 + text2 << endl;

This was the lab project that I was given. I wrote code and I have had it reviewed by my classmates and TA’s, but there are still errors and we can’t come to a conclusion as to what the error in the code is. I was wondering if anyone was able to spot what we haven’t been able uncover. The code below is the code that I have written.

This code is the first part I wrote including the classes. I have named it text.h.

#ifndef TEXT_H
#define TEXT_H
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstddef>

using namespace std;


class Text
{
public:

Text();
friend ostream& operator << (ostream& outs, Text& string1);
friend Text operator + (Text A, Text B);
friend Text operator + (Text A, string B);
friend Text& operator + (Text A, const char* B);
Text(Text& object);
~Text();
Text& operator =(Text& object);

private:
string *value;
int length;

};


#endif



This is the main part of the code which includes “text.h”. This was named text.cpp.


#include <iostream>
#include "text.h"


using namespace std;


Text::Text():length(0){

value=NULL;

}

Text::~Text(){
delete [] value;
}

Text::Text(Text& object){
value= new string[length];
length = object.length;
for(int i=0;i<length;i++){
value[i]=object.value[i];
}

}

Text& Text::operator = (Text& object){

if(this != &object) {
value= new string[object.length];
length = object.length;
for(int i=0;i<length;i++){
value[i]=object.value[i];
}
}

return *this;
}

ostream& operator << (ostream& outs, Text& string1){
for(int i = 0; i < string1.length; i++) {
outs<<string1.value[i] << " ";
}

return outs;
}

Text operator + (Text A, Text B){

Text temp;

temp.value=new string [A.length+B.length];

for(int i=0;i<A.length;i++){

temp.value[i]=A.value[i];
}
for(int i=A.length;i<B.length;i++){

temp.value[i]=A.value[i];
}

return temp;
}

Text operator + (Text A, string B){

Text temp;

temp.value=new string [A.length+1];

for(int i=0;i<A.length;i++){
temp.value[i]=A.value[i];
}

temp.value[A.length]=B;

return temp;

}

Text& operator + (Text A, const char* B){
//Textc operator + (Text A, const char* B){


//Text temp;
Text* temp = new Text();


(*temp).value=new string [A.length+1];
//temp.value=new string [A.length+1];

for(int i=0;i<A.length;i++){
(*temp).value[i]=A.value[i];
//temp.value[i]=A.value[i];
}
string temp1=B;

(*temp).value[A.length]=temp1;
//temp.value[A.length]=temp1;

return *temp;
//return temp;

}



And the last part of the code is the test program that should output text1 & text2 when it is compiled along with text.cpp.


#include <iostream>
#include "text.h"

using namespace std;

int main()
{

Text text1, text2;

text1= text1 + "the";
cout<<text1<<endl;

return 0;
}

So there’s the code. Please help with this issue. Thank you.
Last edited on
I found several problems.

The one causing problems may be due to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Text operator + (Text A, Text B){

Text temp;

temp.value=new string [A.length+B.length];

for(int i=0;i<A.length;i++){

temp.value[i]=A.value[i];
}
// for(int i=A.length;i<B.length;i++){ // bad code
for(int i=A.length;i<B.length+A.length;i++){// may work better

temp.value[i]=A.value[i];
}

or this...
1
2
3
4
5
6
7
8
9
// uninitialized value used in copy ctor
Text::Text(Text& object){  // const Text& is usual choice here
value= new string[length];// what does length= here?
length = object.length;// you probably meant for this line to be first
for(int i=0;i<length;i++){
value[i]=object.value[i];
}

}

If your TA couldn't find those then he should be taking your class.

Other things I see are just errors, which may not affect Text values.

A memory leak:
1
2
3
4
5
6
7
8
9
10
Text& Text::operator = (Text& object){
if(this != &object) {
value= new string[object.length];// release memory before overwriting this pointer value!
length = object.length;
for(int i=0;i<length;i++){
value[i]=object.value[i];
}
}
return *this;
}

I see you may have other issues too (that Text& operator + (Text A, const char* B) function looks like you are having trouble there ).
But I'll stop there.
Last edited on
Thank you, thank you! I'll take a crack at it bright and early tomorrow.
These didn't solve the issue. Now it is simply outputting nothing on the terminal screen. Any other suggestions??
Topic archived. No new replies allowed.