将文件读入mattrix(2D数组)

时间:2017-05-19 13:16:24

标签: c++

我的输入文件是:

3 4
1 −3 8 2
3 10 1 0
2 −2 3 1

在这种情况下,第一行表示mattrix [3] [4]的大小,我需要将文件输入矩阵。我在这里找到了许多类似问题的主题,但对我没什么用。似乎vstup >> a >> b;没有正确阅读。他们的价值总是:-858993460

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;
int main(int argc, _TCHAR* argv[])
{
int a,b;
double matica[100][100];
ifstream vstup("vstup.txt");

if (!vstup) {
    cout << "subot sa neda otvorit.\n";
    system("pause");
    return 0;
}

vstup >> a >> b;
for (int y = 0; y < a; y++) {
    for (int x = 0; x < b; x++) {
        vstup >>  matica[x][y];
    }
}
vstup.close();

cout << a << b;
    for (int y = 0; y < a; y++) {
    for (int x = 0; x < b; x++) {
        cout << matica[x][y] << " ";
    }
    cout << endl;
}



system("pause");
return 0;
}

0 个答案:

没有答案
相关问题