i have a problem that how to concatenate more than two string in operator overloading i write a code but it is not run

#include<iostream>
#include<conio.h>
using namespace std;
class count
{
      private:
       char str[50];
       public:
       count()
       {
            str[0]='r';
       }
       void input()
       {
           cout<<"Enter string = ";
           cin.getline(str,50);
       }
       count operator +(count x,count y)
       {
            count t;
            strcpy(t.str,str);
            strcat(t.str,x.str);
            strcat(t.str,y.str)
            return t;
       }
       void show()
       {
            cout<<"string is = "<<str<<endl;
       }
};
int main()
{
    count x,y,z,l;
    x.input();
    cout<<endl;
    x.show();
    cout<<endl<<endl;
    y.input();
    cout<<endl;
    y.show();
    cout<<endl<<endl;
    l.input();
    z=x+(y+l);
    cout<<endl<<endl;
    cout<<endl<<endl;
    z.show();
    getch();
    return 0;
}

it is not working

Recommended Answers

All 13 Replies

but it is not run

If you want better help, you should expand on that.
Errors, warnings, outputs etc...

The entire class is quite dangerous -- how can you be sure the results of line 43 will not overflow the size of str (line 7)?

line 20: What is the contents of str at that point? Hint: undetermined because you failed to initialize str in the constructor. Just setting the first byte to 'r' does nothing at all. It need to be set to '\0', which is what all the fucntions in string.h expect.

my program is not working with '\0'. and in 43 line x is calling object and my logic is (l+y) send two object and i think sendin is failed Plz help me

line 18: operator can only have 1 parameter.

line 3: uncluding all std like that causes a name conflict with your count class name. replace that line with using std::cout; or change the name of your class to something else.

After fixing those problems your program works ok for me.

plz set set my progrm for me plzzz plzzzz

solution is simple: just remove the second parameter to that operatopr function and delete line 23.

brother i want to concatenate three strings therefore pasing two parameters are necessary for concatenating three strings if you have any other solution for three string then tell me and out put should be as follows:
(input)
string 1: "how"
string 2: "are"
string 3: "you"

(output)
(how are you) or (howareyou)
and my english is very very poor so ignore and try to understand my broken english

Did you bother to try what I told you? You're english is good enough and you express yourself very well. This is the code I'm trying to tell you about.

 count()
   {
        str[0]='\0';
   }
count operator +(count x)
       {
            count t;
            strcpy(t.str,str);
            strcat(t.str,x.str);
            return t;
       }

can we cancatenate three string with upper code. i already have been write upper code this code is only for concatenating two string but my problem is that how to concatenate three or more string. according to me there is one way to concatenate three or more string is following.

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class count
{
      private:
       char str[50];
       public:
       count()
       {
            str[0]='\0';
       }
       void input()
       {
           cout<<"Enter string = ";
           cin.getline(str,50);
       }
       count operator +(count x)
       {
            count t;
            strcpy(t.str,str);
            strcat(t.str,x.str);
            return t;
       }
       void show()
       {
            cout<<"string is = "<<str<<endl;
       }
};
int main()
{
    count x,y,z,l;
    x.input();
    cout<<endl;
    x.show();
    cout<<endl<<endl;
    y.input();
    cout<<endl;
    y.show();
    cout<<endl<<endl;
    l.input();
    cout<<endl;
    l.show();
    z=x+y;
    z=z+l;
    cout<<endl<<endl;
    cout<<endl<<endl;
    z.show();
    getch();
    return 0;
}

upper code is working properly.

Change lines 45 and 46 back to the way you had it. There was no need to change that line.

z=x+(y+l);

Here is what I see when I run your program

Enter string = one

string is = one

Enter string = two

string is = two

Enter string = three

string is = onetwothree

There is no theoritical limit to the number of strings you can concantinate with that. for example

Enter string = one
Enter string = two
Enter string = three
Enter string = four
Enter string = five
string is = onetwo threefourfive

All I did to get the avove was add a couple more instances of your count class, removed all those blank lines, then change line 44 to this:
z = x + y + l + m + n;

ok thanks you very much now i understand that in operator overloading, the operator function can take only one argument or parameters.
but you tell me how was my logic in line 45 and 46. plz reply

If the assignment is to concantinate 3 or more strings at the same time then lines 45 and 46 do not test that problem. So if this is a class assignment then your instructor might (or might not) mark you down for that. z = x + y + l is the correct way to test the assignment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.