2009-12-10 24 views
0

所以我的任務是爲Person,Name,ID#,Address和Phone#創建多個類。C++文本菜單:寫入,讀取和排序數據

名稱組成:首字母,中間名和姓氏。 ID編號:9位數字。 地址組成:街道,城市,州和5位數郵政編碼。 電話號碼組成:3位數區號和7位數字。 個人組成:姓名(第一,中間,最後),地址,電話號碼(區號和號碼)以及ID號碼(9位數字)。

我完成了所有這些。我的問題是,我們也應該製作一個菜單,指定用戶希望輸入的人數,他們想要保存文件的位置,如果他們想要讀取或寫入用戶指定的文件,並且能夠按名稱(最後,第一或中間)或ID#對人員進行排序,並將排序後的列表保存到用戶指定的文件中。

我已經編寫了所有的代碼,但是由於某種原因,我的寫入功能不工作。發生什麼事是我運行該程序,我創建的菜單彈出。我選擇'1'輸入文件,然後菜單再次彈出,我選擇'2'以確保它不能讀取,因爲我正在測試的特定文件中沒有任何內容。接下來,我選擇'3'將People寫入用戶指定的文件。它會提示我要輸入多少人,並輸入一個數字(2)。然後在名字輸入的提示彈出,我得到了一些錯誤,說我的項目中的.exe「的未處理的win32異常出現」 ...

這裏是我的代碼:

//global variables 
char filename[256]; 
fstream file2 (filename); 

int r; 
Person * stuArrPtr=new Person[r]; 

int w; 
Person * stuArrPtr2=new Person[w]; 

//global functions 
void WriteUserFile() { 
//write as many ppl as specified to a file... 
// int w; 
cout << "How many students would you like to enter?: "; 
cin >> w; 

// Person * stuArrPtr2=new Person[w]; 
if (!file2.is_open()) { 
    cout << "File did not open" << endl; 
    file2.clear(); 
    file2.open (filename, ios_base::out); 
    file2.close(); 
    file2.open (filename, ios_base::out | ios_base::in); 

} 
else { 
    for (int i = 0; i < w/*!file2.eof()*/; i++) { 
    stuArrPtr2[i].InputPerson(); 
    if (strcmp(stuArrPtr2[i].PersonNam.GetFirst(), "EOF") != 0) 
    stuArrPtr2[i].Display (file2); 
    } 
} 
cout << endl; 
// delete [] stuArrPtr2; 
} 

void Menu() { 
int option; 
do { 
    //display menu 
    cout << " Type '1' - to open a file for reading or writing" << endl << endl; 
    cout << " Type '2' - to read from the file you specified in '1'" << endl << endl; 
    cout << " Type '3' - to write from the file you specified in '1'" << endl << endl; 
    cout << " Type '4' - sort students by last name" << endl << endl; 
    cout << " Type '5' - sort students by first name" << endl << endl; 
    cout << " Type '6' - sort students by middle name" << endl << endl; 
    cout << " Type '7' - sort students by ID number" << endl << endl; 
    cout << " Type '8' - exit" << endl << endl; 
// cout << " Enter appropriate number here: [ ]\b\b"; 
    cout << " Enter appropriate number here: "; 
    cin >> option; 

    switch(option) { 
    case 1: 
    cout << "you entered option 1" << endl; 
    OpenUserFile(); 
    break; 
    case 2: 
    cout << "you entered option 2" << endl; 
    ReadUserFile(); 
    break; 
    case 3: 
    cout << "you entered option 3" << endl; 
    WriteUserFile(); 
    break; 
    case 4: 
    cout << "you entered option 4" << endl; 
    SortLastName(); 
    break; 
    case 5: 
    cout << "you entered option 5" << endl; 
    SortFirstName(); 
    break; 
    case 6: 
    cout << "you entered option 6" << endl; 
    SortMiddleName(); 
    break; 
    case 7: 
    cout << "you entered option 7" << endl; 
    SortIDNumber(); 
    break; 
    case 8: 
    cout << "you entered option 8" << endl; //exit 
    delete [] stuArrPtr; 
    delete [] stuArrPtr2; 
    break; 
    default: 
    cout << "you screwed up, no big deal, just try again!" << endl; 
    } //end switch 
    //if (option == 6) { 
    // break; 
    //} 
} while (option != 8); 
// system("pause"); 
} 

