I really don't know where to start and to be honest I really don't know what I'm doing. I was given this project and we never went over these topics in class or the previous class. My attempts to wing it are not going so well and quite frankly its embarrassing. The program should be divided in its respective header and implementation files, but I am just trying to get the code to work. I can divide the code up later. I still need to write one more class which is the entire collection of DVD's. The c-string class is a re-definition of c-strings that I have to implement. The DVD class is attributes of a DVD in the collection. If anyone could please at the very least guide me in the right direction I would really appreciate it.
#include <iostream>
using std::istream;
using std::ostream;
clas string
{
private:
char _string[];
//recursive functions
int str_length(const char str[]);
int str_compare(const char *str1, const char *str2);
void str_reverse(char str[] , int length);
bool str_replace_first(char str[], const char find[], const char replace[]);
//iterative functions
int length_iter(const char str[]);
int str_compare_iter(char str1[], const char str2[]);
void str_reverse_iter(char str[], int length);
bool str_replace_first_iter(char str[], const char find[], const char replace[]);
public:
//Constructors
string();
string(char *array, int size);
//Deconstructor
~string();
int str_length(){
return str_length(_string);}
int str_compare(){
return str_compare(_string, _string);}
void str_reverse(){
return str_reverse(_string, str_length(_string));}
bool str_replace_first(){
return str_replace_first(_string, _string, _string);}
int length_iter(){
return str_length(_string);}
int str_compare_iter(){
return str_compare(_string, _string);}
void str_reverse_iter(){
return str_reverse(_string, str_length(_string));}
bool str_replace_first_iter(){
return str_replace_first(_string, _string, _string);}
//Overloaded Operators
void operator+(string &rstring);
friend std::istream &operator >>(std::istream &is, string &string);
friend std::ostream &operator <<(std::ostream &os, string &string);
string::string()
{
_string = new char[100];
_string[0] = '\0';
}
string::string(char *array, int size)
{
_string = new char[size];
for(int i=0; i < size; i++)
{
_string = *array;
}
}
//Deconstructor
string::~string()
{
delete [] _string;
}
/**
* This function finds the length of a c-string using recursion
* @param str[ ] a c-string variable that stores a string from user
* @pre in main command = "length", str[0] is not an empty string-'\0'
* @post if str[0]='\0' then 0 is returned or str_length is called again but at the next index of str[]
* @return the length of the c-string str[]
*/
int string::str_length(const char str[])
{
//base case
if (str[0] == '\0')
return 0;
//recursive statement
else
return 1 + str_length(str+1);
}
/**
* This function finds the length of a c-string using iteration
* @param str[ ] a c-string variable that stores a string from user
* @pre in main command = "length_iter", str[ ] is not an empty string-'\0'
* @post i is < 0
* @return the length of the c-string str[]
*/
int string::length_iter(const char str[])
{
int i=0;
while(str[i] != '\0')
{
i++;
}
return i;
}
/**
* This recursive function compares 2 c-strings to see if they are equal or there is a character mismatch
* @param *str1 is a pointer to charArray[ ]
* @param *str2 is a pointer to scharArray2[ ]
* @pre in main command = "compare", str1[ ] and str2[ ] is not an empty string-'\0'
* @post str1[] and str2[ ] are equal or *str1 and *str2 point to different characters
* @return a integer vaule that is the distance between the ASCII character vaules. If *str1 points to
a smaller character, the vaule is negative. If *str2 points to the a smaller character the value is positive.
If both *str1 and *str2 do not point to a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned
*/
int string::str_compare(const char *str1, const char *str2)
{
if (*str1 != '\0' || *str2 != '\0')
{
if (*str1 != *str2)
{
if (*str1 > *str2)
{
return *str1 - *str2;
}
else if (*str1 < *str2)
{
return *str1 - *str2;
}
}
}
else return 0; //C-strings are equal-base case
//recursive statement
return str_compare(str1 + 1, str2 + 1);
}
/**
* This iterative function compares 2 c-strings to see if they are equal or there is a character mismatch
* @param str1[ ] is a c-string variable that stores a string from user
* @param str2[ ] is a c-string variable that stores a string from user
* @pre in main command = "compare_iter", str1[ ] and str2[ ] are not an empty string-'\0'
* @post str1[] and str2[ ] are equal or str1[i] and str2[i] contain different characters
* @return a integer vaule(diff) that is the distance between the ASCII character vaules. If str1[i] contains
a smaller character, the vaule(diff) is negative. If str2[i] contains the a smaller character the value(diff) is positive.
If both str1[i] and str2[i] do not contain a mismatch, then str1[ ] and str2[ ] are equal and 0 is returned (diff)
*/
int string::str_compare_iter(char str1[], const char str2[])
{
for( ; *str1=*str2; *str1++, str2++)
{
if(*str1 == 0)
return 0;
}
return int(*str1-*str2);
}
/**
* This recursive function will reverse the c-string str
* @param *str is a pointer to charArray[ ]
* @param length is the length of charArray[ ]
* @pre in main command = "reverse", *str does notpoint to an empty string-'\0'
* @post the string in charArray[ ] is reversed
* @return no return value
*/
void string::str_reverse(char *str, int length)
{
char temp, temp1;
if (length >= 1)
{
// swap first & last chars
temp = str[0];
str[0] = str[length-1];
str[length-1] = temp;
// recursive call--
str_reverse(&str[1], length-2);
}
}
/**
* * This recursive function will reverse the c-string str
* @param str[ ] str1[ ] is a c-string variable that stores a string from charArray[ ]
* @param length is the length of charArray[ ]
* @pre in main command = "reverse_iter", str[ ] does notpoint to an empty string-'\0'
* @post the string in charArray[ ] is reversed
* @return no return value
*/
void string::str_reverse_iter(char str[], int length)
{
char temp;
int starti, endi, mid;//starting and ending index
starti = 0;
endi = length - 1;
mid = length / 2;
while(starti < mid)
{
temp = str[starti];
str[starti] = str[endi-starti];
str[endi-starti] = temp;
starti++;
}
}
/**
* This recursive function will search a c-string for a character(s) to be replaced with other character(s)
* @param *str points to charArray[ ] the array to be searched
* @param *find points to charArray2[ ], contains what to search for
* @param*replace points to charArray3[ ] what to replace the characters with
* @param is_partial_match is true if part of the find string is found in the original string
* @pre in main command = "replace_first", *str[ ] , *find, *replace does nont point to an empty string-'\0'
* @post Either the string was found and replaced in the array or *find and *replace were not equal in size thus the array was not searched
* @return false condition if *find and *replace were not equal in size thus the array was not searched or
true condition if *find and *replace were equal in size thus the array was searched and replaced with the contents *replace points to
*/
bool string::str_replace_first(char *str, const char *find, const char *replace)
{
if(length_iter(find) != length_iter(replace))
{
cout << "ERROR: The find and replace strings are of different length." << endl;
cout << "Please change the find and replace strings to the same length." << endl;
return false;
}
while(*str != '\0')
{
if(*str == *find)
{
*str = *replace;
*replace++;
*find++;
}
else str_replace_first(str+1, find, replace);
}
return true;
}
/**
* This iteraive function will search a c-string for a character(s) to be replaced with other character(s)
* @param str points to charArray[ ] the array to be searched
* @param find points to charArray2[ ], contains what to search for
* @param replace points to charArray3[ ] what to replace the characters with
* @param is_partial_match is true if part of the find string is found in the original string
* @pre in main command = "replace_first_iter", str , find, replace does nont point to an empty string-'\0'
* @post Either the string was found and replaced in the array or find and replace were not equal in size thus the array was not searched
* @return false condition if find and replace were not equal in size thus the array was not searched or
true condition if find and replace were equal in size thus the array was searched and replaced with the contents replace points to
*/
bool string::str_replace_first_iter(char str[], const char find[], const char replace[])
{
if(length_iter(find) != length_iter(replace))
{
cout << "ERROR: The find and replace strings are of different length." << endl;
cout << "Please change the find and replace strings to the same length." << endl;
return false;
}
int i, j, k, m, n, delta;
int n1, n2, n3;
char* p, *q;
if (!str || !find)
return 0;
if (!replace)
replace = "";
n1 = str_length(str);
n2 = str_length(find);
n = n1 - n2 + 1;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n2 && find[j] == str[i+j]; ++j)
;
if (j == n2) // found
{
n3 = str_length(replace);
delta = n3 - n2;
m = n1 - i - n2;
if (delta < 0) /* move left */
{
p = str + (i + n2 + delta);
q = p - delta;
for (k = 0; k <= m; ++k)
p[k] = q[k];
}
else if (delta > 0) /* move right */
{
q = str + n1 - m;
p = q + delta;
for (k = m; k >= 0; --k)
p[k] = q[k];
}
for (k = 0; k < n3; ++k)
str[i+k] = replace[k];
return str + i + n3;
}
}
return 0;
}
int string::str_length(){
return str_length(_string);}
int string::str_compare(){
return str_compare(_string, _string);}
void string::str_reverse(){
return str_reverse(_string, str_length(_string));}
bool string::str_replace_first(){
return str_replace_first(_string, _string, _string);}
int string::length_iter(){
return str_length(_string);}
int string::str_compare_iter(){
return str_compare(_string, _string);}
void string::str_reverse_iter(){
return str_reverse(_string, str_length(_string));}
bool string::str_replace_first_iter(){
return str_replace_first(_string, _string, _string);}
void string::operator+(string &rstring)
{
//string temp;
int len = str_length(_string) + str_length(rstring);
string temp(_string, len);
int i;
for (i = 0; i < str_length(_string); i++)
temp[i] = _string[i];
for (int j = 0; j < str_length(rstring); j++, i++)
temp[i] = rstring[j];
//rstring[len]='\0';
}
istream &operator >>(std::istream &is, string &string)
{
for(int i = 0; i < str_length(string); i++)
is >> string[i];
}
ostream &operator <<(std::ostream &os, string &string)
{
for(int i = 0; i < str_length(string); i++)
os << string[i];
return os;
}
};
namespace string
{
clas DVD : public string
{
private:
string actor, title, descrip;
int year;
public:
DVD();
DVD(string actor, string title, string descrip, int year);
friend std::ostream& operator <<(std::ostream &os, DVD &DVD);
DVD()
{
}
DVD(string actor, string title, string descrip, int year):string(actor, str_length(actor)),
string(title, str_length(title)), string(descrip, str_length(descrip)), year =0)
{
}
friend std::istream& operator >>(std::istream &is, DVD &DVD)
{
is.getline(actor, 100);
is.getline(album, 100);
is >> year;
is.getline(descrip, 100);
do
{
is.getline(descrip, 100);
} while(descrip != 'END');
return is;
}
}
};
int main(){
DVD one;
one.actor >> "The Cure";
one.title >> "Boys Don't Cry";
one.year >> 1985;
one.descrip >> "The best.";
return 0;
}
Compile errors:
test.cpp:51: error: `SString::SString()' and `SString::SString()' cannot be overloaded
test.cpp:57: error: `SString::SString(char*, int)' and `SString::SString(char*, int)' cannot be overloaded
test.cpp:66: error: `SString::~SString()' and `SString::~SString()' cannot be overloaded
test.cpp:77: error: `int SString::str_length(const char*)' and `int SString::str_length(const char*)' cannot be overloaded
test.cpp:94: error: `int SString::length_iter(const char*)' and `int SString::length_iter(const char*)' cannot be overloaded
test.cpp:114: error: `int SString::str_compare(const char*, const char*)' and `int SString::str_compare(const char*, const char*)' cannot be overloaded
test.cpp:146: error: `int SString::str_compare_iter(char*, const char*)' and `int SString::str_compare_iter(char*, const char*)' cannot be overloaded
test.cpp:165: error: `void SString::str_reverse(char*, int)' and `void SString::str_reverse(char*, int)' cannot be overloaded
test.cpp:189: error: `void SString::str_reverse_iter(char*, int)' and `void SString::str_reverse_iter(char*, int)' cannot be overloaded
test.cpp:217: error: `bool SString::str_replace_first(char*, const char*, const char*)' and `bool SString::str_replace_first(char*, const char*, const char*)' cannot be overloaded
test.cpp:252: error: `bool SString::str_replace_first_iter(char*, const char*, const char*)' and `bool SString::str_replace_first_iter(char*, const char*, const char*)' cannot be overloaded
test.cpp:300: error: `int SString::str_length()' and `int SString::str_length()' cannot be overloaded
test.cpp:303: error: `int SString::str_compare()' and `int SString::str_compare()' cannot be overloaded
test.cpp:306: error: `void SString::str_reverse()' and `void SString::str_reverse()' cannot be overloaded
test.cpp:309: error: `bool SString::str_replace_first()' and `bool SString::str_replace_first()' cannot be overloaded
test.cpp:312: error: `int SString::length_iter()' and `int SString::length_iter()' cannot be overloaded
test.cpp:315: error: `int SString::str_compare_iter()' and `int SString::str_compare_iter()' cannot be overloaded
test.cpp:318: error: `void SString::str_reverse_iter()' and `void SString::str_reverse_iter()' cannot be overloaded
test.cpp:321: error: `bool SString::str_replace_first_iter()' and `bool SString::str_replace_first_iter()' cannot be overloaded
test.cpp:325: error: `void SString::operator+(SString&)' and `void SString::operator+(SString&)' cannot be overloaded
test.cpp:338: error: `std::istream& SString::operator>>(std::istream&, SString&)' must take exactly one argument
test.cpp:344: error: `std::ostream& SString::operator<<(std::ostream&, SString&)' must take exactly one argument
test.cpp: In constructor `SString::SString()':
test.cpp:52: error: incompatible types in assignment of `char*' to `char[0u]'
test.cpp: In constructor `SString::SString(char*, int)':
test.cpp:58: error: incompatible types in assignment of `char*' to `char[0u]'
test.cpp:61: error: incompatible types in assignment of `char' to `char[0u]'
test.cpp: In member function `bool SString::str_replace_first(char*, const char*, const char*)':
test.cpp:220: error: `cout' undeclared (first use this function)
test.cpp:220: error: (Each undeclared identifier is reported only once for each function it appears in.)
test.cpp:220: error: `endl' undeclared (first use this function)
test.cpp: In member function `bool SString::str_replace_first_iter(char*, const char*, const char*)':
test.cpp:255: error: `cout' undeclared (first use this function)
test.cpp:255: error: `endl' undeclared (first use this function)
test.cpp: In member function `void SString::operator+(SString&)':
test.cpp:327: error: no matching function for call to `SString::str_length(SString&)'
test.cpp:10: note: candidates are: int SString::str_length(const char*)
test.cpp:28: note: int SString::str_length()
test.cpp:331: error: no match for 'operator[]' in 'temp[i]'
test.cpp:332: error: no matching function for call to `SString::str_length(SString&)'
test.cpp:10: note: candidates are: int SString::str_length(const char*)
test.cpp:28: note: int SString::str_length()
test.cpp:333: error: no match for 'operator[]' in 'temp[i]'
test.cpp:333: error: no match for 'operator[]' in 'rstring[j]'
test.cpp: In member function `std::istream& SString::operator>>(std::istream&, SString&)':
test.cpp:339: error: no matching function for call to `SString::str_length(SString&)'
test.cpp:10: note: candidates are: int SString::str_length(const char*)
test.cpp:28: note: int SString::str_length()
test.cpp:340: error: no match for 'operator[]' in 'string[i]'
test.cpp: In member function `std::ostream& SString::operator<<(std::ostream&, SString&)':
test.cpp:345: error: no matching function for call to `SString::str_length(SString&)'
test.cpp:10: note: candidates are: int SString::str_length(const char*)
test.cpp:28: note: int SString::str_length()
test.cpp:346: error: no match for 'operator[]' in 'string[i]'
test.cpp: At global scope:
test.cpp:365: error: `SString::CD::CD()' and `SString::CD::CD()' cannot be overloaded
test.cpp:369: error: `SString::CD::CD(SString, SString, SString, int)' and `SString::CD::CD(SString, SString, SString, int)' cannot be overloaded
test.cpp:384:22: warning: multi-character character constant
test.cpp: In constructor `SString::CD::CD(SString, SString, SString, int)':
test.cpp:369: error: no matching function for call to `SString::CD::str_length(SString&)'
test.cpp:10: note: candidates are: int SString::str_length(const char*)
test.cpp:28: note: int SString::str_length()
test.cpp:370: error: no matching function for call to `SString::CD::str_length(SString&)'
test.cpp:10: note: candidates are: int SString::str_length(const char*)
test.cpp:28: note: int SString::str_length()
test.cpp:370: error: no matching function for call to `SString::CD::str_length(SString&)'
test.cpp:10: note: candidates are: int SString::str_length(const char*)
test.cpp:28: note: int SString::str_length()
test.cpp:370: error: expected `(' before '=' token
test.cpp:370: error: multiple initializations given for base `SString'
test.cpp:370: error: multiple initializations given for base `SString'
test.cpp:370: error: expected `{' before '=' token
test.cpp: In function `std::istream& SString::operator>>(std::istream&, SString::CD&)':
test.cpp:356: error: invalid use of non-static data member `SString::CD::artist'
test.cpp:377: error: from this location
test.cpp:378: error: `album' undeclared (first use this function)
test.cpp:357: error: invalid use of non-static data member `SString::CD::year'
test.cpp:379: error: from this location
test.cpp:356: error: invalid use of non-static data member `SString::CD::descrip'
test.cpp:380: error: from this location
test.cpp:356: error: invalid use of non-static data member `SString::CD::descrip'
test.cpp:383: error: from this location
test.cpp:356: error: invalid use of non-static data member `SString::CD::descrip'
test.cpp:384: error: from this location
test.cpp: At global scope:
test.cpp:388: error: expected unqualified-id before '}' token
test.cpp:388: error: expected `,' or `;' before '}' token
test.cpp: In function `int main()':
test.cpp:391: error: `CD' undeclared (first use this function)
test.cpp:391: error: expected `;' before "one"
test.cpp:392: error: `one' undeclared (first use this function)
Execution terminated