[英]Need help passing an array of structure to a function
So, I am currently studying at school and I need to do my homework. 因此,我目前正在学校学习,需要做作业。 I am a beginner in C++ and somehow the compiler shows me an error in my code.
我是C ++的初学者,因此编译器以某种方式向我显示了我的代码中的错误。 Basically, I have a
.txt
file where the data is stored. 基本上,我有一个
.txt
文件,用于存储数据。 The .txt
file looks something like this: .txt
文件如下所示:
5
Petras 23.25 10.50
Rimas 125.40 1.20
Romas 55.00 1.00
Jurgis 1000.90 0.25
Algis 15.00 25.50
The first line shows how much people in the list we have, so I created integer n
. 第一行显示列表中有多少人,因此我创建了整数
n
。
Next we have a list of people. 接下来,我们有一个人名单。 The list tells the name of the person, how much money he has in different currency, and shows the rate of exchange to euros.
该列表告诉该人的姓名,他用不同货币拥有的货币数量,并显示对欧元的汇率。
And the problem is that I am trying to find the sum of the money they have in Euros. 问题是我试图找出他们用欧元计算的总金额。 This is my code.
这是我的代码。
#define USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
const int Cn = 100;
const int Cname = 15;
int n;
struct listofpeople {
string name;
double MoneyInOtherCurrency;
double RateOfExchange;
double MoneyInEuros;
};
listofpeople A[Cn + 1];
void data();
void ChangeCurrency();
double sum(double C[], int m);
int main () {
data();
ChangeCurrency();
cout << sum(A[].MoneyInEuros, n);
return 0;
}
//-------------------------------------------------------
void data(){
ifstream is ("U2duom.txt");
is >> n;
char symbols[Cname + 1];
for(int i = 1; i <= n; i++){
is.ignore(80, '\n');
is.get(symbols, Cname);
A[i].name = symbols;
is >> A[i].MoneyInOtherCurrency;
is >> A[i].RateOfExchange;
}
}
//----------------------------------------------------------
void ChangeCurrency(){
for(int i = 1; i <= n; i++){
A[i].MoneyInEuros = A[i].MoneyInOtherCurrency*A[i].RateOfExchange;
cout << A[i].name << " " << A[i].MoneyInEuros << " " <<
A[i].MoneyInOtherCurrency << " " << A[i].RateOfExchange << endl;
}
}
//---------------------------------------------------------------
double sum(double C[], int m){
double a = 0;
for(int i = 1; i <= m; i++){
a= a + C[i];
}
return a;
}
And the problem is that the compiler shows me an error in the line where i try to print the sum. 问题是编译器在我尝试打印总和的行中向我显示错误。 Could anyone help me?
有人可以帮我吗? Thanks.
谢谢。
EDIT: 编辑:
My compiler shows this error: 我的编译器显示此错误:
error: expected primary-expression before ']' token
If I specify array elements I want to use, for example: 如果指定要使用的数组元素,例如:
sum(A[n].MoneyInEuros, n);
The compiler shows this error: 编译器显示此错误:
cannot convert 'double' to 'double*' for argument '1' to 'double
sum(double*, int)'
double sum(double C[], int m);
takes an array of doubles, but you only have an array of listofpeople
. 需要一个双打数组,但您只有一个
listofpeople
数组。 This doesn't work. 这行不通。
You have to change sum
so that it takes an array of listofpeople
, or you change your data structure from a Structure Of Arrays to an Array of Structures (better for performance, more complex to handle). 您必须更改
sum
,以使其采用listofpeople
数组,或者将数据结构从“数组结构”更改为“结构数组”(性能更好,处理起来更复杂)。
Typically, sum
requires reimplementing: 通常,
sum
需要重新实现:
double sum(listofpeople *s, int m);
Be aware that in C++, we don't use the [] notation for types. 请注意,在C ++中,我们不对类型使用[]表示法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.