【问题标题】:Trying to print the contents of an array in C++尝试在 C++ 中打印数组的内容
【发布时间】:2014-02-25 18:24:55
【问题描述】:

我正在使用一个函数从一个文本文件中填充一个数组。据我所知,数组填充得很好,但是当我使用 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);

};

感谢任何输入!

干杯

【问题讨论】:

  • 您永远不会用文件中的数据填充数组,也永远不会为plane 实现operator&lt;&lt;。基本上你的程序什么都不做。
  • 你怎么知道数组填充得很好?您的代码似乎没有填充它。它只是用文件中的值覆盖局部变量。
  • 如果不先定义struct plane,就不能使用&lt;&lt; 运算符。如果您不想自己定义运算符,请执行cout &lt;&lt; playArray[i].name 之类的操作。
  • 好的,所以我在 couts 之后添加了 .name 等,我不再收到错误但没有打印。

标签: c++ arrays


【解决方案1】:

您正在尝试打印 plane 类的对象,就在这里:cout &lt;&lt; planeArray[i] &lt;&lt; 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&lt;&lt;

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

【讨论】:

  • 好的,所以我在 couts 之后添加了 .name 等,我不再收到错误,但没有打印任何内容
  • 那是因为您没有将文件中的数据分配给 propper 变量。而不是做 - 例如 - infile &gt;&gt; name;,做infile &gt;&gt; planeArrat[i].name
  • 啊,是的,我应该看到那个,抱歉。我现在收到错误提示错误:在“planeArray”中请求成员“name”,它的指针类型为“plane*”
  • 抱歉,对于 populate 函数内部 for 循环中的每个 cout,我现在将更新我帖子中的代码。
【解决方案2】:
cout << planeArray[i] << endl;
     ^^^^^^^^^^^^^^^^   
            |
            -> of type 'struct plane'

由于&lt;&lt;没有为struct plane重载,所以不能直接放在&lt;&lt;之后。

你需要改成

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;
}

【讨论】:

  • 好的,所以我在 couts 之后添加了 .name 等,我不再收到错误,但没有打印任何内容
【解决方案3】:

程序有几个错误。让我们从辅助功能开始。

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;
    }
}

结构平面没有重载运算符

        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;
    }
}

最后 main 看起来像

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

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

没有必要在右大括号后放置分号。否则,它是任何函数之外的空 b 语句。

【讨论】:

    猜你喜欢
    • 2022-11-20
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多