stumped on arrays

I need write a program that inputs no more than 10 values from a program of tree growth. This program will be using arrays. This should show how much the tree has grown and the difference from the average growth

The output text should look like:
Tree 3 grew 12.4 which is -2.3 from the average

I have no idea how to start this and incorporate an array that ignores anything past ten values.

1
2
3
4
5
6
7
8
9
10
11
#include <fstream>
#include <iostream>

using namespace std;


int main()
{
    
    ifstream fin("growth.txt", ios::in);
How does an input file look?

If the input file has the ten values on a single line, for each tree, you could read the entire line as a string, then make a string stream out of it, and finally extract the first ten values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <sstream>
#include <string>
#include <vector>

// ...

std::string line;
std::vector<float> growth(10);

while (std::getline(fin, line)) // read all lines
{
    std::stringstream line_stream(line);

    // this for() loop can be written more elegantly,
    // but I want the idea behind it to be obvious
    for (int i=0; i < 10; ++i)
        line_stream >> growth[i];
}

Topic archived. No new replies allowed.