Char into Strings beginner question

Apr 7, 2011 at 10:29pm

Hi all,

Im just starting out learning C++ however i'm stuck/have a small question about strings and chars.

for example I have two chars

char* x
char* y

I was wondering if its possible to add these two chars into one string

The code i am using is

std::string string (x,y);

however when i print this to the screen on the char x will appear and im not sure why or even if its possible?

I hope you can help im an absolute beginner to so if i haven't explained myself well let me know and i'll try and explain.

p.s. Hello everyone glad to be part of the forums!

Apr 7, 2011 at 10:37pm
You can't do it directly with the constructor. Instead just append it on after you are done.

1
2
std::string str(x); //construct it
str += y; //append to it 
Apr 7, 2011 at 10:56pm
Thank you very much!

I was sure it could be done i was just not sure how. however when i look at your code it makes a lot of sense why it cant be done the was i was trying!
Topic archived. No new replies allowed.