所以我的任务是为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