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
|
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;
//constant number of friends
const int numberOfFriends = 100;
//struct
struct addressType
{
string firstName;
string lastName;
string address;
string phone;
addressType *link;
};
//function prototype
void addressFunction(addressType& first, int friends);
//void write_addressBook(addressType& second, int fullIndex);
void WriteAddressBook(addressType* head);//the argument is the head of list;
void InsertList(addressType*& head,addressType& newnode);
int main(int argc, char* argv[]){
addressType *head, *newNode;
head = nullptr;
newNode = nullptr;
//variables
char addAddress;
int friendCounter = 0;
int exitLoop = 0;
cout << "Welcome to Address Book.\n " << endl;
do{
cout << "Do you want to add a friend? (Y or N): ";
cin >> addAddress;
cout << endl;
if (addAddress == 'N' || addAddress == 'n'){
cout << "Thank you for using Address Book" << endl;
exitLoop = 1;
}
else{
newNode=new addressType;
addressFunction(*newNode, friendCounter);
InsertList(head,*newNode);//insert the new node in the list
friendCounter++;
}
} while ((exitLoop == 0) && (friendCounter < numberOfFriends));
if (friendCounter != 0){
//write_addressBook(*newNode, friendCounter);
WriteAddressBook(head);
cout << "Thank you for choosing Address Book.";
}
for(addressType* temp=head;temp!=nullptr;)//free memory
{
head=head->link;
delete temp;
temp=head;
}
system("pause");
return 0;
}
//Function definition
void addressFunction(addressType& first, int friends){
cin.ignore(100, '\n');
cout << "Please enter the first name of the person you";
cout << " wish to add to your Address Book: " << endl;
getline(cin, first.firstName);//first's data type is struct not struct pointer
cout << endl;
cout << "Please enter the last name of the person ";
cout << "you wish to add to your Address Book : " << endl;
getline(cin, first.lastName);
cout << endl;
cout << "Please enter " << first.firstName << " " << first.lastName << "'s address.";
getline(cin, first.address);
cout << endl;
cout << "Please enter " << first.firstName << " " << first.lastName << "'s phone number.";
getline(cin, first.phone);
cout << endl;
}
//void write_addressBook(addressType &second, int fullIndex)//omit &,should use reference
void WriteAddressBook(addressType* head)
{
ofstream outfile;
string fileName;
cout << "Please enter a file name to store your address book: ";
cin >> fileName;
cout << endl;
outfile.open(fileName.c_str());
if (outfile.is_open()) {
/*for (int i = 0; i < fullIndex; i++){
outfile << second->firstName << "," << second->lastName << endl;
outfile << second->address << endl;
outfile << second->phone << endl << endl;
}*/
for(;head!=nullptr;head=head->link)
{
outfile <<"name:\t"<< head->firstName << "," << head->lastName << endl;
outfile <<"address:\t"<< head->address << endl;
outfile << "phone:\t"<<head->phone << endl << endl;
}
outfile.close();
}
else{
cout << fileName << " did not open.\n\n";
}
}
void InsertList(addressType*& head,addressType& node)
{
node.link=head;
head=&node;
}
|