Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

programming project 8 - chapter 11 Define a class called Text whose object store

ID: 3534419 • Letter: P

Question

programming project 8 - chapter 11


Define a class called Text whose object store lists of words.The class Text
will be just like the class StringVar except that the class Text will use a dy-
namic array with base type StringVar rather than base type char and will
mark the end of the array with a StringVar object consisting of a single
blank, rather than using '' as the end marker. Intuitively, an object of
the class Text represetns some text consisisting of words seperated by blanks.
Enforce the restriction that the array elements of type StringVar contain no blanks
(except for the end marker elements of type StringVar).

Your class Text will have member functions corresponding to all the member functions
of StringVar. The constructor with an argument of type const char a[ ] will initialize
the Text object in the same way as described below for input_line. If the C-string argument
contains the new-line symbol ' ', that is considered an error and ends the program
with an error message.

The member function input_line will read blank seperated strings and store each string
in one element of the dynamic array with base type StringVar. Multiple blank spaces are treated
the same as a single blank space. When outputting an object of the class Text, insert one blank
between each value of type StringVar. You may either assume that no tab symbols are used or you can treat
the tab symbols the same as a blank.


//DISPLAY 11.11 Program Using the StringVar Class
//This is the definition for the class StringVar
//whose values are strings. An object is declared as follows.
//Note that you use (max_size), not [max_size]
//     StringVar the_object(max_size);
//where max_size is the longest string length allowed.
#include <iostream>
using namespace std;

    class StringVar
    {
    public:
        StringVar(int size);
        //Initializes the object so it can accept string values up to size
        //in length. Sets the value of the object equal to the empty string.

        StringVar( );
        //Initializes the object so it can accept string values of length 100
        //or less. Sets the value of the object equal to the empty string.

        StringVar(const char a[]);
        //Precondition: The array a contains characters terminated with ''.
        //Initializes the object so its value is the string stored in a and
        //so that it can later be set to string values up to strlen(a) in length

        StringVar(const StringVar& string_object);
        //Copy constructor.

        ~StringVar( );
        //Returns all the dynamic memory used by the object to the freestore.

        int length( ) const;
        //Returns the length of the current string value.

         void input_line(istream& ins);
        //Precondition: If ins is a file input stream, then ins has been
        //connected to a file.
        //Action: The next text in the input stream ins, up to ' ', is copied
        //to the calling object. If there is not sufficient room, then
        //only as much as will fit is copied.
        friend ostream& operator <<(ostream& outs, const StringVar& the_string);
       
        //Overloads the << operator so it can be used to output values
        //of type StringVar
        //Precondition: If outs is a file output stream, then outs
        //has already been connected to a file.

         private:
             char *value; //pointer to dynamic array that holds the string value.
             int max_length; //declared max length of any string value.
         };


//Program to demonstrate use of the class StringVar.

void conversation(int max_name_size);
//Carries on a conversation with the user.


int main( )
{
    using namespace std;
    conversation(30);
    cout << "End of demonstration. ";
    return 0;
}

// This is only a demonstration function:
void conversation(int max_name_size)
{
    using namespace std;

    StringVar your_name(max_name_size), our_name("Borg");

    cout << "What is your name? ";
    your_name.input_line(cin);
    cout << "We are " << our_name << endl;
    cout << "We will meet again " << your_name << endl;
}


Explanation / Answer

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];  }