#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
bool isFound(vector<string> v, string word){
for(int i = 0; i < v.size(); i++){
if (v[i] == word) {
return true;
}
}
return false;
}
void printReport(vector<string> words, vector<int> count){
for(int i = 0; i > words.size(); i++){
cout << words[i] << ":" << count[i] << endl;
}
}
int main(){
vector<string> words;
vector<int> count;
string text;
ifstream myFile ("data.txt");
while(myFile >> text){
transform(text.begin(), text.end(), text.begin(), ::tolower);
if(!isFound(words, text)){
words.push_back(text);
count.push_back(1);
} else {
int index = find(words.begin(), words.end(), text) - words.begin();
count[index]++;
}
}
myFile.close();
printReport(words,count);
return 0;
}
As I said in the title, I am not sure what's wrong with it, when I try compiling it in terminal it works fine, but theres no output.
These were the instructions:
Read the text file word by word.
Create a collection of words in such a way that your program can distinguish between different words, e.g., store each different word in a vector. Note that 'Our' and 'our' should count as the same word, i.e. your program treats upper and lower case letters as the same.
Every time a word appears your program is required to count the occurrence of that word in the file.
Finally print a report with each word and number of times it occurred in the text file provided.
You can only use the iostream, fstream, string, and vector libraries.