tree.txt input: 6.5 7.8 -1.4 9 22 -2.7 0 17 |
Is this the entire input file? Then one of your problems is in line 31.
Line 31 reads an int from your input file. So, it sees '6' and then '.'. The period is not part of an integer value, so line 31 stops reading. Now count is set to 6 and the input file stream has the following contents:
.5 7.8 -1.4 9 22 -2.7 0 17 |
The program will now read and average the following values: 0.5, 7.8, -1.4, 9, 22 and -2.7.
I do not understand problem 2.. this is my first time applying arrays in a program what does this mean, or how would this work? |
Line 32 is not valid C++ because the compiler does not know how much memory to allocate to the array at compile time. Some compilers (yours apparently, and for that matter, the 1 I use, too) allow this construct as a convenience to coders, but you should avoid using it. Instead, an array must be declared using a constant integer value. Like this:
1 2
|
const int count = 8;
double numbers[count]; // memory statically allocated on the stack
|
If you want the file to declare how many entries there are (like in line 31), you need to do something like this:
1 2 3 4 5 6 7 8 9
|
int count;
double* numbers;
treefile >> count;
numbers = new double[count]; // new dynamically allocates memory on the heap
// the [count] means it's an array
// do all you processing here
// when you are done, do...
delete[] numbers; // delete[] deallocates memory in an array
|
When you learn about the standard template library, you can use the std::vector class.