From ec6aa923fed0d4494bcf223124d0f8ccacf9d524 Mon Sep 17 00:00:00 2001 From: Daniele Calabrese Date: Thu, 30 Oct 2025 18:33:03 +0100 Subject: [PATCH] =?UTF-8?q?Lo=20switch=20non=20si=20pu=C3=B2=20usare,=20us?= =?UTF-8?q?o=20map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/ex3.cpp | 59 +++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/exercises/ex3.cpp b/exercises/ex3.cpp index 025d77c..af97e05 100644 --- a/exercises/ex3.cpp +++ b/exercises/ex3.cpp @@ -1,43 +1,32 @@ -/* Could you still use a switch case here? May you can use a map. */ - #include +#include +#include using namespace std; int main() { - string textInput; + string textInput; + + cout << "Inserisci un nome famoso (nome+cognome) "; + cin >> textInput; - cout << "Enter a famous name+surname, ex. BarackObama " << endl; - cin >> textInput; + map famousPeople = { + {"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"} + }; - 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; - } + if (famousPeople.find(textInput) != famousPeople.end()) + { + cout << famousPeople[textInput] << endl; + } + else + { + cout << "Nome non valido! inserisci un nome valido" << endl; + } - return 0; -} \ No newline at end of file + return 0; +}