-1
#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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Side note: iostream, fstream, string, and vector are headers, not libraries. – user4581301 Jan 17 '23 at 18:37
  • 2
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Eljay Jan 17 '23 at 18:37
  • 1
    what is inside the file? You do not check if the file opened sucessfully. If it didnt your code will just not print anything on the console – 463035818_is_not_an_ai Jan 17 '23 at 18:37
  • 1
    This would be a great time to learn how to use a debugger and/or an IDE, then you can step through your program line by line as it is executing, and inspect the values of your variables to confirm if it behaves according to your expectations – Cory Kramer Jan 17 '23 at 18:38
  • Note: Regular indentation makes a lot of bugs impossible and many more obvious. Right now it looks like you close the file at the wrong spot, but when the indentation is sorted out, it is merely redundant. The `ifstream` destructor closes the file for you. – user4581301 Jan 17 '23 at 18:39
  • You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Jan 17 '23 at 18:40
  • *You can only use the iostream, fstream, string, and vector libraries* -- In those instructions, were you told what *would* be the better way to solve this? It doesn't make sense (in terms of learning things) if you're never told the way this problem would be solved in the "real world". – PaulMcKenzie Jan 17 '23 at 18:43
  • 2
    Side note: Rather than two parallel arrays consider making a structure containing the string and the count of occurrences for that string and having a single `vector` of that structure. it reduces the book-keeping a little and makes sure you can never have an indexing mismatch between the string and its count. – user4581301 Jan 17 '23 at 18:45
  • Side note: `isFound()` should return the found index instead of a `bool`, then you don't have to search the vector again with `std::find()` afterwards. Otherwise, just use `std::find()` by itself and get rid of `isFound()` altogether. You are doing twice the work than you should be. – Remy Lebeau Jan 17 '23 at 18:53
  • 2
    @OP -- FYI -- using a `std::map` or `std::unordered` would be the better choice of solving this problem. Note that you have to rescan the entire vector for each word you find. A map would do a fast lookup to check if the word exists, regardless of the number of words currently found. – PaulMcKenzie Jan 17 '23 at 18:55

1 Answers1

2

In printReport

i > words.size();

should be

i < words.size();

That's enough to explain the lack of output, but you should also be checking that the file opens successfully, that would also be a possible explanation.

And as is said in the comments, the sooner you learn how to use a debugger, so you can find simple errors like this for yourself, the better.

john
  • 85,011
  • 4
  • 57
  • 81