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
|
/*
* Chapter 11 Problem 3
*
* Create an address book problem that builds on problem 1 - this
* time, the user should be able to not just fill in a single structure
* but should be able to add new entries as he or she wants - is this easy
* to do? Is it even possible? Add the ability to display all or some of
* the entries, letting the user browse the list of entries.
*
* Define structure
* Let user add 5 entries if more required, make array bigger
* Display menu to add entry, display entries, display a single entry, quit
*
*/
#include <iostream>
#include <string>
using namespace std;
struct AddressBook
{
string name;
string address;
string phone;
};
// Function prototypes
AddressBook AddEntry(AddressBook array[], int index_entries);
void DisplayEntry(AddressBook array[], int size);
void DisplayAllEntries(AddressBook array[], int index_entries);
void DisplayRangeOfEntries(AddressBook array[], int begin, int end);
int main()
{
// Declare and intialize variables
AddressBook person[20];
int previous_count = 0;
int choice = 0;
int count = 0;
int first = 0;
int last = 0;
while (true)
{
// Display Menu
cout << "Menu:" << endl <<
'\t' << "0. Quit" << endl <<
'\t' << "1. Add an entry" << endl <<
'\t' << "2. Display last entry" << endl <<
'\t' << "3. Display all entries" << endl <<
'\t' << "4. Display a range of entries" << endl;
cout << "Please make a selection: ";
cin >> choice;
switch (choice)
{
case 0:
return 0;
case 1:
previous_count = count;
person[count] = AddEntry(person, count);
count++;
break;
case 2:
DisplayEntry(person, previous_count);
break;
case 3:
DisplayAllEntries(person, count);
break;
case 4:
cout << "Enter beginning value: ";
cin >> first;
cout << "Enter ending value: ";
cin >> last;
DisplayRangeOfEntries(person, first, last);
break;
}
}
}
AddressBook AddEntry(AddressBook array[], int index_entries)
{
cout << "Please enter your full name: ";
getline(cin, array[index_entries].name);
cout << "Please enter your address: ";
getline(cin, array[index_entries].address);
cout << "Please enter your phone number: ";
getline(cin, array[index_entries].phone);
return array[index_entries];
}
void DisplayEntry(AddressBook array[], int index_entries)
{
cout << array[index_entries].name << endl;
cout << array[index_entries].address << endl;
cout << array[index_entries].phone << endl;
}
void DisplayAllEntries(AddressBook array[], int index_entries)
{
for (int i = 0; i < index_entries; i++)
{
cout << array[i].name << endl;
cout << array[i].address << endl;
cout << array[i].phone << endl << endl;
cin.get();
}
}
void DisplayRangeOfEntries(AddressBook array[], int begin, int end)
{
for (int i = begin; i < end; i++)
{
cout << array[i].name << endl;
cout << array[i].address << endl;
cout << array[i].phone << endl;
}
}
| |