當我添加一個人它會被正確添加,但是當我去刪除它的名稱說沒有找到該名稱。該程序允許用戶在電話簿中創建的電話簿和編輯信息對上述程序我不能讓我的刪除功能從列表中刪除項目
#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();
}
哇,代碼太多了。而且格式不好引導。你能分辨出讓你困惑的部分嗎? – 2011-04-14 03:14:50
string PhoneBook :: DeleteFunction(){ char答案; string PersonsName; int位置; cout <<「\ n \ n要移除的名稱:」; cin >> PersonsName;如果(!strcmp(PersonsName.c_str(),ContactNumber [i] .NameEntry)爲(012)。c_str())) Location = i; else Location = -1; } – NewProgrammer22 2011-04-14 04:21:07
if(Location!= -1)ContactNumber [Location] .NameEntry = ContactNumber [Size] .NameEntry; ContactNumber [Location] .PhoneNumber = ContactNumber [Size] .PhoneNumber; cout << PersonsName <<「從電話本中刪除\ n」; cout <<「按回車繼續」; cin.get(); cin.get(); 系統(「清除」); return「」; } cout <<「在電話簿中找不到名稱\ n」; cout <<「按回車繼續」; cin.get(); cin.get(); 系統(「清除」); return「」; } – NewProgrammer22 2011-04-14 04:21:49