diff --git a/exercises/ex3.cpp b/exercises/ex3.cpp index 025d77c..ee803ed 100644 --- a/exercises/ex3.cpp +++ b/exercises/ex3.cpp @@ -1,43 +1,31 @@ /* Could you still use a switch case here? May you can use a map. */ #include +#include using namespace std; int main() { string textInput; + map presidents; + + presidents.insert(make_pair("BarackObama","44th president of the United States")); + presidents.insert(make_pair("SandroPertini","Former President of the Italian Republic")); + presidents.insert(make_pair("NelsonMandela","Former President of South Africa")); + presidents.insert(make_pair("MahatmaGandhi","Bapu")); + presidents.insert(make_pair("DonaldKnuth","Creator of LaTeX")); + presidents.insert(make_pair("DennisRitchie","Creator of C")); cout << "Enter a famous name+surname, ex. BarackObama " << endl; cin >> textInput; - 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 + auto it = presidents.find(textInput); + if(it == presidents.end()) { cout << "Invalid input! Please enter a good name!" << endl; + return EXIT_FAILURE; } + cout << presidents[textInput] << endl; - return 0; + return EXIT_SUCCESS; } \ No newline at end of file