I'm trying to make an array of structures using data from a file, and then calculate the distance from a user inputted point to the closest data point. I am lost and could use help.
This is my code:
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
const int SIZE = 3;
ifstream fin; //declaration of the stream type input
ofstream fout;
struct datdata {
int x;
int y;
int z;
double distance;
};
datdata aos[SIZE];
void aosFunc(datdata aos[SIZE])
{
for (int i = 0; i < SIZE; i++) {
cin >> aos[i].x >> aos[i].y >> aos[i].z;
}
}
float distanceCal(datdata aos[SIZE], int x1, int y1, int z1)
{
aos->distance = sqrt(pow(aos->x - x1, 2) + pow(aos->y - y1, 2) + pow(aos->z - z1, 2) * 1.0);
return aos->distance;
}
int main()
{
int x1;
int y1;
int z1;
ifstream fin("data.dat");
if (fin.fail()) {
cout << "Unable to open input file." << endl;
exit(EXIT_FAILURE); //exit if cannot open file
}
while (!fin.eof()) {
aosFunc(datdata);
}
cout << "Enter your 3 coordinates (x y z): ";
cin >> x1 >> y1 >> z1;
distanceCal(datdata, x1, y1, z1);
cout << "The distance is from the structure at " << x1 << " " << y1 << " " << z1 << " to the stored structure is " << aos->distance << "." << endl;
return 0;
}
The errors are:
<source>: In function 'int main()':
<source>:45:24: error: expected primary-expression before ')' token
aosFunc(datdata);
^
<source>:50:24: error: expected primary-expression before ',' token
distanceCal(datdata, x1, y1, z1);
I've tried changing aos[SIZE]
in place of the datdata
error, but this results in a new error:
<source>: In function 'int main()':
<source>:45:26: error: cannot convert 'datdata' to 'datdata*' for argument '1' to 'void aosFunc(datdata*)'
aosFunc(aos[SIZE]);
^
<source>:50:38: error: cannot convert 'datdata' to 'datdata*' for argument '1' to 'float distanceCal(datdata*, int, int, int)'
distanceCal(aos[SIZE], x1, y1, z1);