整数でキーボードから文字を取得しました:
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
が使用できなくなると思います。
どうすればこれを解決できますか?