1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <vector>
#include <sstream>
class manager{ //Contacts manager class.
public: //Handles storage, adding contacts, etc.
manager::manager(std::string targetFile);
std::vector< std::vector<std::string> >
contacts;//Vector of string vectors.
std::string file;
void viewAll();
void add(std::string name,
std::string number,
std::string address);
void askAdd();
};
manager::manager(std::string targetFile){
file = targetFile;//targetFile for persistent storage.
};
void manager::viewAll(){//Displays all contacts.
std::system("cls");
int size = contacts.size();
for (int i=0; i < size; i++){
std::vector<std::string> contact;
contact = contacts.at(i);
std::printf("%s \n", "=========================");
std::cout << "NAME::" << contact.at(0) << "\n";
std::cout << "NUMBER::" << contact.at(1) << "\n";
std::cout << "ADDRESS::" << contact.at(2) << "\n";
std::printf("%s \n", "=========================");
}
};
void manager::add(std::string name, //Add contact.
std::string number,
std::string address){
std::string oldName;
int size = contacts.size();
for (int i; i < size; i++){//Verify the name doesn't
oldName = contacts.at(i).at(0);//exist.
if (oldName == name){
contacts.erase(contacts.begin()+i);
}
}
std::vector<std::string> newContact;
newContact.push_back(name);
newContact.push_back(number);
newContact.push_back(address);
contacts.push_back(newContact);
};
void manager::askAdd(){ //Ask user to add new contact.
std::string name, number, address;
std::system("cls");
std::printf("%s \n","Adding new contact... 99 to end.");
std::printf("%s \n","Enter NAME: ");
std::getline(std::cin, name, '\n');
if (name == "99"){return;}
std::printf("%s \n","Enter NUMBER: ");
std::getline(std::cin, number, '\n');
if (name == "99"){return;}
std::printf("%s \n","Enter ADDRESS: ");
std::getline(std::cin, address, '\n');
if (name == "99"){return;}
add(name, number, address);
};
// END CLASS manager.........
void dump(){} // pass, do nothing, etc.
void printMenu(){ //Display main menu.
std::printf("%s \n","<<< Simple Contact Directory >>>");
std::printf("%s \n","Select an option by number:");
std::printf("%s \n","1) Add new contact.");
std::printf("%s \n","2) View all.");
std::printf("%s \n","3) Search names.");
std::printf("%s \n","4) Remove contact.");
std::printf("%s \n","5) Save / Load.");
std::printf("%s", "Enter desired menu number... ");
}
int main(){
std::vector<std::string> contacts;
bool mainloop = true;
int choice;
manager lib("storagefile.txt");
while (mainloop){
printMenu();
std::cin >> choice;
switch (choice){
case 1: lib.askAdd();
case 2: lib.viewAll();
case 3: dump();
case 4: dump();
case 5: dump();
}
std::system("cls");
}
}
|