所以我的任務是爲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///////
對不起了代碼太長了,任何幫助都非常非常感謝!
你能重新編寫代碼嗎?只需將它縮進4個空格。 – Lucas 2009-12-10 08:23:27
你能否重新格式化你的代碼,至少代碼部分是在1塊?謝謝 – fritzone 2009-12-10 08:23:41
這就是你如何編輯你的代碼:http://stackoverflow.com/editing-help – Lucas 2009-12-10 08:24:50