/*---
    CS 111 - Week 14 Lecture 2 - 2025-12-04

    compile using:
        g++ 111lect14-2.cpp -o 111lect14-2
    run using:
        ./111lect14-2

    by: Sharon Tuttle
    last modified: 2025-12-04
---*/

#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>  // to do stream-based file i/o
using namespace std;

/*===
    REFACTORING so now uses a for-loop and shortcuts!

    signature: sum_array: double[] int -> double
    purpose: expects an array of numbers and its
       size, and returns the sum of all of those numbers.
    tests:
       double sales[3] = {120.10, 30.20, 1.01};
       double my_list[5] = {10, 20, 3, 4, 100};

       sum_array(sales, 3) == (120.10 + 30.20 + 1.01)
       sum_array(my_list, 5) == 137.0
===*/

double sum_array(double num_set[], int set_size)
{
    double sum_so_far = 0.0;

    // "walk" through the array num_set, adding up
    //     its contents

    for (int index = 0; index < set_size; index += 1)
    {
        sum_so_far += num_set[index];
    }

    return sum_so_far;
}


/*---
   test the function above,
   and show a small example of 
       writing to and reading from a file
---*/

int main()
{
    cout << boolalpha;

    cout << "*** Testing: sum_array ***" << endl;

    double sales[3] = {120.10, 30.20, 1.01};
    double my_list[5] = {10, 20, 3, 4, 100};

    cout << (sum_array(sales, 3) == (120.10 + 30.20 + 1.01)) << endl;
    cout << (sum_array(my_list, 5) == 137.0) << endl;

    cout << "just for fun: "
         << sum_array(sales, 3) << endl;

    //=====
    // TRYING OUT some file input/output!!

    // to write to a file, I need an output file stream

    ofstream my_fout;

    // I need to connect my new output file stream
    //    to a file. 
    // (NOTE: this version of ofstream's open method 
    //     will create a file with this name
    //     if it does not currently exist, or open
    //     if for writing and delete all of its current
    //     contents if it does currently exist!)

    my_fout.open("outfile.txt");

    // NOW I can write to the file using this
    //   ofstream similarly to writing to standard output:

    my_fout << "LOOKY" << endl;

    // GOOD PRACTICE is to CLOSE your file stream
    //     when you are done

    my_fout.close();

    // to read from a file, you can use an input file stream

    ifstream my_fin;

    // you still need to attach it to the file be read
    //     (but this expects a file with this name to
    //     already exits):

    my_fin.open("outfile.txt");

    // if that worked, you can now read from that file
    //    like you read from standard input using
    //    cin (except you don't need a user prompt
    //    when reading from a input file stream):

    string word_read;

    my_fin >> word_read;

    cout << "I just read: " << word_read << endl;

    // and, again, it is GOOD PRACTICE to CLOSE your
    //     file stream when you are done

    my_fin.close();

    return EXIT_SUCCESS;
}