2009-12-10 97 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