aboutsummaryrefslogtreecommitdiff
path: root/num3/num3.cpp
blob: 8719ec77fb800bb506aec070491cf3b807381a9d (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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]; //holds strings.
    int length_arr[MAX]; //keeps track of full length of strings. Counts characters including spaces.
    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;
    }

    //Build 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;
    }
}