0

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);
Nandish
  • 1,136
  • 9
  • 16
Dayus
  • 1
  • The lines of code in your error message do not show up anywhere in the code you've posted. – NathanOliver Apr 19 '22 at 16:10
  • 2
    Your calls `aosFunc(aos[SIZE]);` and `distanceCal(aos[SIZE],x1,y1,z1);` try to supply _one_ instance of the elements in `aos` to the functions. It's out of bounds too. Drop `[SIZE]` when you call the functions. Also read: [Why is `iostream::eof()` inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). TL;DR: This `while(!fin.eof())` will end in tears. – Ted Lyngmo Apr 19 '22 at 16:10
  • ... but by looking at what the `distanceCal` function actually does, it seems that the function signature is wrong and that it should only take one element (by reference or pointer) and not the whole array. Which element is the question. You didn't specify that (other than `[SIZE]` before the edit, but that's out of bounds as I mentioned). – Ted Lyngmo Apr 19 '22 at 16:33
  • You seem to be confusing types with variables (passing `datdata` as an argument is like passing `int`, but you wouldn't write the call `distanceCal(datdata, int, int, int);`), and not quite clear about how arrays work or the point of member variables. I would recommend reading more in your favourite C++ book and writing less code. – molbdnilo Apr 19 '22 at 16:37

0 Answers0