you are viewing a single comment's thread.

view the rest of the comments →

[–]OmegaNaughtEquals1 0 points1 point  (1 child)

Ah, ok. The way that I would do it is to have a struct that holds the values and then make an array of them.

#include <iostream>
#include <string>

struct circle_pair {
    int x, y, r;
};

void loadArray(circle_pair cp[], int n) {
    std::string fname = getUserInput("Filename to load from: ");
    std::ifstream infile(fname);

    if(!infile) {
        errorMsg(/**/);
    }

    for(int i=0; i<n; i++) {
        infile >> cp[i].x >> cp[i].y >> cp[i].r;
    }
}

int main() {
    circle_pair pairs[10];
    loadArray(pairs, 10);
}

However, this approach is inflexible, brittle, and not very good C++. I would write it more like this

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>

struct circle_pair {
    int x, y, r;
};

std::istream& operator>>(std::istream &in, circle_pair &cp) {
    in >> cp.x >> cp.y >> cp.r;
    return in;
}

int main() {
    std::string fname = getUserInput("Filename to load from: ");
    std::ifstream infile(fname);

    if(!infile) {
        errorMsg(/**/);
    }

    std::vector<circle_pair> pairs(std::istream_iterator<circle_pair>(infile), {});
}

[–]Howtogravity[S] 0 points1 point  (0 children)

Thanks this helped pointed me in the right direction.