找出一个正方形逻辑中可能的网格数量,但代码不接受超过六个输入,它停止并说.exe停止工作

find out the number of grids possible in a square logic is write but the code not accepts more than six inputs ,it stops and says .exe stopped working

本文关键字:六个 输入 不接受 exe 停止工作 代码 正方形 一个 网格      更新时间:2023-10-16

这里怎么了?我们不能这样增加数组大小:

int main() {
    int k = 0, c;
    int b[k];
    cin >> c;
    do {
        b[k++] = c;
        cin >> c;
    } while (c != 0);
}

int k=0,c; int b[k]定义了大小 0的数组b。因此,您将无法解决此数组,例如通过b[1]=10,因为这超出了数组的边界并产生未定义的行为。

是C ,使用std::vector代替" C风格"动态数组:

#include <iostream>
#include <vector>
int main() {
    int c;
    std::vector<int> b;
    cin >> c;
    do {
        b.push_back(c);
        cin >> c;
    } while (c != 0);
    for (auto c  : b) {
        cout  << c << endl;
    }
}

尝试一下:

vector<int> b{istream_iterator<int>(cin), istream_iterator()}

这将从cin读取并填充vector<int>。请注意,您必须发送EOF才能终止循环。在我的计算机上, ctrl z