[英]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'
由於你沒有重載<<
for 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;
}
}
結構平面沒有重載運算符<< so this statement
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.