整数でキーボードから文字を取得しました:
int c = getch();
ではない場合にのみ、文字列に追加したい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);
}
}
ただし、これにより、 のスコープsomefunctionが残っている場合にセグメンテーション エラーが発生します。dtor forstrが呼び出されると、ポインターchが使用できなくなると思います。
どうすればこれを解決できますか?