Skip to content

Commit

Permalink
completed implementation of word counter and tested successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
Oluwaseun Jimoh committed May 20, 2024
1 parent a336c93 commit 99ef029
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions exercises-cpp/jimoh_yusuf/ex01_basics/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ std::map<std::string, int> countWordOccurrences(const std::string& inputFilePath
// create a map with integer keys and string values
std::map<std::string, int> wordCount;

if (!openFile.is_open()) {
std::cerr << "Error: Unable to open the input file." << std::endl;
// check the file for errors
if(!openFile.good()) {
std::cout << "Error: Unable to open the path of the input file." << inputFilePath << std::endl;
return wordCount; // Return empty map if file cannot be opened
}

Expand Down Expand Up @@ -62,6 +63,7 @@ bool writeResultsToFile(const std::vector<std::pair<std::string, int>>& wordCoun
outputFile << "----------------" << std::endl;

for (const auto& pair : wordCount) {
std::cout << pair.first << "\t" << pair.second << std::endl;
outputFile << pair.first << "\t" << pair.second << std::endl;
}

Expand All @@ -77,13 +79,6 @@ int main(int argc, char* argv[]) {

const std::string inputFilePath = argv[1];

std::ifstream openFile(inputFilePath);

// check the file for errors
if(!openFile.good()) {
std::cout << "Error: Unable to open the path of the input file." << inputFilePath << std::endl;
return 1;
}
std::map<std::string, int> wordCount = countWordOccurrences(inputFilePath);

if (wordCount.empty()) {
Expand Down

0 comments on commit 99ef029

Please sign in to comment.