From 78c2536a0ae5096b7c34afff3e179c907ad0cca7 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Tue, 4 Nov 2025 14:45:14 +0100 Subject: [PATCH 1/2] refactor: improve map --- exercises/ex3.cpp | 40 ++++++++++++---------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/exercises/ex3.cpp b/exercises/ex3.cpp index 025d77c8..1227930c 100644 --- a/exercises/ex3.cpp +++ b/exercises/ex3.cpp @@ -1,43 +1,27 @@ /* Could you still use a switch case here? May you can use a map. */ #include +#include using namespace std; int main() { string textInput; + map famDes = { + {"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"} }; + 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 - { - cout << "Invalid input! Please enter a good name!" << endl; - } + auto temp = famDes.find(textInput); + if(temp != famDes.end()) cout << famDes[textInput]; + else cout << "Invalid input! Please enter a good name!" << endl; return 0; } \ No newline at end of file From bbda40e0fa660abf65599efeb692d8adea6efe29 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Wed, 5 Nov 2025 18:02:37 +0100 Subject: [PATCH 2/2] style(ex3.cpp): remove whitespaces --- exercises/ex3.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/exercises/ex3.cpp b/exercises/ex3.cpp index 1227930c..9f12955e 100644 --- a/exercises/ex3.cpp +++ b/exercises/ex3.cpp @@ -8,13 +8,13 @@ int main() { string textInput; - map famDes = { - {"BarackObama", "44th president of the United States"}, - {"SandroPertini", "Former President of the Italian Republic"}, - {"NelsonMandela", "Former President of South Africa"}, + map famDes = { + {"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"} }; + {"DennisRitchie", "Creator of C"}}; cout << "Enter a famous name+surname, ex. BarackObama " << endl; cin >> textInput;