尝试使用C中的一个函数来填充数组++ [英] Trying to populate an array using a function in C++

查看:535
本文介绍了尝试使用C中的一个函数来填充数组++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想填充和使用功能,打印一小阵,但是我已经打了一个有点路障。在code我是:

 的#include<&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&; cstdlib>
#包括LT&;串GT;使用命名空间std;飞机结构
{
    字符串名称;
    诠释席;
    INT范围;
    诠释权重;
};INT填充(平面planesArray [5])
{
    字符串名称;
    INT座椅,范围,重量,我;
    ifstream的INFILE;
    infile.open(plane_data.txt);
    如果(!INFILE)
    {
        COUT<< 文件不能达到
    }    对(INT I = 0; I&下; 4;我++){
        INFILE>>名称;
        INFILE>>席;
        INFILE>>范围;
        INFILE>>重量;    }    infile.close();}诠释主(){    平面planesArray [5];
    填充(平面planesArray [5]);};

我得到在code的不同迭代我使用的是错误的,众说纷纭。有了这一块上面我正在粘贴:

  44行:错误:预计前原前pression(planesArray)

我有点失去了诚实。有5件在阵列中的数据,我只是不知道如何从文件到阵列中使用可靠我创建函数获取数据。

任何帮助将是非常美联社preciated!


解决方案

  INT的main(){
  平面planesArray [5];
  填充(planesArray); //这是如何调用你的函数
}
^^^
注意: ;消失

当你打电话给一些参数的函数,你不提这个参数的类型。

接下来,我们将尝试执行你的函数。目前,它不做任何处理数组参数,但我们不会提供现成调入的解决方案,但你遇到的一些具体问题,而提供帮助。

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)

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.

Any help would be much appreciated!

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