Cant concatenate two strings that are the result of two functions.

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::eek: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::eek: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.
 
R

roxorsoxor2345

Can I somehow cast the functions?
cout << "tail,head: " << Cstring(string1.tail()) + char(string1.head())
<< endl;

but that doesnt work, how would I do this?
 
T

Thomas J. Gritzan

N

Nate Barney

Peter said:
since stl exists there is no need anymore for a custom string class.

Not for real programs, but it could be an interesting exercise for
someone who has never done it.

Nate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top