diff options
Diffstat (limited to 'num3/num3.cpp')
| -rw-r--r-- | num3/num3.cpp | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/num3/num3.cpp b/num3/num3.cpp new file mode 100644 index 0000000..d3ea934 --- /dev/null +++ b/num3/num3.cpp @@ -0,0 +1,143 @@ +//Tylr Taormina +//CST 116 +//Dec 3, 2021 + + + +#include <iostream> +#include <fstream> +#include <stdio.h> +#include <string> +#include <algorithm> + +using std::cin; +using std::cout; +using std::endl; +using std::ifstream; +using std::ofstream; +const int MAX = 10; + +int ReadData (ifstream &inFile, std::string data_arr[MAX]); +void CharCount (std::string data_arr[MAX], int length_arr[MAX], int charCount_arr[MAX]); +void FindLength(std::string data_arr[MAX], int length_arr[MAX]); + + +int main() +{ + //stand alone objects + std::string input; + int flag; + int i; + int record_counter = 0; + + //arrays + std::string data[MAX]; + int length_arr[MAX]; + int charCount_arr[MAX]; + + + //display the file choices + cout << "THE FILES WE HAVE TO CHOOSE FROM: " << endl; + cout << "===================================================" << endl; + cout << "data.txt" << endl; + cout << "data2.txt" << endl; + cout << "hello.txt\n\n" << endl; + + + cout << "Please enter a name for the file to open: "; + cin >> input; + + //modify input to avoid case sensitivity errors + transform(input.begin(), input.end(), input.begin(), ::tolower); + + ifstream inFile; + inFile.open (input); + + if ( inFile.is_open ( ) ) + { + + record_counter = ReadData (inFile, data); + inFile.close ( ); + flag = 1; + } + + else + { + cout << "Trouble Opening File: " << input << endl; + cout << "Check to make sure your spelling is correct for the file you are trying to open. Include .txt or whatever file extension is relevant." << endl; + cout << "\n\n\t\t ** About to EXIT NOW! ** "; + flag = 0; + } + + //Builds arrays. See functions below for descriptions. + FindLength(data, length_arr); + CharCount(data, length_arr, charCount_arr); + + // the flag helps to only display info if we can open the file. + if (flag == 1) + { + for (i = 0; i < record_counter; i++) + { + cout << i+1 << ") " << data[i] << " (" << charCount_arr[i] << " characters not counting spaces)" << endl; + + } + } + return 0; + +} + + +int ReadData(ifstream &inFile, std::string data_arr[MAX]) + //Reads data into an array of strings using new lines + //as the indicator to end the array. +{ + int counter = 0; + + getline(inFile, data_arr[0],'\n'); + + while ( !inFile.eof ( ) ) + { + counter++; + getline(inFile, data_arr[counter], '\n'); + } + + return counter; +} + + +void FindLength(std::string data_arr[MAX], int length_arr[]) + //Finds the length of each string in the array + //counting the number of spaces. This helps + //keep us within the scope of our array when + //counting characters in the CharCount function. +{ + int i; + + for (i = 0; i < MAX; i++) + { + length_arr[i] = data_arr[i].size(); + } + +} + + +void CharCount (std::string data_arr[MAX], int length_arr[], int charCount_arr[MAX]) + //Counts the number of characters not including spaces. If we wanted + //to include spaces, we could simply use the length array that is + //built in the FindLength function. +{ + int ctr = 0; + int i, j; + + for (i = 0; i < MAX; i++) + { + for (j = 0; j < length_arr[i]; j++) + { + if (data_arr[i][j] != ' ') + ctr++; + } + charCount_arr[i] = ctr; + ctr = 0; + } +} + |