Skip to content

Commit

Permalink
added code to sort words, and to write results to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
Oluwaseun Jimoh committed May 20, 2024
1 parent 41b4daa commit a336c93
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions exercises-cpp/jimoh_yusuf/ex01_basics/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,23 @@ int main(int argc, char* argv[]) {
}
std::map<std::string, int> wordCount = countWordOccurrences(inputFilePath);

if (wordCount.empty()) {
std::cerr << "Error: No valid words found in the file" << std::endl;
return 1;
}

// Sort word-count pairs in descending order of usage
std::vector<std::pair<std::string, int> > sortedWordCount(wordCount.begin(), wordCount.end());
std::sort(sortedWordCount.begin(), sortedWordCount.end(), [](const auto& a, const auto& b) {
return a.second > b.second; // Sort by count in descending order
});

const std::string outputFilePath = "results.txt";
if (!writeResultsToFile(sortedWordCount, outputFilePath)) {
return 1; // Return error code if writing to file fails
}

std::cout << "Results written to " << outputFilePath << std::endl;
return 0;
}

0 comments on commit a336c93

Please sign in to comment.