2010-12-31 5 views
1

整数(スコア)を文字に変換し、それにプレーヤーの名前(player1)を付加する次のコードがあります。それはその後に表示されます。これは、大きなプロジェクトの一部である:整数をC++の文字ポインタに変換し、それを別のchacterポインタに追加する

#include <iostream> 
#include <string.h> 
using namespace std; 

char* convertIntTochar(int number) 
{ 
    char t[3]; 
    t[0] = 0; 
    t[1] = 0; 
    t[2] = '\0'; 

    int i = 0; 
    for(; number != 0; i++) 
    { 
     t[i] = ((number%10) + 48); 
     number/=10; 
    } 

    if(i == 2) 
    { 
     char temp = t[0]; 
     t[0] = t[1]; 
     t[1] = temp; 
    } 
    else 
     t[i] = '\0'; 
    char *ans = t; 
    return ans; 
} 

int main() 
{ 
    char str11[] = "Player1: "; 
    char *str1 = str11; 
    char *str2 = convertIntTochar(11); 
    strcat(str1 , str2); 

    while(*str1) 
    { 
     cout<<*(str1++); 
    } 

    return 0; 
} 

それは正しくコンパイルが、私はそれを実行すると、それは次のようなエラーが表示されます。

*** stack smashing detected ***: ./a.out terminated 
======= Backtrace: ========= 
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x50)[0x9b3390] 
/lib/tls/i686/cmov/libc.so.6(+0xe233a)[0x9b333a] 
./a.out[0x80487ff] 
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x8e7bd6] 
./a.out[0x8048621] 
======= Memory map: ======== 
00110000-00134000 r-xp 00000000 08:06 2887608 /lib/tls/i686/cmov/libm-2.11.1.so 
00134000-00135000 r--p 00023000 08:06 2887608 /lib/tls/i686/cmov/libm-2.11.1.so 
00135000-00136000 rw-p 00024000 08:06 2887608 /lib/tls/i686/cmov/libm-2.11.1.so 
004b9000-004d4000 r-xp 00000000 08:06 2887597 /lib/ld-2.11.1.so 
004d4000-004d5000 r--p 0001a000 08:06 2887597 /lib/ld-2.11.1.so 
004d5000-004d6000 rw-p 0001b000 08:06 2887597 /lib/ld-2.11.1.so 
0077d000-00866000 r-xp 00000000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13 
00866000-00867000 ---p 000e9000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13 
00867000-0086b000 r--p 000e9000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13 
0086b000-0086c000 rw-p 000ed000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13 
0086c000-00873000 rw-p 00000000 00:00 0 
008d1000-00a24000 r-xp 00000000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so 
00a24000-00a25000 ---p 00153000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so 
00a25000-00a27000 r--p 00153000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so 
00a27000-00a28000 rw-p 00155000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so 
00a28000-00a2b000 rw-p 00000000 00:00 0 
00a3b000-00a58000 r-xp 00000000 08:06 2883667 /lib/libgcc_s.so.1 
00a58000-00a59000 r--p 0001c000 08:06 2883667 /lib/libgcc_s.so.1 
00a59000-00a5a000 rw-p 0001d000 08:06 2883667 /lib/libgcc_s.so.1 
00b74000-00b75000 r-xp 00000000 00:00 0   [vdso] 
08048000-08049000 r-xp 00000000 08:06 4719693 /home/dhruv/Desktop/a.out 
08049000-0804a000 r--p 00000000 08:06 4719693 /home/dhruv/Desktop/a.out 
0804a000-0804b000 rw-p 00001000 08:06 4719693 /home/dhruv/Desktop/a.out 
08b67000-08b88000 rw-p 00000000 00:00 0   [heap] 
b77f7000-b77f9000 rw-p 00000000 00:00 0 
b780d000-b7810000 rw-p 00000000 00:00 0 
bfd2a000-bfd3f000 rw-p 00000000 00:00 0   [stack] 
Player1: "�ӿ�XMAborted 

その理由は何ですか?どのようにそれを修正することができます。私はすでにconvertIntTochar関数にヌル終了文字を入れました。唯一の2桁の数字のために働くconvertIntTochar

答えて

2
char str11[] = "Player1: "; 

これは問題です。文字列の連結に十分な余地がありません。これを試してみてください:

char str11[100] = "Player1: "; 

いっそ、Cのようなchar*の代わりにstd::stringを使用しています。 (using namespace stdが、その後std::stringstd::一部を省略することができますが存在するので、私はちょうどそれを残すことを好む)、文字列の問題を解決可能な最小の変更はこれらです:

#include <iostream> 
#include <string> // instead of <string.h> 
using namespace std; 

std::string convertIntTochar(int number) 
{ 
    ... 
} 

int main() 
{ 
    std::string str1 = "Player1: "; 
    std::string str2 = convertIntTochar(11); 
    str1 += str2; 

    cout << str1; 

    // Or even more effective, just one line of code: 
    cout << "Player1: " << convertIntTochar(11); 

    return 0; 
} 
+0

をしたい場合は、あなたがのstd ::文字列について教えてくださいできますか?私は言語にちょっと新しいです – higherDefender

+0

'str11 [100]'?3つだけの文字を追加します!!! – Cratylus

+0

なぜ安く遊べますか?これらはどこから来たのか、さらに多くのバイトがあります。 – Dialecticus

6

多くの問題ここに...

  1. 。チェックは行われません。
  2. char t[3]で定義されているconvertIntTocharはローカル変数です。ポインタを返すことはできません。このポインタはconvertIntTocharの外側に使用してください。
  3. strcat(str1 , str2);は、すでに完全な(str11)配列に追加してスタックを上書きします。

std :: stringsに切り替えるだけで簡単になります。

0

char *にint型を変換することができますかどうかを確認しますコンパイラでitoaを使用してください。
サポートされていない場合は、目的を達成するための実装を見つけることができます。
これは、C文字列を使用して行う必要がある場合です。

1

std :: stringsとstd :: ostringstreamsを使用すると、はるかに簡単です。

#include <sstream> 
#include <iostream> 
std::ostringstream player_score_stream; 
player_score_stream << "Player1: " << score_as_an_integer; 
std::string player_score(player_score_stream.str()); 
std::cout << player_score; 

、あなたはconstのchar型を返す読み取り専用のC文字列、使用player_score.c_str()、*

関連する問題