Map
#include <iostream>
#include <map>
#include <bits/stdc++.h>
using namespace std;
class State
{
string nameOfTheStates;
string popOfTheStates;
static int counter;
public:
State(); // Default Constructer
void insert(map<string, string> &);
void del(map<string, string> &);
void display(map<string, string> &);
void displayState(map<string, string> &);
};
// Function To Handle Incorrect User Input
template <typename T>
T checkInput(T Input, string content)
{
while (!cin.good())
{
cout << "Wrong Input, Please Enter it Correctly\n";
cin.clear(); // Clears The Previous Input
cin.ignore(100, '\n'); // Ignores The Incorrect Input Upto 100 Characters
cout << content;
cin >> Input;
}
while (Input < 0)
{
cout << "This Input Cannot Be Negative\n";
cin.clear();
cin.ignore(100, '\n');
cout << content;
cin >> Input;
}
return Input;
}
int State::counter = 0;
// Default Constructer Definition
State::State()
{
nameOfTheStates = "";
popOfTheStates = "";
}
void State::insert(map<string, string> &mp)
{
cout << "\nEnter The Name of State " << ++this->counter << endl;
getline(cin, nameOfTheStates);
cout << "\nEnter The Population of " << nameOfTheStates << endl;
getline(cin, popOfTheStates);
mp.insert(make_pair(nameOfTheStates, popOfTheStates));
}
void State::del(map<string, string> &mp)
{
string name;
cin.ignore();
cout << "\nEnter The Name of The State To Delete" << endl;
getline(cin, name);
mp.erase(name);
cout << "\nState Deleted\n";
}
void State::display(map<string, string> &mp)
{
map<string, string>::iterator iter;
int lastIndex = mp.size() - 1;
cout << "\n{";
for (iter = mp.begin(); iter != mp.end(); iter++)
{
if ((++iter) == mp.end())
{
iter--;
cout << "\"" << iter->first << "\""
<< " : "
<< "\"" << iter->second << "\"";
break;
}
iter--;
cout << "\"" << iter->first << "\""
<< " : "
<< "\"" << iter->second << "\""
<< ",\n";
}
cout << "}\n";
}
void State::displayState(map<string, string> &mp)
{
string name;
cout << "\nEnter The Name of State\n";
cin.ignore();
getline(cin, name);
map<string, string>::iterator iter;
for (iter = mp.begin(); iter != mp.end(); iter++)
{
if (name == (*iter).first)
{
cout << endl
<< (*iter).first << " : " << (*iter).second << endl;
return;
}
}
cout << "\nNo State of This Name\n";
}
int main()
{
map<string, string> states;
int userInput;
int num = 0;
string content = "\n\t\t1. Press 1 To Insert\n\t\t2. Press 2 To Display All States\n\t\t3. Press 3 To Display Population of State By Its Name\n\t\t4. Press 4 To Delete\n\t\t5. Press 5 To Exit\n>>>>";
string content1 = "\nEnter The Number of States\n";
State *st = NULL; // NULL Pointer
while (true)
{
cout << content;
cin >> userInput;
userInput = checkInput(userInput, content);
switch (userInput)
{
case 1:
cout << content1;
cin >> num;
num = checkInput(num, content1);
cin.ignore();
st = new (nothrow) State[num];
for (int i = 0; i < num; i++)
{
st[i].insert(states);
}
if (num > 0)
{
cout << "\nAll The Details Are Successfully Added\n";
}
break;
case 2:
if (num == 0)
{
cout << "\nList is Empty\n";
break;
}
st->display(states);
break;
case 3:
if (num == 0)
{
cout << "\nList is Empty\n";
break;
}
st->displayState(states);
break;
case 4:
if (num == 0)
{
cout << "\nList is Empty\n";
break;
}
st->del(states);
break;
case 5:
exit(0);
default:
cout << "\nPlease Enter Correct Input\n";
break;
}
}
return 0;
}
// Output
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>2
// List is Empty
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>3
// List is Empty
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>4
// List is Empty
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>1
// Enter The Number of States
// 10
// Enter The Name of State 1
// Maharashtra
// Enter The Population of Maharashtra
// 112,372,972
// Enter The Name of State 2
// Madhya Pradesh
// Enter The Population of Madhya Pradesh
// 72,597,565
// Enter The Name of State 3
// Rajasthan
// Enter The Population of Rajasthan
// 68,621,012
// Enter The Name of State 4
// Uttar Pradesh
// Enter The Population of Uttar Pradesh
// 199,812,341
// Enter The Name of State 5
// Bihar
// Enter The Population of Bihar
// 103,804,637
// Enter The Name of State 6
// Tamil Nadu
// Enter The Population of Tamil Nadu
// 72,138,958
// Enter The Name of State 7
// Karnataka
// Enter The Population of Karnataka
// 61,130,704
// Enter The Name of State 8
// Gujarat
// Enter The Population of Gujarat
// 60,383,628
// Enter The Name of State 9
// Andhra Pradesh
// Enter The Population of Andhra Pradesh
// 49,386,799
// Enter The Name of State 10
// Odisha
// Enter The Population of Odisha
// 41,947,358
// All The Details Are Successfully Added
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>2
// {"Andhra Pradesh" : "49,386,799",
// "Bihar" : "103,804,637",
// "Gujarat" : "60,383,628",
// "Karnataka" : "61,130,704",
// "Madhya Pradesh" : "72,597,565",
// "Maharashtra" : "112,372,972",
// "Odisha" : "41,947,358",
// "Rajasthan" : "68,621,012",
// "Tamil Nadu" : "72,138,958",
// "Uttar Pradesh" : "199,812,341"}
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>3
// Enter The Name of State
// Maharashtra
// Maharashtra : 112,372,972
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>3
// Enter The Name of State
// Madhya Pradesh
// Madhya Pradesh : 72,597,565
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>4
// Enter The Name of The State To Delete
// Odisha
// State Deleted
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>4
// Enter The Name of The State To Delete
// Andhra Pradesh
// State Deleted
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>2
// {"Bihar" : "103,804,637",
// "Gujarat" : "60,383,628",
// "Karnataka" : "61,130,704",
// "Madhya Pradesh" : "72,597,565",
// "Maharashtra" : "112,372,972",
// "Rajasthan" : "68,621,012",
// "Tamil Nadu" : "72,138,958",
// "Uttar Pradesh" : "199,812,341"}
// 1. Press 1 To Insert
// 2. Press 2 To Display All States
// 3. Press 3 To Display Population of State By Its Name
// 4. Press 4 To Delete
// 5. Press 5 To Exit
// >>>>5
Comments
Post a Comment