Help me

I want to add two char in a string and I dump this two character another string. How is it possible? I tried but not success.
1
2
3
4
5
6
7
8
9
#include<iostream>
#include<string>
using namespace std;
int main(){
	string x="afgfd";
	string z=x[0]+x[1];
	cout<<z<<endl;
	return 0;
}

It shows this error
error C2440: 'initializing' : cannot convert from 'int' to 'class
x[0]+x[1] will perform an addition between characters ( characters are stored as numbers ) and pass the result of that to the string constructor.
If you want to get the string concatenation, one of the operands has to be a string
eg:
string z = string(x[0]) + x[1];
1
2
string z = x[0];
z += x[1];
You can try using strcpy and strcat.

string x = "afgfd";
strcpy (string z, string x[0]);
strcat (string z, string x[1]);

Is this what you really want?
Those functions won't work with C++ strings
Topic archived. No new replies allowed.