You could also use correct C++ code to do the task.
With modern C++ elements and algorithms.
And, especially, by really using all iostream facilities.
One example could be:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <limits>
struct TermDefinition {
// The data
std::string term{};
std::string definition{};
// Override extractor operator
friend std::istream& operator >> (std::istream& is, TermDefinition& td) {
std::getline(std::getline(is, td.term, ';') >> std::ws, td.definition);
return is;
}
// Overwrite inserter operator
friend std::ostream& operator << (std::ostream& os, const TermDefinition& td) {
return os << td.term << " - " << td.definition;
}
};
int main() {
// Open file and check, if it could be opened
if (std::ifstream dictionaryStream{ "dictionary.txt" }; dictionaryStream) {
// Read complete file. Split it into components. Add to vector
std::vector dictionary(std::istream_iterator<TermDefinition>(dictionaryStream), {});
// As long as we should run the program, do the loop
bool runProgram{ true };
while (runProgram) {
// Get teh election for the user. Check for errors in input
std::cout << "
Menu. Please select
1 -> Starting
2 -> Containing
3 -> Ending
4 -> Stop
Please enter 1 or 2 or 3 or 4: ";
if (unsigned int selection{}; std::cin >> selection) {
// Depending on selction gieven by user
switch (selection) {
case 1:
// Give instruction to user
std::cout << "
Searching by first letter. Enter letter: ";
// Get one letter from user and check for valid input
if (std::string letter{}; std::getline(std::cin >> std::ws, letter) && letter.length() == 1)
// Copy all dictionary entries fullfilling the search criteria to std::cout
std::copy_if(dictionary.begin(), dictionary.end(), std::ostream_iterator<TermDefinition>(std::cout, "
"),
[&letter](const TermDefinition& td) { return *td.term.begin() == letter[0]; });
break;
case 2:
// Give instruction to user
std::cout << "
Searching by any letter. Enter letter: ";
// Get one letter from user and check for valid input
if (std::string letter{}; std::getline(std::cin >> std::ws, letter) && letter.length() == 1)
// Copy all dictionary entries fullfilling the search criteria to std::cout
std::copy_if(dictionary.begin(), dictionary.end(), std::ostream_iterator<TermDefinition>(std::cout, "
"),
[&letter](const TermDefinition& td) { return td.term.find(letter[0]) != std::string::npos; });
break;
case 3:
// Give instruction to user
std::cout << "
Searching by last letter. Enter letter: ";
// Get one letter from user and check for valid input
if (std::string letter{}; std::getline(std::cin >> std::ws, letter) && letter.length() == 1)
// Copy all dictionary entries fullfilling the search criteria to std::cout
std::copy_if(dictionary.begin(), dictionary.end(), std::ostream_iterator<TermDefinition>(std::cout, "
"),
[&letter](const TermDefinition& td) { return td.term.back() == letter[0]; });
break;
case 4:
// User wants to stop the program
// Set loop flag to 0
runProgram = false;
std::cout << "
Done. Exiting program . . .
";
break;
default:
std::cout << "
Invalid number. Please try again
";
break;
}
}
else {
std::cout << "
Error: Invalid input. Please try again
";
// Reset bad input state an consume nonesense characters
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '
');
}
}
}
else std::cerr << "
Error: Could not open input file!
";
return 0;
}
You already accepted the other answer. So, my guess is that you do no read this and therefore do not need further explanations. Otherwise, please comment and I will add explanations.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…