Skip to content

Using map in the exercises number 3 #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions exercises/ex3.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
/* Could you still use a switch case here? May you can use a map. */

#include <iostream>
#include <map>
using namespace std;

int main()
{
map<string, string> famous;
string textInput;
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";

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")
bool flag = true;
for (auto i = famous.begin(); i != famous.end(); ++i)
{
cout << "Creator of C" << endl;
if (i->first == textInput)
{
cout << i->first << " Description: " << i->second << endl;
flag = false;
break;
}
}
else
if (flag != false)
{
cout << "Invalid input! Please enter a good name!" << endl;
}
Expand Down