矢量打印无法正常工作C ++

时间:2016-03-16 00:19:27

标签: c++

我试图将这两个放在一起,如下所示,但两个列在我的代码中是相同的,当它们不应该时,将包括产品运行在下面

int main ()
{

    int nentries;
    int i, j;
    vector<double> xcoord;
    vector<double> ycoord;
    cout << "how many entries?" << endl;
    cin >> nentries;
    double coord[nentries][1];
    cout << "Enter x" << endl;

    for (i = 0; i < nentries; i ++)
        cin >> coord[i][0];
    cout << "Enter y" << endl;
    for (i = 0; i < nentries; i ++)
    cin >> coord[i][1];

    cout << "Scanned." << endl;
    for (i = 0; i < nentries; i++)
    {
        cout << coord[i][0] << "/t" << coord[i][1] << endl;
    }


}

1 个答案:

答案 0 :(得分:0)

coord[i][1]超出范围,您无法访问它。

另请注意,C ++标准不支持变长数组。

更正后的代码:

#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;

int main ()
{

    int nentries;
    int i;
    vector<double> xcoord;
    vector<double> ycoord;
    cout << "how many entries?" << endl;
    cin >> nentries;
    double (*coord)[2] = new double[nentries][2];
    cout << "Enter x" << endl;

    for (i = 0; i < nentries; i++)
        cin >> coord[i][0];
    cout << "Enter y" << endl;
    for (i = 0; i < nentries; i++)
    cin >> coord[i][1];

    cout << "Scanned." << endl;
    for (i = 0; i < nentries; i++)
    {
        cout << coord[i][0] << "/t" << coord[i][1] << endl;
    }

    delete[] coord;
}