Question

In: Computer Science

C++ ^ ^ Write a menu driven program to perform following operations using a map container...

C++
^
^
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.

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;
// display menu
void menu()
{
cout<<"1. Add Information"<<endl;
cout<<" 2. Display Information"<<endl;
cout<<"3. Update Information"<<endl;
cout<<"4. Erase Information"<<endl;
cout<<"5. Clear Information"<<endl;
  
cout<<"0. To end the program"<<endl;

  
}

int main() {
   // your code goes here
   map<int,float>mp;
   menu();
   int n;// to store the user input;
   while(1)
   {
   cin>>n;
   if(n==0)
   break;
   if(n==1)
   {
   // takes the input and stores the information in map
   int year;
   cin>>year;
   float population;
   cin>>population;
   mp.emplace(year,population);
   cout<<"Record Added: USA population in "<<year<<" is "<<population<<" Million"<<endl;
   }
   if(n==2)
   {
   int year;
   cin>>year;
   // take the year and display the population
   cout<<"USA population in "<<year<<" is "<<mp[year]<<" Million"<<endl;
  
  
   }
   if(n==3)
   {
   // take the year and population to be updated
   int year;
   cin>>year;
   float population;
   cin>>population;
   mp[year]=population;// updatin population
  
   cout<<"Record Updated: USA population in "<<year<<" is "<<mp[year]<<" Million"<<endl;
  
   }
   if(n==4)
   {
   // clear the year
   int year;
   cin>>year;
   float temp=mp[year];
   mp.erase(year);
   cout<<"Record Deleted: USA population in "<<year<<" was "<<temp<<" Million"<<endl;
  
  
   }
   if(n==5)
   {
   // clear the map
   mp.clear();
   }
   }
   return 0;
}

For input

1
2010 323.4
1
2012 326.9
1
2016 330.9
1
2020 335.8
2
2016
3
2016 333.8
4
2016
5
0

output

1. Add Information
2. Display Information
3. Update Information
4. Erase Information
5. Clear Information
0. To end the program
Record Added: USA population in 2010 is 323.4 Million
Record Added: USA population in 2012 is 326.9 Million
Record Added: USA population in 2016 is 330.9 Million
Record Added: USA population in 2020 is 335.8 Million
USA population in 2016 is 330.9 Million
Record Updated: USA population in 2016 is 333.8 Million
Record Deleted: USA population in 2016 was 333.8 Million


Related Solutions

Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
Write a menu driven Java program which uses a method for each of the following operations:...
Write a menu driven Java program which uses a method for each of the following operations: (Note : The user should be allowed to repeat the operations as long as he wants to. Use appropriate number of parameters and return type for each method.) A. to find the sum of the following series (up to N terms). The program should    display the terms:              22 + 42 + 62… For example, if N=4, then the program should display the following...
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries. Note: The program must loop until the 2nd menu option is chosen.
No Global variables No goto statement No break outside switch Write a menu driven C program...
No Global variables No goto statement No break outside switch Write a menu driven C program using functions and switch. Feel free to use “Empty Outlines” template from Canvas to design the functions as needed to build the code. Make sure to submit your work through Canvas. You can show me your code running in class when you are done. The program shows following menu to the user repetitively until user selects option 3 to exit. Circle Triangle Exit Based...
Write a  program in c++ using a map to create the following output. Here is the list...
Write a  program in c++ using a map to create the following output. Here is the list of students: 100: Tom Lee 101: Joe Jones 102: Kim Adams 103: Bob Thomas 104: Linda Lee Enter a student an ID to get a student's name: ID:  103 students[103] - Bob Thomas # of students: 5 Delete a student (Y or N)?  Y Enter ID of student to be deleted:  103 Here is the list of students after the delete: 100: Tom Lee 101: Joe Jones...
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for...
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for this game are special oriental dice. Each die has six different patterns. The six possible patterns are: Fish, Shrimp, Crab, Chicken, goldenCoin, Barrel. This game has three dice. In this game, the player can place his or her bet on any die-pattern. The amount of the winning prize is directly proportional to the number of matched dice after the two dice have been tossed...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
Write a menu-driven program to handle the flow of widgets into and out of a warehouse....
Write a menu-driven program to handle the flow of widgets into and out of a warehouse.     The warehouse will have numerous deliveries of new widgets and orders for widgets     The widgets in a filled order are billed at a profit of 50 percent over their cost     Each delivery of new widgets may have a different cost associated with it     The accountants for the firm have instituted a last-in, first-out system for filling orders         the newest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT