c++ - 使用 `Command terminated` 时为 `cin`

标签 c++ cin

我正在使用 C++ 11 编写一个简单的代码,但如果我输入特定值,则会遇到 Command terminated 错误。

代码是:

#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 

为什么会这样??

最佳答案

你在这里使用了错误的上限:

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

应该是:

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

关于c++ - 使用 `Command terminated` 时为 `cin`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41878452/

相关文章:

c++ - 让独立的 shared_ptr 指向同一个对象是否合法?

c++ - 字符串与 vector 存储大文本

c++ - get() 和 peek() 有助于存储大量数据

C++ for 循环跳过 cin.get()

c++ - 关闭cin与C scanf同步的弊端

c++ - 在这种情况下我应该使用引用成员吗?

c++ - 如何在 C++ 的一条语句中设置多个类成员?

c++ - 如何修复损坏的 OpenCL header

c++ - 在 C++ 中读取输入并分配给不同的变量

c++ - 用极其简单的代码悲伤