0

Hi, I've spent a lot of time on this program and I'm stuck.

My assignment is to prompt for two input strings.
Print out each string
Concatenate string1 and string2 and store the concatenated string into string3 without changing what's stored in string1 or string2.

print out the concatenated string.

I have to also, overload the - to remove the designated string from the concatenated string. and Print out the remaining strings.

Example of how program should run:

Enter string one: cat

Enter string two: dog

String one contains: cat
string two contains: dog
string one plus string two contains: catdog
Enter the string to remove from catdog: dog
the new string is: cat.


Here is the code I have so far.

HEADER FILE (string.h)

/*-----------------------------------------------------------------------------
*
*  HEADER name: string.h
*  Purpose:     header file for string.cpp
*  Usage:	Requires string.cpp (main file) & stringDef.cpp
*		    (Definition file).
*
*  Author: Sandy Davis
*  Date:   20100308
* 
------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------
*	Class: String
*	Purpose: Holds all information that deals with a string.
------------------------------------------------------------------------------*/

class String
{
public:
	friend istream &operator>>( istream &, String &);

	/*------------------------------------------------------------------------------
	*	Function: Constructor
	*	Purpose:  to create objects when called.
	------------------------------------------------------------------------------*/
	String( const char * = "" );
	
	/*------------------------------------------------------------------------------
	*	Function: Copy Constructor
	*	Purpose:  
	------------------------------------------------------------------------------*/
	String( const String & );

	/*------------------------------------------------------------------------------
	*	Function: Destructor
	*	Purpose:  Called to destroy objects which releases dynamically allocated
	*			  memory.
	------------------------------------------------------------------------------*/
	~String();

	/*------------------------------------------------------------------------------
	*	Operator Overloading: Concatenation operator + 
	*   DO NOT PASS BY REFERENCE.
	------------------------------------------------------------------------------*/
	const String operator+(const String &);

	/*------------------------------------------------------------------------------
	*	Operator Overloading: Concatenation operator =
	*   PASS BY REFERENCE.
	------------------------------------------------------------------------------*/
	const String &operator=( const String &);

	/*------------------------------------------------------------------------------
	*	Function: getString.
	*	Purpose: to get *sPtr and return it to main so that I can print statements
	*	for user.
	------------------------------------------------------------------------------*/
	char *getString() {return sPtr;};

private:

	/*------------------------------------------------------------------------------
	*	string length. (It does not include the null terminator.)
	*	represents the number of characters in the string.
	------------------------------------------------------------------------------*/
	int length;

	/*------------------------------------------------------------------------------
	*	Pointer to the start of the pointer-based string.
	*	points to dynamically allocated memory
	------------------------------------------------------------------------------*/
	char *sPtr;

	/*------------------------------------------------------------------------------
	*	Utility Function.
	------------------------------------------------------------------------------*/
	void setString( const char * );
};

DEFINITION FILE (stringDef.cpp)

/*-----------------------------------------------------------------------------
*
*  Program name: stringDef.cpp
*  Purpose:  Create a string class that will implement the operand + to
*			 make string1 = string2 + string3
*
*  Usage:	Requires string.h (header file) & string.cpp
*		    (main file).
*
*  Author: Sandy Davis
*  Date:   20100308
* 
------------------------------------------------------------------------------*/
#include <iostream>

using namespace std;

/*------------------------------------------------------------------------------
*	Header File.
------------------------------------------------------------------------------*/
#include  "string.h"

/*------------------------------------------------------------------------------
*	Function: Conversion / Default Constructor
*	Purpose: to convert char * to string. It calls the utility function to
*			 allocate memory.
------------------------------------------------------------------------------*/
String::String( const char *s)
	: length( ( s != 0) ? strlen(s) : 0)
{
	cout << "Conversion (and default) constructor: " << s << endl;

	/*------------------------------------------------------------------------------
	*	Calls the utility function.
	------------------------------------------------------------------------------*/
	setString(s);
}

/*------------------------------------------------------------------------------
*	Function: Copy Constructor
*	Purpose: It copies the length member from the source String object to the
*			 target String object. 
*	Note: Copy Constructor calls setString function so that it can allocate
*		  memory for the target object's internal character string. If I did
*		  not do this, then both objects would point to the same dynamically
*	      allocated memory. The first destructor to execute would then delete
*		  the dynamically allocated memory, and the other object's sPtr would
*		  be undefined (meaning sPtr would be a DANGLING POINTER). 
------------------------------------------------------------------------------*/
String::String(const String &copy)
	: length( copy.length )
{
	cout << "Copy Constructor: " << copy.sPtr << endl;
	setString( copy.sPtr ); 
}

