struct PersonalData
{
intchar *FirstName;
char *LastName;
char *Birthday; // in the format of 12/30/1978
int id;
};
PersonalData PersonOne;
// Populate PersonOne with data
PersonOne.FirstName = "John";
PersonOne.LastName = "Doe";
PersonOne.Birthday = "12/30/1978";
PersonOne.id = 5;
// Populate PersonTwo with data
PersonTwo.FirstName = "Matt";
PersonTwo.LastName = "Thompson";
PersonTwo.Birthday = "15/20/1958";
PersonTwo.id = 3;
Lets say i add even more persons, How can I sort them lets say according to the 'id' so i can print them out in lets say, descending order.
so with higher 'id' value first, and then with lower and so on??
Also, once you have them sorted, it doesn't matter the order either ascending or descending because you can correct that with a loop. It would be easier just to sort them accordingly with the sort function the first time but even if you do not you can just use a loop to give what you want. First I would try to sort them how I wanted. Below is what I was talking about with the loop.
#include<iostream>
usingnamespace std;
int main()
{
int one[5] = {1,2,3,4,5};
// This goes one way
for(int i = 0; i < 5; i ++)
{
cout << one[i] <<" ";
}
cout << endl;
//// This goes the other
for(int i = 4; 0 <= i; i--)
{
cout << one[i] <<" ";
}
system("pause");
return 0;
}
ye, i know how to sort arrays, you go through the array indexes with a for loop, but how do you cicle through a structure? -i dont know how to use it in a structure...
You can print it like above. If you read my example above with the sort function you will see that it says if( tele[j].lname < tele[smallest].lname )
That's using a structure. .lname
Is that what you want?
noob I don't know what this means but don't apologize for any question.
When I did the assignment I took that from I used an array. You said that you might want to add more people to this eventually? A vector would be better but I do not know much about those.
This is how I organized my own project.
Then I used an array.
declared as such infoType tele[NSIZE];
You already have the sort function above.
That's as far as I got lol :)
I'm just suggesting maybe you can you can do something similar. I say that vectors are better because I know you can add or subtract from the size.
Just advice take it or leave it.
I'm new to programming also I saw your post and said oh I have done something similar before.
If you are in control of the input for this program, you should create an input file with the names and information in the order you want to read them in. Then you can read it into an array. This will bring everything into a nice orderly place then you can use basically the same sort function I gave you above. Otherwise I think (I think) it will be a pain to have all of the people separated all over the place. It's going to make it hard to sort.
Look try this for an input function just create a file and order the input
John
Doe
12/...etc.
You could literally just modify this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Function to get info from file.
void readData(ifstream &fin, infoType tele[], int& size)
{
int i = 0;
while(!fin.eof())
{
fin >> tele[i].fname;
fin >> tele[i].lname;
fin.get();
getline(fin,tele[i].streetAdd,'\n');
getline(fin,tele[i].cityStateZ,'\n');
getline(fin,tele[i].phone,'\n');
i++;
}
size = i;
}
I needed the size = i but if you know how many you are bringing in you could do without.
Then once you have this into an array just use the sort function I gave you. The only thing you actually have to do is create the array to be read into.