append() function

I am trying to use the append() function with a string:

1
2
3
4
string my_string;
char my_char = (char)(37);   //gets the character from ASCII 37
my_string.append(my_char);
	        
}

If I were to use my_string.append("g") it would work ok.
But now that I have my_string.append(my_char) it does not.

Any thoughts why?
Perhaps it wants c-style string and not just an individual char? I'm not sure to be honest, try checking the function's prototype on this website's reference.
yes actually I did check:

http://www.cplusplus.com/reference/string/string/append/

Look at the first example where it does

First example; line 13

str.append(str2);

I have know idea why it doesn't work for me.
std::string::append() basically takes a string-like object and appends a copy of it to the current std::string.
If you want to add a character, just use std::string::push_back(): my_string.push_back(37)
Last edited on
thanks! makes sense. (...STRING-LIKE OBJECT)

I thought .push_back() only works with vectors and I wanted to avoid using vectors.

In the meantime I found another solution.

So 2 solutions:

 
my_string.push_back(my_char)


and

 
my_string += my_char


They both work like charm!
std::string is basically just an std::vector with a few functions for string manipulation.
Topic archived. No new replies allowed.