diff --git a/exercises/ex3.cpp b/exercises/ex3.cpp index 025d77c..219f053 100644 --- a/exercises/ex3.cpp +++ b/exercises/ex3.cpp @@ -1,43 +1,37 @@ /* Could you still use a switch case here? May you can use a map. */ - #include +#include using namespace std; +void printFamous(string); int main() { - string textInput; + string textInput; cout << "Enter a famous name+surname, ex. BarackObama " << endl; cin >> textInput; + printFamous(textInput); + return 0; +} - if (textInput == "BarackObama") - { - cout << "44th president of the United States" << endl; - } - else if (textInput == "SandroPertini") - { - cout << "Former President of the Italian Republic" << endl; - } - else if (textInput == "NelsonMandela") - { - cout << "Former President of South Africa" << endl; - } - else if (textInput == "MahatmaGandhi") - { - cout << "Bapu" << endl; - } - else if (textInput == "DonaldKnuth") - { - cout << "Creator of LaTeX" << endl; - } - else if (textInput == "DennisRitchie") - { - cout << "Creator of C" << endl; - } - else +void printFamous(string textInput) +{ + map famous; + + famous["BarackObama"] = "44th president of the United States"; + famous["SandroPertini"] = "Former President of the Italian Republic"; + famous["NelsonMandela"] = "Former President of South Africa"; + famous["MahatmaGandhi"] = "Bapu"; + famous["DonaldKnuth"] = "Creator of LaTeX"; + famous["DennisRitchie"] = "Creator of C"; + + for (auto i = famous.begin(); i != famous.end(); ++i) { - cout << "Invalid input! Please enter a good name!" << endl; + if (i->first == textInput) + { + cout << i->first << " Description: " << i->second << endl; + return; + } } - - return 0; + cout << "Invalid input! Please enter a good name!" << endl; } \ No newline at end of file