2011-04-14 110 views
0

當我添加一個人它會被正確添加,但是當我去刪除它的名稱說沒有找到該名稱。該程序允許用戶在電話簿中創建的電話簿和編輯信息對上述程序我不能讓我的刪除功能從列表中刪除項目

#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(); 
} 
+4

哇,代碼太多了。而且格式不好引導。你能分辨出讓你困惑的部分嗎? – 2011-04-14 03:14:50

+0

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

+0

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

回答

2

看起來你忘了在找到匹配項後刪除你的刪除。

1

我認爲你使用的是strcmp錯誤。如果兩個字符串匹配,它將返回0,因此您需要strcmp() == 0。看看here爲可能的返回值。
接下來,因爲你已經在使用std::string S,只需直接比較這些,他們支持:

if(Contact == ContactNumber[Counter].NameEntry) 

此外,你還沒有找到入口後,破壞你的for循環的出要刪除。


另一個側面提醒:您的電話簿可以包含重複的條目,因爲你不檢查,如果這個人在你AddEntry功能已經exsists。
最後,在經歷了所有這些基本上從一個字符串映射到另一個字符串的痛苦之後,爲了進一步使用這樣的數據結構,請考慮使用std::map。 :)

#include <map> 

int main(){ 
    map<string /*name*/, string /*number*/> phonebook; 
    phonebook["Meyers"] = "03024233"; 
    string number = phonebook.find("Meyers"); 
} 
0
contact ContactNumber [GROUPSIZE]; 

當前實現,我認爲它不會刪除對象的數組中的條目。你有GROUPSIZE號碼的contact對象(即一組對象)。數組無法修改大小。如果你需要從數組中刪除一個元素,首先你需要一個contact類型的引用。

contact *ContactNumber ; // new it with GROUPSIZE number of objects in PhoneBook constructor. 

如果您需要在m位置刪除元素,你需要使用複製交換規則GROUPSIZE - m翻譯。即m+1位置對象應該位於m位置,m+2位置對象應位於m+1位置,....是我認爲正確的執行方式。並且請記住在PhoneBook的析構函數中使用delete[] ContactNumber;以避免內存泄漏。

爲了避免所有的痛苦,改用std::vector代替。