I'm having some problems with a program that I'm writing where you find the count of words in a file. The issue I'm having is with my function "get_word_counts" in my cpp. I'm continually getting an message in Visual Studio that states "error C4716: 'TextCounter::get_word_counts': must return a value", despite the fact that I do, in fact, return a value at the end of the function.
Can someone help me understand what the issue with this is? I have searched everywhere but I can't seem to figure out exactly what the problem is. Perhaps it's something simple, but I just can't see it.
I'll post below my cpp file as well as the header:
#include"TextCounter.h"
#include <string>
#include <iostream>
#include <vector>
/*
Constructor takes in the filename and builds the map.
*/
TextCounter::TextCounter(std::string file):
filename(file) {
// Build the input list.
parse_file();
}
/*
Get the count of each word in the document.
If a word doesn't occur in the document, put 0.
*/
std::vector<int> TextCounter::get_word_counts(const std::vector<std::string>& words) {
// TODO: Finish this method.
std::vector<int> result;
std::unordered_map<std::string, int>::iterator iter;
for (autoconst &i : words) {
iter = TextCounter::frequency.find(i);
if (iter == frequency.end()) {
result.push_back(0);
}
else {
result.push_back(iter->second);
}
}
return result;
}
// Add a word to the map.
// Check to see if the word exists, if so, increment
// otherwise create a new entry and set it to 1.
void TextCounter::add_word(std::string word) {
// COMP: finished this method.
//Look if it's already there.
if (frequency.find(word) == frequency.end()) // Then we've encountered the word for a first time.
frequency[word] = 1; // Initialize it to 1.
else // Then we've already seen it before..
frequency[word]++; // Increment it.
}
// Parse an input file.
// Return -1 if there is an error.
int TextCounter::parse_file() {
// Local variables.
std::ifstream inputfile; // ifstream for reading from input file.
// Open the filename specified for input.
inputfile.open (filename);
// Tokenize the input.
// Read one character at a time.
// If the character is not in a-z or A-Z, terminate current string.
char c;
char curr_str[MAX_STRING_LEN];
int str_i = 0; // Index into curr_str.
bool flush_it = false; // Whether we have a complete string to flush.
while (inputfile.good()) {
// Read one character, convert it to lowercase.
inputfile.get(c);
c = tolower(c);
if (c >= 'a' && c <= 'z') {
// c is a letter.
curr_str[str_i] = c;
str_i++;
// Check over-length string.
if (str_i >= MAX_STRING_LEN) {
flush_it = true;
}
} else {
// c is not a letter.
// Create a new string if curr_str is non-empty.
if (str_i>0) {
flush_it = true;
}
}
if (flush_it) {
// Create the new string from curr_str.
std::string the_str(curr_str,str_i);
// std::cout << the_str << std::endl;
// COMP: Insert code to handle new entries or increment an existing entry.
TextCounter::add_word(the_str);
// Reset state variables.
str_i = 0;
flush_it = false;
}
}
// Close input file.
inputfile.close();
return 0;
}
#include <fstream>
#include <unordered_map>
#include <string>
#include <vector>
#define MAX_STRING_LEN 256
#define DICT_SIZE 20000
class TextCounter {
public:
explicit TextCounter(std::string file = "");
// Get the counts of a vector of words.
std::vector<int> get_word_counts(const std::vector<std::string>& words);
private:
// Name of the input file.
std::string filename;
// COMP: Implement a data structure to keep track of each word and the
// number of times that word occurs in the document.
std::unordered_map<std::string, int> frequency;
// Parse an input file.
int parse_file();
// Add a word to the map.
void add_word(std::string word);
};