main.cpp

URL: http://147.64.242.52/~bennett/class/cmsc2040/fall2026/notes/templates/code/main.cpp
 
#include <iostream>
#include <fstream>
#include "ArrayT.h"
#include "WordCounterT.h"

using namespace std;

const string FILE_NAME{"ocap.txt"};

void FillArray(ArrayT<WordCounterT> & a);
//void PrintArray(const ArrayT<WordCounterT> & a);
void PrintArray( ArrayT<WordCounterT> param);
size_t Find(const ArrayT<WordCounterT> & ary, const WordCounterT & word);

int main() {
    
    ArrayT<WordCounterT> a;

    FillArray(a);
    PrintArray(a);

    ArrayT<WordCounterT> b{a};

    ArrayT<WordCounterT> c;
    c = a;

    //cout << "This should cause a compile error " << a.BadFun() << endl;

    return 0;
}

size_t Find(const ArrayT<WordCounterT> & ary, const WordCounterT & word){
    bool found{false};
    size_t pos{0};

    while (!found and pos < ary.Size()) {
        if (ary[pos] == word) {
           found = true;
        } else {
           ++pos;
        }

    }
    return pos;
}

//void PrintArray(const ArrayT<WordCounterT> & a) {
void PrintArray(ArrayT<WordCounterT> param) {
   for(size_t i = 0; i < param.Size(); ++i) {
      cout << i << " " << param[i] << endl;
   }
}
    
void FillArray(ArrayT<WordCounterT> & a){
    ifstream inFile{FILE_NAME};
    string word;

    if (!inFile) {
       cerr << "Unable to open " << FILE_NAME << endl;
       return;
    }

    inFile >> word;

    while (inFile) {
        WordCounterT w{word};
        //cout << "I got \"" <<  word << "\" and it turned into \"" 
        //     << w.GetWord() << "\"" << endl;
        if (w.GetWord() != "") {
             size_t pos = Find(a,w);
             if(pos == a.Size()) {
               a.push_back(w);
             } else{
                ++a[pos];
             }
        }
        inFile >> word;
    }

    inFile.close();
}