473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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::operator= (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[i];
}
return temp;
}

///////////////////////////////////
//Operator Overloads
///////////////////////////////////

Cstring operator+ (Cstring& left, const char right)
{
Cstring temp;
for(int i=0; i<50; i++)
temp.array[i] = left.array[i];

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[i];
}
return output;
}

istream& operator>>(istream& input, Cstring& right)
{
input.getline(right.array,50);
return input;
}
Cstring Cstring::operator=(const Cstring& right)
{
int i=0;
Cstring temp;
temp.array[i] = right.array[i];
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.

Oct 5 '06 #1
4 2797
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?

Oct 6 '06 #2

roxorsoxor2345 wrote:
I have this custom string class that I have created.

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

Oct 6 '06 #3
roxorsoxor2345 schrieb:
I have this custom string class that I have created.
[...]
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.
Read the FAQ:
http://www.parashift.com/c++-faq-lite/

Especially here:
http://www.parashift.com/c++-faq-lit...rrectness.html
and here:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

I also recommend to use std::string instead.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 6 '06 #4
Peter wrote:
roxorsoxor2345 wrote:
>I have this custom string class that I have created.

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
Oct 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely....
30
by: priya | last post by:
Hi How to concatenate two integer Values. Example Program : #include "Port.h" #include "BinaryConversion.h" # include "iostream.h"
13
by: jt | last post by:
I can't seem to find a way to concatenate strings that have nulls within the string. I have a string that I need another string that has nulls in it and what to append the 2nd string, 3 string...
2
by: Rico | last post by:
Hello, I'm wondering if there is a way to concatenate two fields or a field and a string value in a single field in a view? Where in Access I might write; & " (m3)" as TotalVolume is...
14
by: metamorphiq | last post by:
Hello, I'm a Java programmer, so I'm probably asking a very simple question here, but I have trouble solving it :) I'd like to know how you concatenate multiple (4, in my case) char* in C++,...
4
by: Avi | last post by:
Hi I am creating web application in which i want to assign by default values to the property which i had created my own. In that one of the property is of type color and i am unable to assign...
5
by: Diego Martins | last post by:
Since C++ (and STL) have many ways to do string concatenation, I want to hear (read) from you how you do to concatenate strings with other strings and other types. The approaches I know are: --...
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
10
by: Aaron Hoffman | last post by:
Hello, I'm hoping someone might be able to offer some guidance to my problem. I have one query in MS Access which consists of 2 tables joined by a SEQUENCE_ID. By joining the two tables I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.