开发者

C++ text menu: writing, reading, and sorting data

开发者 https://www.devze.com 2022-12-13 13:23 出处:网络
So my assignment is to create multiple classes for a Person, Name, ID #, Address, and Phone #. Name makes up: First, Middle, and Last name.

So my assignment is to create multiple classes for a Person, Name, ID #, Address, and Phone #.

Name makes up: First, Middle, and Last name. ID # makes up: 9 digits. Address makes up: street, city, state, and 5 digit zip code. Phone # makes up: 3 digit area code and 7 digit number. Person makes up: a full Name (First, Middle, Last), an Address, a Phone # (area code, and number), and a ID # (9 digit number).

I have accomplished all of this. My problem is we are also supposed to make a menu, to specify how many people the user wishes to type in, where they want to save the file, if they want to read or write to a file specified by the user, and being able to sort the people by name (last, first, or middle) or by ID #, and save the sorted list to a user specified file.

I have all the code written, but my write function is not wo开发者_JS百科rking for some reason. What happens is I run the program, the menu I created pops up. I select '1' enter the file, then the menu pops up again, and I select '2' to make sure it cant read since there is nothing in the specific file I am testing with. Next, I select '3' to write People to the user specified file. It prompts me for how many People I want to enter and I enter a number (2). Then the prompt for typing in the first name pops up and I get some error saying "an unhandled win32 exception occured" in my project .exe...

Here is my code:

//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///////

Sorry the code is so long, and any help is very, very much appreciated!


the problem with your code are the first few lines.

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

At program startup w is most probably initialized with 0. So you create an array of zero Persons. The moment you call stuArrPtr2[i].InputPerson() which should be stuArrPtr2[i]->InputPerson() by the way, you try to access a member function of an non existing object.

What you will have to do is create new Person objects depending on the number you just entered like stuArrPtr2 = new Person[w] within the function WriteUserFile().

Cheers Holger


what stuArrPtr2[i].Display (file2); is doing ?

also you don't have a better way to remember the last person ?

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

like number of item in the array or chained list.


Some hints:

  • use std::string, not arrays of char
  • do not create objects using new unless absolutely necessary
  • learn about scope - your file stream shoud not be global
  • main must return an int
  • think before writing code
0

精彩评论

暂无评论...
验证码 换一张
取 消