summaryrefslogtreecommitdiffstats
path: root/C++/Arrays/Files, Functions and Arrays.cpp
blob: 2ce808ae16cee5c6c01796b455eefe995d7630f5 (plain) (blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Name: msglm
// Date: August 25th 2022
// Program Name: Files, Functions and Arrays
// Description: Create a program that reads in an unknown number of ATU class names (such as Programming 2) from an input file (see explanation of unknown number below). Then, output those class names to both the screen and an output file - output once in the order in which the data was read in from the file and again in reverse order.



#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Named constants
const int ARR_SIZE = 50; //Personally, i'd pass this as an env var or a flag


//Global Variable declaration
//
//Usually this is bad practice, but the function requirement for this has forced me to partake in this
//I could have done some mess with passing by reference, but every function was assumed to share the same data
//(I wrote it all in main first and then fragmented it), so this is the best way I could find to reach requirement
//
//Something like this should never reach production ever.

string tmpArr[ARR_SIZE]; //Probably could pass through the file first to get the size first and declare this, but that's past this program's scope.
int epoch {}; //both counts up and is the length of the array.  

//Vars necessary to invert an array
int start {};
string tmp;
int arrEnd {}; //Conflicts with namespace, hence the name

//It's a shame this isn't STDIN and STDOUT
ifstream inFile;
ofstream outFile;


//Functions are pretty unncessary for this code and overcomplicate more than they help
//The original code for this before I split it into 5 functions was a cute little 40 SLOC thing.

bool fileFound(string location) {
    ifstream testFile;
    testFile.open(location);

    if (testFile) {
        return true;
    } else {
        return false;
    }
}

void orderArray() {
    //Read the line, put it at the epoch in the array, also check if EOF. if EOF, abort.
    while(getline(inFile, tmpArr[epoch]) && inFile.peek() != EOF) {  
        epoch = epoch + 1;
    }

}

void displayNormalOrderArray() {
    int i = 0;
    while(i<epoch){
        cout << tmpArr[i] << endl;
        outFile << tmpArr[i] << endl;
        i++;
    }

}

void reverseOrderArray() {
    //the skip of the last digit is purposeful, the other loop covers it
    for(int i=epoch;i>=0;i--) { 
        cout << tmpArr[i] << endl;
        outFile << tmpArr[i] << endl;
    }
}

void closeFiles() {
    inFile.close();
    outFile.close();
}

int main() {

    //Var assignments

    // User input
    if (fileFound("in.txt")) {

        inFile.open("in.txt");
        orderArray();
        outFile.open("out.txt");
        displayNormalOrderArray();
        reverseOrderArray();
        closeFiles();

        return 0;

    } else {
        cout << "ERROR. Input File doesn't exist!";
        return 1;

    }
}
/*
   This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License along with this program.  If not, see <https://www.gnu.org/licenses/>.
   */