Q: Program Practice Exercise On Streams C++ so I'm practing using stream for the future when I want to make a video game with just text but I 'm having a problem.This ebook I'm using wants me to Write a program that reads a text file containing floating-point numbers. Print the average of the numbers in the file. Prompt the user for the file name.
Here is my work so far:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream Practice_Text;

    double Numbers[5] = { 3.4, 5.6, 7.8, 9.3, 2.1 };

    Practice_Text.open("./Point_Numbers");

    Practice_Text << Numbers;

    Practice_Text.close();

    double practice_mode[5];

    double avg_number;

    double First_Num;

    ifstream Open_Text;

    Open_Text.open("./Point_Numbers");

    Open_Text >> First_Num[1];

    cout << First_Num[1];

    Open_Text.close();

    return 0;
}

Now please forgive the use of using namespace as I know that it's not recommended but I wanted to focus on using streams here. I used an array of five elements and named it Numbers. I then put it inside the file, close it, and then reopened it in the hopes of printing out the average of each number.
Now I know that what I'm doing is not the way to print out all the numbers then printing out the average, but what I was trying to do was to get out the first number and print it, to at least know that I'm on the right track but I keep getting zero instead, what am I doing wrong here? I understand that I can do it with pointers but I would rather do it with what I have here. 

A: I fixed your compiler errors:


*

*You can't directly write an array to a file. The array decays to the pointer to the first element

*You can open a file with the fstream constructor

*The fstream destructor can also close your file.

*First_Num is a double, not an array. You can't subscript it

*I removed unused variables

*I added checks if the file was correctly opened

*I removed using namespace std;. It's dangerous, especially for beginners. Many beginner don't know with names are already used in that namespace. They use the same names for their variables or functions and get a program with strange behavior.


Code:
#include <iostream>
#include <fstream>
using std::cout;
using std::ifstream;
using std::ofstream;

int main()
{
    double Numbers[5] = { 3.4, 5.6, 7.8, 9.3, 2.1 };

    {
        ofstream Practice_Text("./Point_Numbers");
        if (!Practice_Text) return EXIT_FAILURE;
        for (const auto &Number: Number) {
            Practice_Text << Number;
        }
    }

    double First_Num;

    {
        ifstream Open_Text("./Point_Numbers");
        if (!Open_Text) return EXIT_FAILURE;
        Open_Text >> First_Num;
    }

    cout << First_Num;

    return EXIT_SUCCESS;
}

