当我添加一个人时,它会正确添加,但是当我去删除名称时,它说找不到该名称。该程序允许用户创建电话簿并编辑电话簿中的信息
#include <iostream>
#include <string>
#include "PhoneBook.h"
using namespace std;
char MenuSelection ();
int main() {
char Selection;
PhoneBook myList;
do{
Selection = MenuSelection ();
switch (Selection){
case 'a':
myList.AddEntry ();
break;
case 'd':
myList.DisplayNamesAndNumbers( );
break;
case 's':
myList.FindEntry ();
break;
case 'r':
myList.DeleteFunction ();
break;
case 'q':
break;
default :
cout << "\n\nNot a command choice\n";
cout << "Press enter to continue";
cin.get();
cin.get();
system ("clear");
}
}while (Selection != 'q');
myList.MakeFile ();
cout << "Press enter to continue";
return 0;
}
//This function prints out the opening menu and allws users to enter a command.
char MenuSelection (){
char Response;
cout << "\n MENU\n";
cout << "a - add a name and phone number\n";
cout << "d - display names and phone number\n";
cout << "r - remove a name and phone number\n";
cout << "s - search for a name and return the phone number\n";
cout << "q - quit program\n\n";
cout << "Enter your choice: ";
cin >> Response;
return Response;
}
上述程序的头文件
这是允许用户操作电话簿的类。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int GROUPSIZE = 100;
struct contact {string NameEntry; string PhoneNumber;};
class PhoneBook {
private:
int Size;
contact ContactNumber [GROUPSIZE];
public:
PhoneBook ();
void MakeFile ();
void AddEntry ();
void FindEntry ();
string DisplayNamesAndNumbers ();
string DeleteFunction ();
};
//opens the file and adds names and numbers to the text file
PhoneBook::PhoneBook (){
string PersonsName, PhoneNum;
int i = 0;
ifstream infile;
infile.open ("phonebook.txt");
if (!infile){
cout << "File does not exist";
}
else{
while (!infile.eof ()){
infile >> PersonsName >> PhoneNum;
ContactNumber [i].NameEntry = PersonsName;
ContactNumber [i++].PhoneNumber = PhoneNum;
}
Size = i;
infile.close ();
}
}
//adds name and phone number to the list
void PhoneBook::AddEntry (){
string PersonsName, PhoneNum;
cout << "\n\nEnter the name to be added: ";
cin >> PersonsName;
cout << "Enter the phone number for " << PersonsName << ": ";
cin >> PhoneNum;
ContactNumber [Size].NameEntry = PersonsName;
ContactNumber [Size].PhoneNumber = PhoneNum;
Size++;
cout << "Press enter to continue";
cin.get ();
cin.get ();
system ("clear");
}
//finds names and numbers in the list
void PhoneBook::FindEntry (){
int Location, Counter;
string Contact;
cout << "\n\nName to find: ";
cin >> Contact;
Counter = 0;
while (Counter < Size){
Counter++;
//for (int i = 0; i < size; i++){
if (strcmp(Contact.c_str (),ContactNumber [Counter].NameEntry.c_str ()))
Location = Counter;
else
Location = -1;
}
if (Location != -1)
cout << "The phone number for " << Contact << " is " << ContactNumber [Location].PhoneNumber << endl;
else
cout << Contact << " not in phonebook\n";
cout << "Press enter to continue";
cin.get();
cin.get();
system ("clear");
}
//displays all information in the list
string PhoneBook::DisplayNamesAndNumbers (){
string PersonsName, PhoneNum;
int Check = 1;
cout << "\n\nList is being sorted\n";
while (Check == 1){
cout << "Name\t\tTelephone Number";
for (int i = 0; i < Size; i++){
cout << ContactNumber[i].NameEntry << "\t\t" << ContactNumber[i].PhoneNumber << "\n";
}
break;
}
cout << "Press enter to continue";
cin.get();
cin.get();
system("clear");
return "";
}
//deletes information from the list
string PhoneBook::DeleteFunction (){
char Answer;
string PersonsName;
int Location;
cout << "\n\nName to remove: ";
cin >> PersonsName;
for (int i = 0; i < GROUPSIZE; i++){
if (!strcmp(PersonsName.c_str (),ContactNumber[i].NameEntry.c_str ()))
Location = i;
else
Location = -1;
}
if (Location != -1){
ContactNumber[Location].NameEntry = ContactNumber[Size].NameEntry;
ContactNumber[Location].PhoneNumber = ContactNumber[Size].PhoneNumber;
cout << PersonsName <<" removed from phonebook\n";
cout << "Press enter to continue";
cin.get();
cin.get();
system("clear");
return"";
}
cout << "Name not found in phonebook\n";
cout << "Press enter to continue";
cin.get();
cin.get();
system ("clear");
return"";
}
//closes the file at the end of the program
void PhoneBook::MakeFile (){
ofstream outfile;
outfile.open("phonebook.txt");
for (int i = 0; i < GROUPSIZE; i++){
outfile << ContactNumber[i].NameEntry << " " << ContactNumber[i].PhoneNumber<<"\n";
}
outfile.close();
}