summaryrefslogtreecommitdiffstats
path: root/C++/Arrays/Files, Functions and Arrays.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'C++/Arrays/Files, Functions and Arrays.cpp')
-rw-r--r--C++/Arrays/Files, Functions and Arrays.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/C++/Arrays/Files, Functions and Arrays.cpp b/C++/Arrays/Files, Functions and Arrays.cpp
new file mode 100644
index 0000000..2ce808a
--- /dev/null
+++ b/C++/Arrays/Files, Functions and Arrays.cpp
@@ -0,0 +1,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/>.
+ */