473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Loading an array of objects

143 100+
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>
  6.  
  7. ifstream FileinStu;
  8.  
  9. void main()
  10. {        
  11.     //open external file
  12. //    FileinStu.open("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;
  16.  
  17.     for(int i = 0; i < 10 ; i++)
  18. //        FileinStu>>stuArray[i];
Mar 29 '07 #1
4 2363
Ganon11
3,652 Expert 2GB
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
teddarr
143 100+
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
Ganon11
3,652 Expert 2GB
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
teddarr
143 100+
I will be storing students in an array.
Mar 30 '07 #5

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

Similar topics

17
by: Aguilar, James | last post by:
My previous example used the concept of a Shape class heirarchy, so I will continue with that. Suppose I have something like fifty different shapes, and I am trying to instantiate one of them. ...
4
by: Adrian MacNair | last post by:
Hi, I created an image gallery which displays 63 images in a slideshow. The problem is that the show was slow because each image loaded one at a time during the show. No problem right? I just...
4
by: Kevin | last post by:
Hi, I have a input with type checkbox. I want it to be automatically checked if the value from the corresponding field in the database is also checked. I tried this: <% set objdc =...
9
by: Ender | last post by:
I have an application that I would like third party developers to be able to create Plug-ins that will be dynamically loaded into our application to extend functionality. I have utilized the...
6
by: Pete Davis | last post by:
I'm confused about what precisely the limitations are on loading plugins in separate app domains. In all my previous apps that supported plugins, I've loaded them into the same domain as the app,...
5
by: John Richardson | last post by:
I've been bothered for some time about my DataGrid not populating my rows very quickly. I have about 10K rows loading into the grid. I create a datatable dt with 2 columns, an ID and a display. ...
1
by: Dale | last post by:
I have a user control on a Windows Form that, in its Load event handler, calls a method from a class library to initialize an array of objects that are then used to populate a ComboBox. When I...
1
by: rekkufa | last post by:
I am currently building a system for serializing python objects to a readable file-format, as well as creating python objects by parsing the same format. It is more or less complete except for a...
9
by: igor.tatarinov | last post by:
Hi, I am pretty new to Python and trying to use it for a relatively simple problem of loading a 5 million line text file and converting it into a few binary files. The text file has a fixed format...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.