Question

In: Computer Science

C++ 14.11 Lab # Map Write a menu driven program to perform following operations using a...

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.

Solutions

Expert Solution


// 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:


Related Solutions

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,...
Write a C++ program to run a menu driven program with the following choices: 1) Display...
Write a C++ program to run a menu driven program with the following choices: 1) Display the grades 2) Adjust grade 3) Display Average for each student 4) Display number of student with at least a B 5) Quit requirements: 1. Write a function called getValidGrade that allows a user to enter in an integer and loops until a valid number that is >= 0 and <= 100 is entered. It returns the valid value. 2. Write a function called...
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...
The Menu-driven Program: Write a menu-driven program to implement a roomsboard for a classroom reservations system....
The Menu-driven Program: Write a menu-driven program to implement a roomsboard for a classroom reservations system. The menu includes the following options: Add new room. The program will prompt the user to enter the roomID, courseID and time of the new reserved room, then will call the add() method from the RoomsBoard class. Remove all occurrences of a room. The program will prompt the user to input the room ID to be removed, then call the remove() method from the...
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 c++ program to perform the following operations on stack of Integers (Array Implementation of...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of Stack with maximum size MAX) (i) Push an Element on to stack (ii) Pop an Element from stack (iii) Demonstrate how stack can be used to check Palindrome (iv) Display the status of stack (v) Exit
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT