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.
#include <iostream>
#include <vector>
#include <string>
usingnamespace 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.
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?
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.