试图用C ++打印数组的内容

时间:2014-02-25 18:24:55

标签: c++ arrays

我正在使用一个函数来填充文本文件中的数组。据我所知,数组填充正常,但是当我使用cout打印数组时,我收到很多错误,表示文件中的数据无法转换为不同的数据类型(如果这些错误是要求,评论让我知道)。

我到目前为止的代码是:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

struct plane //strcutre of plane data to use
{
string name;
string direction;
int id;
int coordX;
int coordY;
int height;
int speed;

};

int populate (plane planeArray[5])
{
string name, direction;
int id, coordX, coordY, height, speed, i;
ifstream infile;                              //declare input file stream
infile.open("plane_data.txt");                //opens file
if (!infile)
{
    cout << "File cannot be reached";         //checks for invalid file path
}

for (int i = 0; i < 4; i++){
    infile >> planeArray.name[i];
    infile >> planeArray.direction[i];
    infile >> planeArray.id[i];
    infile >> planeArray.coordX[i];
    infile >> planeArray.coordY[i];
    infile >> planeArray.height[i];
    infile >> planeArray.speed[i];

}

infile.close();

}

void text_display(plane planeArray[5])
{
for (int i = 0; i < 4; i++)
{
    cout << planeArray[i].name << endl;
    cout << planeArray[i].id << endl;
    cout << planeArray[i].coordX << endl;
    cout << planeArray[i].coordY << endl;
    cout << planeArray[i].height << endl;
    cout << planeArray[i].speed << endl;
}
}


int main() {

plane planeArray[5];
populate( planeArray);
//text_display( planesArray);

};

任何输入都表示赞赏!

干杯

3 个答案:

答案 0 :(得分:1)

您正在尝试打印类plane的对象,就在这里:cout << planeArray[i] << endl;。编译器给你错误,因为它根本不知道如何做到这一点。根据您的要求,您可以将行更改为:

cout << &planeArray[i] << endl;

打印地址,或

cout << planeArray[i].name << endl;
cout << planeArray[i].direction << endl;
cout << planeArray[i].id << endl;
cout << planeArray[i].coordX << endl;
cout << planeArray[i].coordY << endl;
cout << planeArray[i].height << endl;
cout << planeArray[i].speed << endl;

如果您需要特定对象的数据。

此外,如果你想按照自己的方式去做(这当然是可能的),你可以告诉编译器如何做到这一点。只需定义一个重载的operator<<

ostream& operator<<(ostream& os, const plane& myObject){
    // here goes the code from above
    return os;
}

答案 1 :(得分:1)

cout << planeArray[i] << endl;
     ^^^^^^^^^^^^^^^^   
            |
            -> of type 'struct plane'

由于您<<尚未超载struct plane,因此无法在<<之后直接加载。{/ p>

您需要将其更改为

cout << planeArray[i].name << endl;
...
cout << planeArray[i].speed << endl;

您必须先保存数据,然后才能打印出来。

更改

for (int i = 0; i < 4; i++){
    infile >> name;    // you read the data, but then throw them away...
    ...
    infile >> speed;
}

for (int i = 0; i < 4; i++){
    infile >> planeArray[i].name;
    ...
    infile >> planeArray[i].speed;
}

答案 2 :(得分:0)

该程序有几个错误。让我们从辅助函数开始。

int populate (plane planeArray[5])
{
    string name, direction;
    int id, coordX, coordY, height, speed, i;
    ifstream infile;                              //declare input file stream
    infile.open("plane_data.txt");                //opens file
    if (!infile)
    {
        cout << "File cannot be reached";         //checks for invalid file path
    }

    for (int i = 0; i < 4; i++){
        infile >> name;
        infile >> direction;
        infile >> id;
        infile >> coordX;
        infile >> coordY;
        infile >> height;
        infile >> speed;
    }

    infile.close();
}

首先,此函数需要额外的参数来报告数组中的元素数量。问题是这些函数声明是等价的

int populate (plane planeArray[5]);
int populate (plane planeArray[10]);
int populate (plane planeArray[]);
int populate (plane *planeArray);

所以最好将此函数声明为

int populate (plane planeArray[, int n );

其中n是数组中元素的数量。

此外,即使输入文件未打开,您也会尝试输入数据。

    if (!infile)
    {
        cout << "File cannot be reached";         //checks for invalid file path
    }

    for (int i = 0; i < 4; i++){
        infile >> name;
        // other stuff

该函数被声明为具有返回类型int,但它不返回任何内容。我会声明它的返回类型为voidbool

bool populate (plane planeArray[], int n );

在循环中,您在本地变量中输入数据。数组本身没有被更改。

考虑到已经说过的所有内容,该功能可以采用以下方式

bool populate (plane planeArray[], int n );
{
    ifstream infile( "plane_data.txt" );

    if ( !infile )
    {
        cout << "File cannot be reached";         //checks for invalid file path
        return false;
    }

    for ( int i = 0; infile && i < n; i++ )
    {
        infile >> planeArray[i].name;
        infile >> planeArray[i].direction;
        infile >> planeArray[i].id;
        infile >> planeArray[i].coordX;
        infile >> planeArray[i].coordY;
        infile >> planeArray[i].height;
        infile >> planeArray[i].speed;
    }

    return ( true );
}

功能text_display也有效

void text_display(plane planeArray[5])
{
    for (int i = 0; i < 4; i++)
    {
        cout << planeArray[i] << endl;
    }
}

结构平面没有重载运算符&lt;&lt;所以这句话

        cout << planeArray[i] << endl;

无效。编译器在解析此语句时会发出错误。

应该是

void text_display( plane planeArray[], int n )
{
    for ( int i = 0; i < n; i++ )
    {
        cout << planeArray[i].name << endl;
        cout << planeArray[i].direction << endl;;
        cout << planeArray[i].id << endl;;
        cout << planeArray[i].coordX << endl;;
        cout << planeArray[i].coordY << endl;;
        cout << planeArray[i].height << endl;;
        cout << planeArray[i].speed << endl;;
        cout << endl;
    }
}

最后主要看起来像

int main() 
{
    const int N = 5;
    plane planeArray[N] = {};

    populate( planeArray, N );
    text_display( planesArray, N );
}

关闭括号后无需放置分号。否则它在任何函数之外都是空的bstatement。

相关问题