尝试以C++打印数组的内容

Trying to print the contents of an array in C++

本文关键字:数组 打印 C++      更新时间:2023-10-16

>我正在使用一个函数从文本文件填充数组。据我所知,数组填充良好,但是当我使用 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的对象,就在这里: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;
}
cout << planeArray[i] << endl;
^^^^^^^^^^^^^^^^   
|
-> of type 'struct plane'

由于您没有超载<<struct plane,因此不能直接将其放在<<之后。

您需要将其更改为

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

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

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

最后主要看起来

int main() 
{
const int N = 5;
plane planeArray[N] = {};
populate( planeArray, N );
text_display( planesArray, N );
}

无需在右大括号后放置分号。否则,它是任何函数之外的空语句。