In: Computer Science
C++ 14.11 Lab # Map
Write a menu driven program to perform following operations
using a map container that stores the information
about USA Population (In Million).
@@@Menu@@@
1. Add Information
2. Display Information
3. Update Information
4. Erase Information
5. Clear Information
For example, Suppose the map initially contains following
information (Use emplace() function to store the
information)
2010, 309.33
2011, 311.58
2012, 313.87
2015, 320.74
2016, 323.07
The program should produce the desired output when a valid
input is provided, as follows:
1. If the Input is:
1
2018
329.16
The output should be:
Record Added: USA population in 2018 is 329.16 Million.
2. If the Input is:
2
2015
The output should be:
USA population in 2015 is 320.75 Million.
3. If the Input is:
3
2015
321.88
The output should be:
Record Updated: USA population in 2015 is 321.88 Million.
4. If the Input is:
4
2015
The output should be:
Record Deleted: USA population in 2015 was 321.88.
5. If the Input is:
5
The output should be:
All records Deleted.
// C++ program to create a menu driven program to perform following
operations using a map container
// that stores the information about USA Population (In
Million).
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int,double> year_population; // create
the map container
// populate records using emplace function
year_population.emplace(2010,309.33);
year_population.emplace(2011,311.58);
year_population.emplace(2012,313.87);
year_population.emplace(2015,320.74);
year_population.emplace(2016,323.07);
int choice;
int year;
double population;
// loop that continues till the user wants
do
{
//display the menu
cout<<endl<<"@@@Menu@@@"<<endl;
cout<<"1. Add
Information"<<endl<<"2. Display
Information"<<endl<<"3. Update
Information"<<endl<<"4. Erase
Information"<<endl<<"5. Clear
Information"<<endl
<<"6. Exit"<<endl;
cout<<"Enter you choice(1-6)
: ";
cin>>choice; // input of menu
choice
// perform the operation based
on the choice selected by the user
if(choice == 1)
{
cout<<"Enter new year : ";
cin>>year;
cout<<"Enter population : ";
cin>>population;
map<int,double>::iterator it =
year_population.find(year);
if(it ==
year_population.end())
{
year_population[year] = population;
cout<<"Record Added: USA population in
"<<year<<" is "<<population<<"
Million."<<endl;
}else
cout<<"USA population for
"<<year<<" exists. Try updating the
population"<<endl;
}else if(choice == 2)
{
cout<<"Enter year : ";
cin>>year;
map<int,double>::iterator it =
year_population.find(year);
if(it !=
year_population.end())
cout<<"USA population in
"<<year<<" is "<<it->second<<"
Million"<<endl;
else
cout<<"USA population for
"<<year<<" not present"<<endl;
}else if(choice == 3)
{
cout<<"Enter existing year : ";
cin>>year;
cout<<"Enter new population : ";
cin>>population;
map<int,double>::iterator it =
year_population.find(year);
if(it !=
year_population.end())
{
year_population[year] = population;
cout<<"Record Updated: USA population in
"<<year<<" is "<<population<<" Million.
"<<endl;
}else
cout<<"USA population for
"<<year<<" not present. Try adding a new
information"<<endl;
}else if(choice == 4)
{
cout<<"Enter year: ";
cin>>year;
map<int,double>::iterator it =
year_population.find(year);
if(it !=
year_population.end())
{
cout<<"Record Deleted: USA population in
"<<year<<" was "<<it->second<<"
Million."<<endl;
year_population.erase(it);
}else
cout<<"USA population for
"<<year<<" not present"<<endl;
}else if(choice == 5)
{
year_population.clear();
cout<<"All
records Deleted"<<endl;
}else if(choice != 6)
cout<<"Invalid choice"<<endl;
}while(choice != 6);
return 0;
}
//end of program
Output: