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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int SIZE = 5;
//structure
struct infoType
{
string fname;
string lname;
string streetAdd;
string cityStateZ;
string phone;
};
// Function to get info from file.
void readData(ifstream &fin, infoType tele[], int& size)
{
int i = 0;
while(!fin.eof())
{
fin >> tele[i].fname;
fin >> tele[i].lname;
fin.get();
getline(fin,tele[i].streetAdd, '\n');
getline(fin,tele[i].cityStateZ, '\n');
getline(fin,tele[i].phone, '\n');
i++;
}
size = i;
}
//Function to display Menu
void displayMenu()
{
cout << "Phone Directory Program " << endl;
cout << endl;
cout << "Options" << endl;
cout << "[A]dd and entry to the phone directory. " << endl;
cout << "[D]elete an entry from the phone directory. " << endl;
cout << "[U]pdate an entry from the phone directory. " << endl;
cout << "[L]ist the entire phone directory. " << endl;
cout << "[E]xit the menu" << endl;
}
//Option A function
int optionA(ifstream &fin, infoType tele[], int& size)
{
// Still need formatting
cout << "----You have selected option A---- " << endl;
cout << endl;
cout << "Please enter the first name. " << endl;
for(int i = 0; i < 1; i++)
{
cin >> tele[i].fname;
}
cout << "Please enter the last name. " << endl;
for(int i= 0; i < 1; i++)
{
cin >> tele[i].lname;
}
cout << "Please enter the street address. " << endl;
cin.get();
for(int i = 0; i < 1; i++)
{
getline(cin,tele[i].streetAdd);
}
cout << "Please enter the city, state and zip code." << endl;
for(int i = 0; i < 1; i++)
{
getline(cin,tele[i].cityStateZ);
}
cout << "Please enter the phone number.(Example, 555-555-5555)" << endl;
for(int i = 0; i < 1; i++)
{
cin >>tele[i].phone;
}
size++;
return size;
}
//Remove function
int remove( infoType tele[], int& size, int position_of_item_to_remove )
{
for( int i = position_of_item_to_remove + 1 ; i < size ; ++i )
tele[i] = tele[i-1] ;
return --size;
}
//Option D function
void optionD( ifstream &fin, infoType tele[], int& size)
{
int index = 0;
string dPhone;
bool found = false;
int nCount = 0;
cout << "You have selected option D " << endl;
cout << "Please enter the phone number of the record to be deleted." << endl;
cout << "(Example, 555-555-5555)" << endl;
cin >> dPhone;
while( index < size && !found )
{
if(tele[index].phone.compare(dPhone)== 0)
{
cout << "The entry to be deleted is " << endl;
cout << tele[index].lname << endl;
cout << tele[index].fname << endl;
cout << tele[index].streetAdd << endl;
cout << tele[index].cityStateZ << endl;
cout << tele[index].phone << endl;
//Remove
infoType *temp[100]; //Not vectors now, quick
for(int i = index; i < size;i++)
{temp[nCount] = &tele[i];nCount++;}
for(int i = index; i < size;i++)
{nCount--;tele[i] = (*temp[nCount]);}
size--;
found = true;
}
else
index++;
}
}
int main()
{
ifstream fin("input.txt");
int newSize = 10;
infoType tele[SIZE];
readData(fin, tele, newSize );
newSize = optionA(fin, tele,newSize);
optionD(fin,tele,newSize);
//displayMenu();
system("pause");
return 0;
}
|