By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,052 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,052 IT Pros & Developers. It's quick & easy.

Loading an array of objects

P: 143
Here's my quandry.....I am building a phone book program with student and staff classes. In main() I am to define 2 arrays 1 for students and 1 for staff.

Each array will hold objects. I have declared the student array and I have a constructor in the student class that doesn't take any arguments. So I have no problem declaring the objects.

The problem is, how do I define the objects and load them in the array?

Each student object contains the following data members. first name, last name, phone number (as string), GPA (as string) and # on hours enrolled (as int).

Using a for loop, how do I put these objects into the array?

And, oh yeah, it all needs to be written to an external file.

Here's my code so far:

Expand|Select|Wrap|Line Numbers
  1. #include "Student.h"
  2. #include <iostream>
  3. #include <string>    //for string manipulation of names
  4. #include <iomanip>    //for set width
  5. #include <fstream>
  7. ifstream FileinStu;
  9. void main()
  10. {        
  11.     //open external file
  12. //"phoneBookStu.txt");
  13.     //create 2 arrays to store data in and load from the file
  14.     Student stuArray[10];  //ensure there is a constructor that accepts no arguements
  15.     int stuCount = 0;
  17.     for(int i = 0; i < 10 ; i++)
  18. //        FileinStu>>stuArray[i];
Mar 29 '07 #1
Share this Question
Share on Google+
4 Replies

Expert 2.5K+
P: 3,652
How will you be getting the information? Will you be using user input to get the names and information? Will they be stored in a file?

Generally, you should have methods such as setFName(string str), setLName(string str), and setGPA(string str) so be able to load the data into an individual object. Once the object has been fully defined, you can add it to the array.
Mar 29 '07 #2

P: 143
I do have those functions in the student class. As a matter of fact set and get functions are just about all the class has in it.

There is also a constructor that will take all of the arguments as well as the constructor that doesn't take any arguments.

The original used interface is a menu that will have the selections of display all students, display all staff, display all entries in both arrays, display all entries with a given last name, and finally a selection for add a new entry. So I need to know how to add load the array with the furnished info and eventually add new entries.

We briefly talked about templates and the stack template the other night so I am thinking about using that.

But how do I get started? I'll post more of what I get as it works on the program.
Mar 30 '07 #3

Expert 2.5K+
P: 3,652
Well, unless you are loading original data from a file (which you still haven't specified), then the only place you will be adding data is in the Add New Entry portion. You should declare some temporary variables to hold the information as you get it from the user, prompt the user for input, and once you have all the information, create a new Student object with the specified information and add it to the list.

Will you be storing the students in an array? In a vector? In some other data structure?
Mar 30 '07 #4

P: 143
I will be storing students in an array.
Mar 30 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.