尝试以C++打印数组的内容
Trying to print the contents of an array in C++
>我正在使用一个函数从文本文件填充数组。据我所知,数组填充良好,但是当我使用 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
,但它不返回任何内容。我会将其声明为具有返回类型void
或bool
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 );
}
无需在右大括号后放置分号。否则,它是任何函数之外的空语句。
相关文章:
- 动态分配的聊天数组打印缺失的数据和空
- 对象数组打印空白字符串
- 数组打印"random"值时出现问题
- 数组打印出垃圾值
- 数组打印奇怪的字符,例如 ∟ @
- 测量我的随机数数组打印的时间始终显示为 0 秒
- 数组打印出错误的数字
- 从对象数组打印字符串对象
- 您好,如何防止此函数使用质数数组打印 1
- 如何从主内部的数组打印总计
- 如何将数组打印为矢量形式
- 有人能给我一些关于在C++中使用数组打印出丰富、完美和不足的数字的提示吗
- 为什么我的C++数组打印相同的值
- 如何阻止我的数组打印地址
- 将值输入到结构数组 + 打印输出
- C++11 为什么 cout 从布尔数组打印大整数
- 从字符串数组打印其中一个字符串时出现空指针赋值错误
- 将无符号字符数组打印为十六进制
- 缩短的数组打印错误的结果
- C++中的字符串数组:打印额外的字符