Question

Quick question.
I have to use parallel arrays to make this phone directory program. I was just wondering if their is a better way to do something this is a sample of my input.
Bill Smith
9768 N. Adrian Hwy
Detroit Mi, 48719
313-272-9871
So since arrays can only have one type I and since I might need to up date the info later I decided that I should have a first name array a second name array an int array for the street address(9763)
Arrays for all the different types one array for for city and state one for zip. You get the idea I'm sure. But this seems kind of stupid. And tedious. I was going to to do a function to get each one then put that in a loop and have it just do it while the file isn't empty. Does this seem like to much work?
Are you familiar with struct (or class)? You can then only have one array of your own type. It hardly reduces the amount of work, but allows a cleaner distribution of it.
closed account (3qX21hU5)
A struct would work nicely in this instance I think. A example of a way to go about this is something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct student_info
{
    string name;
    double midterm, final;
};

// Function to write the grades to the struct
istream &read(istream &is, student_info &s)
{
    is >> s.name >> s.midterm >> s.final;
    return is;
}

int main()
{
    vector<student_info> students;
    student_info record;

    cout << "If you would like to stop entering the students" <<
            "info at any time just enter end of file (CRTL Z)" << endl;
    cout << endl;

        while (read(cin, record))
        {
            students.push_back(record);
        }

}


This is some simplified code from accelerated C++ (Didn't wanna type out all the functions and stuff ;p) but I think you get the idea. If you still aren't familiar with struct you can check it out here on this website (Just enter struct into the search).

EDIT: Wait a second the code I just copied and pasted was just in another topic of yours talking about struct lol so you should know all that already ;p.
Last edited on
YEAH! HA. I know structures now. (Sort of). So, using the structure I could have all the arrays inside of it ? I'm still new to the structure idea. I can just put those inside? Simplify it a bit?
1
2
3
string first_name[50];
string second_name[50];
...


becomes

1
2
3
4
5
6
7
struct Person{
   string first_name;
   string second_name;
   ...
};

Person people[50];


You can then write a function/method to work with a single person and not worry about the array.
OH, the directions say that it has to be in parallel arrays. So I don't think I can use that. Thanks anyways would be nice to be able to use that though.
Or can I? Isn't that still an array? Because of the Person people[50]? It's just one array? Hmmm. I'm not sure.
That's an array but not parallel to anything. If you have to do it like that, what is your question again?
Never mind I think I'm just going to stick with the parallel arrays because of the directions thanks for the help.
Topic archived. No new replies allowed.