尝试使用C ++中的函数填充数组 [英] Trying to populate an array using a function in C++
问题描述
所以,我试图填充和使用函数打印一个小数组,但我打了一个路障。我的代码是:
So, I'm trying to populate and print a small array using functions, however I've hit a bit of a roadblock. The code I have is:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
struct planes
{
string name;
int seats;
int range;
int weight;
};
int populate (planes planesArray[5])
{
string name;
int seats, range, weight, i;
ifstream infile;
infile.open("plane_data.txt");
if (!infile)
{
cout << "File cannot be reached";
}
for (int i = 0; i < 4; i++){
infile >> name;
infile >> seats;
infile >> range;
infile >> weight;
}
infile.close();
}
int main() {
planes planesArray[5];
populate(planes planesArray[5]);
};
在我使用的代码的不同迭代中我收到了大量的错误。上面粘贴了这一个,我得到:
I'm getting a multitude of errors in the different iterations of code I'm using. With this one pasted above I am getting:
line 44: error: expected primary expression before (planesArray)
我有点失去诚实。在数组中有5个数据,我只是不知道如何使用我创建的函数从文件到数组中可靠地获取数据。
I'm a bit lost to be honest. There are 5 pieces of data in the array, I just don't know how to get the data from the file into the array reliably using the functions I've created.
任何帮助将非常感激。
推荐答案
int main() {
planes planesArray[5];
populate( planesArray); // this is how to call your function
}
^^^
note: ; disappeared
当你调用一个给定某个参数的函数时,你不提这个参数的类型。
When you call a function given some parameter you do not mention type of this parameter.
接下来,您将尝试实现您的函数。目前它没有使用数组参数,但我们不会提供准备调试解决方案,而是帮助,当你遇到一些具体问题。
Next, you will try to implement your function. At the moment it does nothing with array parameter, but we won't provide ready tuned-in solution but rather help when you meet some concrete problems.
这篇关于尝试使用C ++中的函数填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!