/*------------------------------------------------------------------------------
*	Function: Destructor
*	Purpose: Releases pointer-based string memory.
------------------------------------------------------------------------------*/
String::~String()
{
	cout << "Destructor: " << sPtr << endl;

	delete[] sPtr; 
}

/*------------------------------------------------------------------------------
*	Function: setString
*	Purpose: called by constructors and operators. It takes a const char *
*			 argument (defaults to empty string) and initializes a String
*			 object containing that same character string.
------------------------------------------------------------------------------*/
void String::setString( const char *string2 )
{
	/*------------------------------------------------------------------------------
	* Allocate Memory.	
	------------------------------------------------------------------------------*/
	sPtr = new char[ length + 1 ];

	/*------------------------------------------------------------------------------
	* If string2 is not a null pointer, then I copy the contents of string2 
	* into sPtr. else if string2 is a null pointer, then I make sPtr an empty
	* string.
	------------------------------------------------------------------------------*/

	if ( string2 != 0 ) 
		strcpy( sPtr, string2);
	else
		sPtr[ 0 ] = '\0';
}

/*------------------------------------------------------------------------------
*	Overloader Istream Function: operator >>
*	Purpose: to allow the user to input string1 and string2.
*   Return: input
------------------------------------------------------------------------------*/
istream &operator>>(istream &input, String &string)
{
	input >> string.sPtr;
	return input;
}

/*------------------------------------------------------------------------------
*	Overloader Function: operator +
*	Purpose: to concatenate string1 and string2 and store it in a temporary
*			 variable
*	Return: temporary variable (tmpStr)
------------------------------------------------------------------------------*/
const String String::operator+(const String &right)
{
	cout << " + operator called " << endl;

	/*------------------------------------------------------------------------------
	* Create a tmpStr of type String to hold the concatenated string.
	------------------------------------------------------------------------------*/
	String tmpStr;

	/*------------------------------------------------------------------------------
	* tmpStr.sPtr will equal the lhs length + rhs length + 1 (b/c of null byte)
	------------------------------------------------------------------------------*/
	tmpStr.sPtr = new char[length + right.length + 1];

	cout << "HERE HERE 1 sPTR" << sPtr << endl;
	/*------------------------------------------------------------------------------
	* Copy sPtr into tmpStr.sPtr.
	------------------------------------------------------------------------------*/
	strcpy(tmpStr.sPtr, sPtr);

	cout << "HERE HERE 2 sPTR " << sPtr << endl;

	cout << "HERE HERE 3 right.Sptr " << right.sPtr << endl;
	/*------------------------------------------------------------------------------
	* Copy right.sPtr after the tmpStr.sPtr + the length that's already there... so 
	* that I don't overwrite what I just put in tmpPtr.sPtr. 
	------------------------------------------------------------------------------*/
	strcpy(tmpStr.sPtr + length, right.sPtr);

	cout << "HERE HERE 4 right.sPTr " << right.sPtr << endl;
	/*------------------------------------------------------------------------------
	* Return tmpStr. You should get string1 and string2 together. 
	------------------------------------------------------------------------------*/
	return tmpStr;
}


/*------------------------------------------------------------------------------
*	Overloader Function: operator =
*	Purpose: to assign the s1 + s2 which should be in tmpStr by the time
*	it gets here to string3. 
*	Return: this pointer, which will be string3 that has string1 + string2.
------------------------------------------------------------------------------*/
const String &String::operator =(const String &right)
{
	cout << "operator = called " << endl;
	/*------------------------------------------------------------------------------
	* Avoid self-assignment by checking to see if the rhs is not equal to the 
	* lhs - if it's not equal I delete sPtr to avoid a memory leak and set the
	* new string length  and I call the utility function so that it can
	* create the new object & allocate memory for it.
	* else if it's equal, I print an error statement.
	------------------------------------------------------------------------------*/
	if( &right != this )
	{
		delete [] sPtr;
		length = right.length;
		setString( right.sPtr);
	}
	else
	{
		cout << "Attempted assignment of a String to itself" << endl;
	}

	return *this;
}