void main() { 
Menu(); 
} 
/////////////////END OF CODE/////// 

對不起了代碼太長了,任何幫助都非常非常感謝!

+0

你能重新編寫代碼嗎?只需將它縮進4個空格。 – Lucas 2009-12-10 08:23:27

+0

你能否重新格式化你的代碼,至少代碼部分是在1塊?謝謝 – fritzone 2009-12-10 08:23:41

+1

這就是你如何編輯你的代碼:http://stackoverflow.com/editing-help – Lucas 2009-12-10 08:24:50

回答

1

您的代碼的問題是前幾行。

int w; 
Person * stuArrPtr2=new Person[w]; 

在程序啓動時w很可能是用0初始化的。所以你創建一個零個人的數組。 順便說一下,您撥打stuArrPtr2[i].InputPerson()應該是stuArrPtr2[i]->InputPerson(),您嘗試訪問一個不存在的對象的成員函數。

你將不得不做的是創建新的Person對象,取決於你剛剛輸入的數字,如stuArrPtr2 = new Person[w]在功能WriteUserFile()內。

乾杯 霍爾格

+0

好的,非常感謝你sooo!這個問題已經解決了,現在你指出了這一點對我來說很有意義。但是,現在我還有1個小問題。當我運行WriteUserFile()時,輸入學生的編號,並跳過第一個提示(輸入名字)。所以我可以輸入的第一個提示是中間名。爲什麼它會跳過第一個提示(輸入名字)? – nick 2009-12-10 16:44:54

+0

,因爲我不知道InputPerson()函數看起來像什麼很難分辨出什麼問題;-) – 2009-12-11 07:57:39

+0

非常感謝您的幫助,我在WriteUserFile()之前用額外的cin.getline解決了這個問題,解決了問題! – nick 2009-12-11 20:48:10

0

什麼stuArrPtr2[i].Display (file2);在做什麼?

你還沒有更好的方法來記住最後一個人嗎?

if (strcmp(stuArrPtr2[i].PersonNam.GetFirst(), "EOF") != 0) 

像數組或鏈表中的項目數。

+0

stuArrPtr2 [i] .Display(file2);將Person數據顯示到file2,它是用戶指定的輸入文件。 不,我不知道如何記住最後一個人... – nick 2009-12-10 08:42:27

+0

所以基本上stuArrPtr2 [i] .Display(file2);將人員數據寫入用戶指定的輸入文件... – nick 2009-12-10 08:44:20

0

一些提示:

  • 使用的std :: string,不是char
  • 使用新的,除非絕對必要的對象不創建數組
  • 瞭解範圍 - 你文件流應該不是全局的
  • 主要必須返回一個int
  • 想寫代碼之前
+0

那麼你錯了main,我的老師告訴我們按照上面這樣做的方式來做,而你所接受的方式被認爲是老練和壞習慣......你不能沒有太多努力就不能使用std :: string因爲我使用的許多函數rquire「char *」,所以它真的是我唯一的選擇......我明白我的文件流的範圍不應該是全局的,但它是我的代碼工作的唯一方式,我沒有時間寫另一類... 我真的很感激你的最後一條評論,那是什麼樣的評論!? – nick 2009-12-10 16:49:54

+0

我不認爲你的老師可能錯了嗎? – 2009-12-10 17:01:51

+0

不,它不會發生在我身上,因爲這是我告訴過我的第二位老師。另外,當我幾個月前訪問大學並坐在多個C++類中時,所有主要函數都按照我上面寫的方式編寫......即使我的老師錯了,爲什麼它會編譯並且其中的代碼運行? – nick 2009-12-10 17:24:15