I've got a character from the keyboard, by integer:
int c = getch();
an I want to append it to a string only if it isn't a return:
void somefunction()
{
std::string str = "you pressed: ";
int c;
while ( 1 )
{
c = getch();
if ( c == 10 ) break;
char* ch;
sprintf(ch,"%c",c);
str += std::string(ch);
}
}
however, this creates a segmentation error when the scope of somefunction is left. I'm geussing that when the dtor for str is called the pointer to ch isn't available any more.
How can I remedy this?
sprintfprints to.char *ch;tochar ch[2];- but really, use any of the proper std::string methods in the answers...str += ...line, no segfault occurs, leading me to believe thechinitialisation is done properly.