diff --git a/exercises/ex3.cpp b/exercises/ex3.cpp index 025d77c8..aca95dd0 100644 --- a/exercises/ex3.cpp +++ b/exercises/ex3.cpp @@ -1,43 +1,39 @@ /* Could you still use a switch case here? May you can use a map. */ #include +#include using namespace std; +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 + + +unordered_map initialize_peopleMap(){ + unordered_map peopleMap = { + {"BarackObama","44th president of the United States"}, + {"SandroPertini","Former President of the Italian Republic"}, + {"NelsonMandela","Former President of South Africa"}, + {"MahatmaGandhi","Bapu"}, + {"DonaldKnuth","Creator of LaTeX"}, + {"DennisRitchie","Creator of C"} + }; + return peopleMap; +} + int main() { string textInput; + unordered_map peopleMap = initialize_peopleMap(); 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 - { + if(peopleMap.count(textInput) > 0){ //checks if there is a name like textInput in the peopleMap + cout << peopleMap[textInput] << endl; + } else { cout << "Invalid input! Please enter a good name!" << endl; + return EXIT_FAILURE; } - return 0; + return EXIT_SUCCESS; } \ No newline at end of file