MAIN FILE (string.cpp)

/*-----------------------------------------------------------------------------
*
*  Program name: string.cpp
*  Purpose:  Create a string class that will implement the operand + to
*			 make string1 = string2 + string3
*
*  Usage:	Requires string.h (header file) & stringDef.cpp
*		    (Definition file).
*
*  Author: Sandy Davis
*  Date:   20100308
* 
------------------------------------------------------------------------------*/
#include <iostream>

using namespace std;

/*------------------------------------------------------------------------------
*	Header File.
------------------------------------------------------------------------------*/
#include  "string.h"

/*------------------------------------------------------------------------------
*	Function: Main
*	Purpose: Driver Program to implement the string class.
*	Returns: 0 for success
------------------------------------------------------------------------------*/
int main()
{
	/*------------------------------------------------------------------------------
	*	Creating 3 string objects of the class String.
	*	s3 uses operator overloading + and = function. 
	------------------------------------------------------------------------------*/
	String s1;
	String s2;

	cout << "Enter string one: " << endl;
	cin >> s1;

	cout << "Enter string two: " << endl;
	cin >> s2;

	String s3 = s1 + s2;

	/*------------------------------------------------------------------------------
	*	Printing s1, s2, and s3 to make sure it worked. 
	------------------------------------------------------------------------------*/
	cout << "testing s1 " << s1.getString() << endl;
	cout << "testing s2 " << s2.getString() << endl;
	cout << "testing s3 " << s3.getString() << endl;
	return 0;
}

Problems I am having:

For some reason when I go to concatenate the two strings
cat and dog --- it copies cat into the temporary variable but then when it gets to dog... somehow cat disappears... and both strings aren't stored into the one temporary variable.

Also, my c++ program (visual studios) keeps complaining about a heap corruption detected. I'm not really sure how to fix something like that. So, if anyone has any suggestions on what I'm doing wrong in my program, I would definitely appreciate it.

Thank you so very much!

-Sandy

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by mattjbond
-1

Hi, I've spent a lot of time on this program and I'm stuck.

My assignment is to prompt for two input strings.
Print out each string
Concatenate string1 and string2 and store the concatenated string into string3 without changing what's stored in string1 or string2.

print out the concatenated string.

I have to also, overload the - to remove the designated string from the concatenated string. and Print out the remaining strings.

Example of how program should run:

Enter string one: cat

Enter string two: dog

String one contains: cat
string two contains: dog
string one plus string two contains: catdog
Enter the string to remove from catdog: dog
the new string is: cat.


Here is the code I have so far.

HEADER FILE (string.h)

/*-----------------------------------------------------------------------------
*
*  HEADER name: string.h
*  Purpose:     header file for string.cpp
*  Usage:	Requires string.cpp (main file) & stringDef.cpp
*		    (Definition file).
*
*  Author: Sandy Davis
*  Date:   20100308
* 
------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------
*	Class: String
*	Purpose: Holds all information that deals with a string.
------------------------------------------------------------------------------*/

class String
{
public:
	friend istream &operator>>( istream &, String &);

	/*------------------------------------------------------------------------------
	*	Function: Constructor
	*	Purpose:  to create objects when called.
	------------------------------------------------------------------------------*/
	String( const char * = "" );
	
	/*------------------------------------------------------------------------------
	*	Function: Copy Constructor
	*	Purpose:  
	------------------------------------------------------------------------------*/
	String( const String & );

	/*------------------------------------------------------------------------------
	*	Function: Destructor
	*	Purpose:  Called to destroy objects which releases dynamically allocated
	*			  memory.
	------------------------------------------------------------------------------*/
	~String();

	/*------------------------------------------------------------------------------
	*	Operator Overloading: Concatenation operator + 
	*   DO NOT PASS BY REFERENCE.
	------------------------------------------------------------------------------*/
	const String operator+(const String &);

	/*------------------------------------------------------------------------------
	*	Operator Overloading: Concatenation operator =
	*   PASS BY REFERENCE.
	------------------------------------------------------------------------------*/
	const String &operator=( const String &);

	/*------------------------------------------------------------------------------
	*	Function: getString.
	*	Purpose: to get *sPtr and return it to main so that I can print statements
	*	for user.
	------------------------------------------------------------------------------*/
	char *getString() {return sPtr;};

private:

	/*------------------------------------------------------------------------------
	*	string length. (It does not include the null terminator.)
	*	represents the number of characters in the string.
	------------------------------------------------------------------------------*/
	int length;

	/*------------------------------------------------------------------------------
	*	Pointer to the start of the pointer-based string.
	*	points to dynamically allocated memory
	------------------------------------------------------------------------------*/
	char *sPtr;

	/*------------------------------------------------------------------------------
	*	Utility Function.
	------------------------------------------------------------------------------*/
	void setString( const char * );
};

DEFINITION FILE (stringDef.cpp)

/*-----------------------------------------------------------------------------
*
*  Program name: stringDef.cpp
*  Purpose:  Create a string class that will implement the operand + to
*			 make string1 = string2 + string3
*
*  Usage:	Requires string.h (header file) & string.cpp
*		    (main file).
*
*  Author: Sandy Davis
*  Date:   20100308
* 
------------------------------------------------------------------------------*/
#include <iostream>

using namespace std;

/*------------------------------------------------------------------------------
*	Header File.
------------------------------------------------------------------------------*/
#include  "string.h"

/*------------------------------------------------------------------------------
*	Function: Conversion / Default Constructor
*	Purpose: to convert char * to string. It calls the utility function to
*			 allocate memory.
------------------------------------------------------------------------------*/
String::String( const char *s)
	: length( ( s != 0) ? strlen(s) : 0)
{
	cout << "Conversion (and default) constructor: " << s << endl;

	/*------------------------------------------------------------------------------
	*	Calls the utility function.
	------------------------------------------------------------------------------*/
	setString(s);
}

/*------------------------------------------------------------------------------
*	Function: Copy Constructor
*	Purpose: It copies the length member from the source String object to the
*			 target String object. 
*	Note: Copy Constructor calls setString function so that it can allocate
*		  memory for the target object's internal character string. If I did
*		  not do this, then both objects would point to the same dynamically
*	      allocated memory. The first destructor to execute would then delete
*		  the dynamically allocated memory, and the other object's sPtr would
*		  be undefined (meaning sPtr would be a DANGLING POINTER). 
------------------------------------------------------------------------------*/
String::String(const String &copy)
	: length( copy.length )
{
	cout << "Copy Constructor: " << copy.sPtr << endl;
	setString( copy.sPtr ); 
}

/*------------------------------------------------------------------------------
*	Function: Destructor
*	Purpose: Releases pointer-based string memory.
------------------------------------------------------------------------------*/
String::~String()
{
	cout << "Destructor: " << sPtr << endl;

	delete[] sPtr; 
}

/*------------------------------------------------------------------------------
*	Function: setString
*	Purpose: called by constructors and operators. It takes a const char *
*			 argument (defaults to empty string) and initializes a String
*			 object containing that same character string.
------------------------------------------------------------------------------*/
void String::setString( const char *string2 )
{
	/*------------------------------------------------------------------------------
	* Allocate Memory.	
	------------------------------------------------------------------------------*/
	sPtr = new char[ length + 1 ];

	/*------------------------------------------------------------------------------
	* If string2 is not a null pointer, then I copy the contents of string2 
	* into sPtr. else if string2 is a null pointer, then I make sPtr an empty
	* string.
	------------------------------------------------------------------------------*/

	if ( string2 != 0 ) 
		strcpy( sPtr, string2);
	else
		sPtr[ 0 ] = '\0';
}

/*------------------------------------------------------------------------------
*	Overloader Istream Function: operator >>
*	Purpose: to allow the user to input string1 and string2.
*   Return: input
------------------------------------------------------------------------------*/
istream &operator>>(istream &input, String &string)
{
	input >> string.sPtr;
	return input;
}

