2017-01-26 6 views
0

私はC++ 11を使って簡単なコードを書いていますが、具体的な値を入力するとCommand terminatedエラーが発生します。`cin 'を使用中に`コマンドが終了しました'

コードは次のとおりです。

#include <iostream> 
using namespace std; 

int main(){ 
    int r, c; 
    cin >> r >> c; 

    int **Data = new int*[r+2](); 
    for(int i=0; i < c+2; i++){ 
     Data[i] = new int[c+2](); 
    } 

    // Input Data 
    for(int n=1; n<r+1; n++){ 
     for(int m=1; m<c+1; m++){ 
      cin >> Data[n][m]; 
     } 
    } 
    return 0; 
} 

このコードは、次のようなほとんどの入力、と正常に動作します:私は特定の値を置けば

clang++ -std=c++11 -stdlib=libc++ test.cpp -o test.out 
./test.out 
2 2 
1 2 
3 4 

しかし、それは衝突する:

clang++ -std=c++11 -stdlib=libc++ test2.cpp -o test2.out 
./test2.out 
5 1 
1 
2 
3 

Command terminated 

なぜこれが起こりますか?

答えて

3

あなたがここにバインドされ、間違ったアッパーを使用している:

for(int i=0; i < c+2; i++){ 

それは次のようになります。

for(int i=0; i < r+2; i++){ 
関連する問題