R
roxorsoxor2345
I have this custom string class that I have created.
#include <cstdlib>
#include <iostream>
using namespace std;
class Cstring
{
private:
//array that will hold the strings
char array[50];
public:
//default constructor
Cstring();
//constructor that turns a single character into a string
Cstring(char stuff);
//constructor that creates a copy of a string
Cstring(const Cstring& other);
//function that returns the first character of the string
char head();
//function that returns the string with the first character removed
Cstring tail();
//////////////////////////////////
//overloaded operators for strings
//////////////////////////////////
//this operator adds a single character to the end of a string
friend Cstring operator+ (Cstring& left, const char right);
//this operator stores a copy of a string in a result string
Cstring Cstring:perator= (const Cstring& right);
//these are my input/output operators
friend istream& operator>> (istream& input, Cstring& right);
friend ostream& operator<< (ostream& output, Cstring right);
};
/////////////////////////////////////
//Constructors
////////////////////////////////////
//default constructor
Cstring::Cstring()
{
sprintf(array,"");
}
//constructor that makes character a string
Cstring::Cstring(char stuff)
{
sprintf(array, "%c", stuff);
}
//constructor that copies string to result string
Cstring::Cstring(const Cstring &other)
{
sprintf(array, "%s", other.array);
}
/////////////////////////////////////
//Functions
/////////////////////////////////////
//this is the head function that returns first character of string
char Cstring::head()
{
return array[0];
}
//this is the tail function that returns the string without the first
character
Cstring Cstring::tail()
{
Cstring temp;
for(int i=1; i<50; i++)
{
temp.array[i-1] = array;
}
return temp;
}
///////////////////////////////////
//Operator Overloads
///////////////////////////////////
Cstring operator+ (Cstring& left, const char right)
{
Cstring temp;
for(int i=0; i<50; i++)
temp.array = left.array;
for(int j=0; j<strlen(temp.array)+1; j++)
{
if(temp.array[j]=='\0')
{
temp.array[j] = right;
temp.array[j+1] = '\0';
break;
}
}
cout << temp << endl;
return temp;
}
ostream& operator<<(ostream& output, Cstring right)
{
Cstring temp;
int length = strlen(right.array);
for(int i=0; i<length; i++)
{
output << right.array;
}
return output;
}
istream& operator>>(istream& input, Cstring& right)
{
input.getline(right.array,50);
return input;
}
Cstring Cstring:perator=(const Cstring& right)
{
int i=0;
Cstring temp;
temp.array = right.array;
return temp;
}
And I have this Program, in which I am merely trying to output the
results of string.fail() + string.head()
#include <cstdlib>
#include <iostream>
#include "cstringclass.h"
using namespace std;
int main(int argc, char* argv[])
{
Cstring reverse(Cstring& string);
Cstring string1, string2;
cout << "Enter a string" << endl;
cin >> string1;
cout << string1 << endl;
cout << "head: " << string1.head() << endl;
cout << "tail: " << string1.tail() << endl;
cout << "tail,head: " << string1.tail() + string1.head() << endl;
system("pause");
return 0;
}
when i do this though i get a bunch of compiler errors griping about my
parameter, and that basically I can't add them together like that.
Any help would be appreciated.
#include <cstdlib>
#include <iostream>
using namespace std;
class Cstring
{
private:
//array that will hold the strings
char array[50];
public:
//default constructor
Cstring();
//constructor that turns a single character into a string
Cstring(char stuff);
//constructor that creates a copy of a string
Cstring(const Cstring& other);
//function that returns the first character of the string
char head();
//function that returns the string with the first character removed
Cstring tail();
//////////////////////////////////
//overloaded operators for strings
//////////////////////////////////
//this operator adds a single character to the end of a string
friend Cstring operator+ (Cstring& left, const char right);
//this operator stores a copy of a string in a result string
Cstring Cstring:perator= (const Cstring& right);
//these are my input/output operators
friend istream& operator>> (istream& input, Cstring& right);
friend ostream& operator<< (ostream& output, Cstring right);
};
/////////////////////////////////////
//Constructors
////////////////////////////////////
//default constructor
Cstring::Cstring()
{
sprintf(array,"");
}
//constructor that makes character a string
Cstring::Cstring(char stuff)
{
sprintf(array, "%c", stuff);
}
//constructor that copies string to result string
Cstring::Cstring(const Cstring &other)
{
sprintf(array, "%s", other.array);
}
/////////////////////////////////////
//Functions
/////////////////////////////////////
//this is the head function that returns first character of string
char Cstring::head()
{
return array[0];
}
//this is the tail function that returns the string without the first
character
Cstring Cstring::tail()
{
Cstring temp;
for(int i=1; i<50; i++)
{
temp.array[i-1] = array;
}
return temp;
}
///////////////////////////////////
//Operator Overloads
///////////////////////////////////
Cstring operator+ (Cstring& left, const char right)
{
Cstring temp;
for(int i=0; i<50; i++)
temp.array = left.array;
for(int j=0; j<strlen(temp.array)+1; j++)
{
if(temp.array[j]=='\0')
{
temp.array[j] = right;
temp.array[j+1] = '\0';
break;
}
}
cout << temp << endl;
return temp;
}
ostream& operator<<(ostream& output, Cstring right)
{
Cstring temp;
int length = strlen(right.array);
for(int i=0; i<length; i++)
{
output << right.array;
}
return output;
}
istream& operator>>(istream& input, Cstring& right)
{
input.getline(right.array,50);
return input;
}
Cstring Cstring:perator=(const Cstring& right)
{
int i=0;
Cstring temp;
temp.array = right.array;
return temp;
}
And I have this Program, in which I am merely trying to output the
results of string.fail() + string.head()
#include <cstdlib>
#include <iostream>
#include "cstringclass.h"
using namespace std;
int main(int argc, char* argv[])
{
Cstring reverse(Cstring& string);
Cstring string1, string2;
cout << "Enter a string" << endl;
cin >> string1;
cout << string1 << endl;
cout << "head: " << string1.head() << endl;
cout << "tail: " << string1.tail() << endl;
cout << "tail,head: " << string1.tail() + string1.head() << endl;
system("pause");
return 0;
}
when i do this though i get a bunch of compiler errors griping about my
parameter, and that basically I can't add them together like that.
Any help would be appreciated.