/*------------------------------------------------------------------------------
*	Overloader Function: operator +
*	Purpose: to concatenate string1 and string2 and store it in a temporary
*			 variable
*	Return: temporary variable (tmpStr)
------------------------------------------------------------------------------*/
const String String::operator+(const String &right)
{
	cout << " + operator called " << endl;

	/*------------------------------------------------------------------------------
	* Create a tmpStr of type String to hold the concatenated string.
	------------------------------------------------------------------------------*/
	String tmpStr;

	/*------------------------------------------------------------------------------
	* tmpStr.sPtr will equal the lhs length + rhs length + 1 (b/c of null byte)
	------------------------------------------------------------------------------*/
	tmpStr.sPtr = new char[length + right.length + 1];

	cout << "HERE HERE 1 sPTR" << sPtr << endl;
	/*------------------------------------------------------------------------------
	* Copy sPtr into tmpStr.sPtr.
	------------------------------------------------------------------------------*/
	strcpy(tmpStr.sPtr, sPtr);

	cout << "HERE HERE 2 sPTR " << sPtr << endl;

	cout << "HERE HERE 3 right.Sptr " << right.sPtr << endl;
	/*------------------------------------------------------------------------------
	* Copy right.sPtr after the tmpStr.sPtr + the length that's already there... so 
	* that I don't overwrite what I just put in tmpPtr.sPtr. 
	------------------------------------------------------------------------------*/
	strcpy(tmpStr.sPtr + length, right.sPtr);

	cout << "HERE HERE 4 right.sPTr " << right.sPtr << endl;
	/*------------------------------------------------------------------------------
	* Return tmpStr. You should get string1 and string2 together. 
	------------------------------------------------------------------------------*/
	return tmpStr;
}


/*------------------------------------------------------------------------------
*	Overloader Function: operator =
*	Purpose: to assign the s1 + s2 which should be in tmpStr by the time
*	it gets here to string3. 
*	Return: this pointer, which will be string3 that has string1 + string2.
------------------------------------------------------------------------------*/
const String &String::operator =(const String &right)
{
	cout << "operator = called " << endl;
	/*------------------------------------------------------------------------------
	* Avoid self-assignment by checking to see if the rhs is not equal to the 
	* lhs - if it's not equal I delete sPtr to avoid a memory leak and set the
	* new string length  and I call the utility function so that it can
	* create the new object & allocate memory for it.
	* else if it's equal, I print an error statement.
	------------------------------------------------------------------------------*/
	if( &right != this )
	{
		delete [] sPtr;
		length = right.length;
		setString( right.sPtr);
	}
	else
	{
		cout << "Attempted assignment of a String to itself" << endl;
	}

	return *this;
}

MAIN FILE (string.cpp)

/*-----------------------------------------------------------------------------
*
*  Program name: string.cpp
*  Purpose:  Create a string class that will implement the operand + to
*			 make string1 = string2 + string3
*
*  Usage:	Requires string.h (header file) & stringDef.cpp
*		    (Definition file).
*
*  Author: Sandy Davis
*  Date:   20100308
* 
------------------------------------------------------------------------------*/
#include <iostream>

using namespace std;

/*------------------------------------------------------------------------------
*	Header File.
------------------------------------------------------------------------------*/
#include  "string.h"

/*------------------------------------------------------------------------------
*	Function: Main
*	Purpose: Driver Program to implement the string class.
*	Returns: 0 for success
------------------------------------------------------------------------------*/
int main()
{
	/*------------------------------------------------------------------------------
	*	Creating 3 string objects of the class String.
	*	s3 uses operator overloading + and = function. 
	------------------------------------------------------------------------------*/
	String s1;
	String s2;

	cout << "Enter string one: " << endl;
	cin >> s1;

	cout << "Enter string two: " << endl;
	cin >> s2;

	String s3 = s1 + s2;

	/*------------------------------------------------------------------------------
	*	Printing s1, s2, and s3 to make sure it worked. 
	------------------------------------------------------------------------------*/
	cout << "testing s1 " << s1.getString() << endl;
	cout << "testing s2 " << s2.getString() << endl;
	cout << "testing s3 " << s3.getString() << endl;
	return 0;
}

Problems I am having:

For some reason when I go to concatenate the two strings
cat and dog --- it copies cat into the temporary variable but then when it gets to dog... somehow cat disappears... and both strings aren't stored into the one temporary variable.

Also, my c++ program (visual studios) keeps complaining about a heap corruption detected. I'm not really sure how to fix something like that. So, if anyone has any suggestions on what I'm doing wrong in my program, I would definitely appreciate it.

Thank you so very much!

-Sandy

strcpy copies the entrie string including the terminating NULL. You are probably copying the second part of your string after this first terminating NULL. When you try to access this later all you get is the string up to the first NULL terminator value. You shoudl probably look at your copy constructor for issues too.

Votes + Comments
It is possible to reply and edit out unessesary text from the quote -- or with no quote at all. Please consider 300 lines of quote with 5 lines of reply would be considered a bad post.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.