Inputting File information into a array

I have an assignment due, basically it is asking me to have 2 input files that the user chooses, in one file is a list of names of shapes, the other file is the corresponding areas and perimeters of the shapes. The assignment wants me to create arrays for both the names and area and perimeter using the file information and then to sort those arrays into 3 different output folders: one for the shapes in alphabetical order with their respective areas and perimeters, one for the areas in lowest to highest order and one more file for the perimeter .
I feel like everything about the code is pretty straight forward, i just have a few things I'm not quite sure about. I also have a feeling that they are pretty straight forward to solve. I haven't worked out the code yet but I've worked a bit trying to figure out the concepts of input and output file ( I'm not the greatest with them) so i don't have any code to show. my main question is how do i take the information from the input files and put that data into an array for the names and a multidimensional array for the perimeter and area?
You cannot use a multidimensional array if you don't know how large the records of your files are. That's because plain arrays have a fixed size. Rather i would suggest organizing your shape data to a struct record and saving them into a std::vector. Then the code would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <fstream> // for std::ifstream and std::ofstream
#include <iostream>
#include <vector>
#include <string>
#include <algorithm> // for std::sort

struct Shape
{
    double area;
    double perimeter;
    std::string name;
};

int main()
{
    std::ifstream ifs_shape("my_shape_names.txt"); // opens an ifstream of a file
    std::ifstream ifs_area_perimeter("my_araas.tst");
    if( !ifs_shape || !ifs_area_perimeter)
    {
        std::cerr << "Input file stream couldn't opened, program aborted!";
        return 1;
     }

    std::string tmp_name;
    double tmp_area, tmp_perimeter;
    std::vector<Shape> shape_v;
    
    // reads from std::ifstream until the stream is at end (or an error happens).
    while( ifs_shape >> tmp_name && ifs_area_perimeter >> tmp_area >> tmp_perimeter) 
    {
        Shape shape;
        shape.area = tmp_area;
        shape.perimeter = tmp_perimeter;
        shape.name = tmp_name;
        shape_v.push_back(shape);  // adds a value to the end of the vector
    }
    // Sorts the vector by Shape.name
    std::sort( shape_v.begin(), shape_v.end(), [](Shape a, Shape b)->bool{ return a.name < b.name; });
    // ...
}
If you don't know beforehand how many lines your files contain the first step would be to count them and then create dynamic arrays for them.
I agree with nuderobmonkey that a struct and a vector would be the best option.
However it seems you have to learn some old C stuff instead of modern C++, so grid your teeth and use arrays.
I understand that using a structure would be better in this case but for this assignment we are only supposed to use arrays. So how would I go about do that. It’s just taking 2 input files and passing that information to two arrays. How would I get past those problems that you’ve stated
Would I just need to make a while loop to to count the amount of numbers or strings in the file until the end of file and then set that number as the dimensions of the array?


edit. I'm just going to make the array unnecessarily large . ex int my array[20]. I don't think it will be a big deal to make the array the exact size of the file and it'll work the same
Last edited on
Topic archived. No new replies allowed.