473,671 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::operat or= (const Cstring& right);
//these are my input/output operators
friend istream& operator>(istre am& input, Cstring& right);
friend ostream& operator<< (ostream& output, Cstring right);
};

/////////////////////////////////////
//Constructors
////////////////////////////////////

//default constructor
Cstring::Cstrin g()
{
sprintf(array," ");
}

//constructor that makes character a string
Cstring::Cstrin g(char stuff)
{
sprintf(array, "%c", stuff);
}

//constructor that copies string to result string
Cstring::Cstrin g(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.a rray)+1; j++)
{
if(temp.array[j]=='\0')
{
temp.array[j] = right;
temp.array[j+1] = '\0';
break;
}
}
cout << temp << endl;
return temp;
}

ostream& operator<<(ostr eam& output, Cstring right)
{
Cstring temp;
int length = strlen(right.ar ray);
for(int i=0; i<length; i++)
{
output << right.array[i];
}
return output;
}

istream& operator>>(istr eam& input, Cstring& right)
{
input.getline(r ight.array,50);
return input;
}
Cstring Cstring::operat or=(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 2810
Can I somehow cast the functions?
cout << "tail,head: " << Cstring(string1 .tail()) + char(string1.he ad())
<< 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
22843
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. This means I need to append trailing spaces to a text string (such as "Test1 ") or append leading space in front of a text string that contains a number (such as " 12.00"). Now, the stored procedure works fine when I run it in Query Analyzer....
30
27015
by: priya | last post by:
Hi How to concatenate two integer Values. Example Program : #include "Port.h" #include "BinaryConversion.h" # include "iostream.h"
13
2370
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 and so forth to the 1st string. Any ideas how to go about this? Thanks,
2
35647
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 there a way to do this in an SQL Server View?
14
27175
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++, and have the result as a char*. I first tried using strcat, but you need a const char* as the second parameter to work, but I don't know all the issues regarding const chars, so maybe I overlooked something.
4
2504
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 any value to that color type property.. my code is Dim Col1Color as String
5
3548
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: -- ostringstream this does the job well, but: * all types involved have to support operator<< * we will lose some readibility in the code because we will always have to create a temp object to do the concatenation
13
21825
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 will concatenate the value of ABC to MYSTR . I need this at compile time.
10
7437
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 able to produce a query that gives me two fields, Part_Number and Product_Type. Below you can see some sample data: Part_Number Product_Type 10MC35231 XYZ1A
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8911
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2808
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.