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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// PBachmann's help to USAHAL
// Phonebook
// 21 May 2016
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Person{ // Makes a struct person
string name; // Intializes the name
string address; // Address
string email; // email
string number; // Number
};
/* Reads myinfile and copies the contents to people[].
* This assumes contacs are written in the file like this:
*
* Carl Snow, 182 High House Rd., carrSnow@gmail.com, 846-356-3548
* David Baker, 823 Elevator Cir., Baker221@yahoo.com, 655-789-1673
* Sapphire Peterson, 224 Apex Dr., SPeterson237@nc.rr.com, 901-779-9777
*
* etc. etc.
*/
void initializeVector(ifstream &myinfile, vector<Person> &people){
Person person1; // Used to temporarily store contact info
string space; // Used to store the extra space
if (myinfile.is_open()){ // Opens file
while (myinfile.good()){ // Checks for various things. For info look up "ios::good"
getline(myinfile, person1.name, ','); // Reads until a comma
getline(myinfile, space, ' '); // Reads the extra space and ignores it
getline(myinfile, person1.address, ','); // Reads until the next comma
getline(myinfile, space, ' '); // Reads the extra space and ignores it
getline(myinfile, person1.email, ','); // Reads until the next comma
getline(myinfile, space, ' '); // Reads the extra space and ignores it
getline(myinfile, person1.number, '\n'); // Reads until the end of the line
people.push_back(person1); // Adds the contact to people[]
}
}
else{ // For when myinfile.is_open returns an error
cout << "Unable to open.\n";
}
}
// Main screen choices
void input(int &choice){
cout << "Welcome to the phonebook." << endl;
cout << "1. Show contacts" << endl;
cout << "2. Add contact" << endl;
cout << "3. Save contacts and quit" << endl;
//cout << "4. Search" << endl;
cin >> choice;
cout << endl;
}
// Prints the contacts in a table
void showContacts(vector<Person> &people){
cout << " Name Address email Number" << endl;
for(unsigned i = 0; i < people.size(); i++){
cout << people[i].name << " ";
cout << people[i].address << " ";
cout << people[i].email << " ";
cout << people[i].number << endl;
}
cout << endl;
}
// Asks user to enter in a new contact
void addContact(ofstream &myoutfile, vector<Person> &people){
Person person1; // Used to temporarily store contact info
cin.ignore(); // Without this line, the user's menu choice number will be read as the contact name
cout << "Enter name:" << endl;
getline(cin, person1.name);
// Using getline allows you to enter a first name and last name (space included)
cout << "Enter address:" << endl;
getline(cin, person1.address);
cout << "Enter email:" << endl;
getline(cin, person1.email);
cout << "Enter number:" << endl;
getline(cin, person1.number);
people.push_back(person1); // Adds person1 to the end of people[]
cout << endl;
}
// Saves current contacts in Phonebook.txt
void saveContacts(ofstream &myoutfile, vector<Person> &people){
if (myoutfile.is_open()){ // Opens file
for(unsigned i = 0; i < people.size(); i++){
if (i > 0) myoutfile << endl; // Starts by writing a line return
myoutfile << people[i].name << ", "; // Writes name then a comma
myoutfile << people[i].address << ", "; // Writes address then a comma
myoutfile << people[i].email << ", "; // Writes email then a commae
myoutfile << people[i].number; // Writes number
}
cout << "Contacts saved.\n";
}
else{ // For when myoutfile.is_open returns an error
cout << "Unable to open.\n";
}
cout << endl;
}
int main(){
int choice; // The user's menu choice
vector<Person> people; // Makes a Person-type vector called people
ifstream myinfile("Contacts.txt"); // Input file
ofstream myoutfile("Phonebook.txt"); // Outputfile
initializeVector(myinfile, people); // Reads Contacts.txt and stores it in person[]
do{ //repeats the loop until user inputs 3
input(choice); // Menu
switch(choice){ // switch is a version of if/then statements. Look up "switch" for more info.
case 1: showContacts(people); break; // Show contacts
case 2: addContact(myoutfile, people); break; // Adds new contact to people[]
case 3: saveContacts(myoutfile, people); break; // Saves contacts to Phonebook.txt
default: break;
}
if (choice < 1 || choice > 3){
cout << "Invalid input.\n\n";
}
} while(choice != 3);
return 0;
}
|