int main()
{
SIZE = however many names there are in the file
int names[SIZE], ages[size]; // create 2 separate arrays of same size
for(int i = 0 to SIZE-1)
{
file >> names[i] >> ages[i]; // put name in name array, and age in age array
}
for(i = 0 to SIZE-1)
{
for(j = 0 to SIZE-2)
{
if( name[j] > name[j+1] ) // if names out of order
{
swap(names[j], name[j+1]); // swap the names
swap(ages[j], ages[j+1]); // swap their corresponding ages
}
}
}
return 0;
}
Obviously, above is not exactly real c++, just something I put together quickly.
If you don't have to use arrays, another option would be to use an std::map<std::string, int>. For each line you will read the name into a string variable and the age into an integer variable, then construct a pair from the name and age and insert it into the map. Here's a simple example using console input: