I'm working on a program that mimics an "Animal Adoption Agency." I read from the file, which contains a list of animal names, breed, age, price, and sex. I have two files, one each for cats and dogs.
The user has an option to sort the list by the above listed categories. I have a for loop currently that will accurately sort the category they choose; however, the other categories will not order themselves accordingly. I'm not sure how to go about this. Here's a condensed version of my code, that allows access only to the dogs portion and sorting by name, rather than have a choice of how to sort.
#include <iostream>
#include <fstream>
#include <istream>
#include <cctype>
#include <string>
#include <string.h>
#include <cstring>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
int animalMenu, animalCount, animAge[50], animPrice[50], entry = 0, total;
string animType, animName[50], animBreed[50], animSex[50], takeHomeWith;
ifstream animalInform;
const int WIDTH = 8, BIG_WIDTH = 12;
void sortingHat(string[]);
void innerSorting(string[], int);
int main() {
animalInform.open("Dog Information.txt");
animType = "dogs";
// SET NUMBER OF ANIMALS IN FILE
animalInform >> animalCount;
cout << "There are " << animalCount << " " << animType << "! \n";
// SETS ALL THE VALUES BY READING FROM FILE
for (int entry = 0; entry < animalCount; entry++) {
animalInform >> animName[entry] >> animBreed[entry] >> animAge[entry] >> animPrice[entry] >> animSex[entry];
cout << setw(BIG_WIDTH) << animName[entry] << setw(BIG_WIDTH) << animBreed[entry] << setw(WIDTH) << animAge[entry] << setw(WIDTH) << animPrice[entry] << setw(WIDTH) << animSex[entry] << endl;
}
// CLOSE FILE
animalInform.close();
// CALL FUNCTION TO SORT (BY NAME ONLY)
sortingHat(animName);
cout << endl;
// DISPLAY NEWLY SORTED LIST
for (int entry = 0; entry < animalCount; entry++) {
cout << setw(BIG_WIDTH) << animName[entry] << setw(BIG_WIDTH) << animBreed[entry] << setw(WIDTH) << animAge[entry] << setw(WIDTH) << animPrice[entry] << setw(WIDTH) << animSex[entry] << endl;
}
system("pause");
}
void sortingHat(string sortingString[])
{ // SORTS DATA AND PUTS IT IN ORDER, ALPHABETICAL --
for (int outer = 0; outer <= animalCount; outer++)
{
for (int entry = 0; entry <= (animalCount - 2); entry++) {
string temporary[50];
if (sortingString[entry] > sortingString[entry + 1])
innerSorting(sortingString, entry);
}
}
}
void innerSorting(string sorter[], int entry)
{
string temporary[50];
temporary[entry] = sorter[entry];
sorter[entry] = sorter[entry + 1];
sorter[entry + 1] = temporary[entry];
}
So I obviously don't have anything that would make the other entries follow suit. So if I choose name to be sorted, my output (this is what is written in my file) will go from
Brienne Shepard 6 $150 F
Jon Labrador 3 $200 M
Aemon ShihTzu 10 $50 M
to
Aemon Shepard 6 $150 F
Brienne Labrador 3 $200 M
Jon ShihTzu 10 $50 M
And I want it to do this (if choosing to sort by name):
Aemon ShihTzu 10 $50 M
Brienne Shepard 6 $150 F
Jon Labrador 3 $200 M
If I have understood you correctly you have a set of arrays that contain animal characteristics. And you are going to sort by one characteristics such a way that all arrays would be sorted. If so then you can write one common function for all arrays.
For example
enum SortType { SortByName, /* other types of sorting */, SortByAge };
//...
void bubble_sort( std::string animName[],
/* other characteristics */
int animAge[],
size_t n,
SortType type )
{
for ( size_t last; not ( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
bool less = false;
switch ( type )
{
case SortByName:
less = animName[i] < animName[i-1];
break;
/* other cases */
case SortByAge:
less = animAge[i] < animAge[i-1];
break;
}
if ( less )
{
/* swapping elements of all the arrays */
last = i;
}
}
}
}
Take into account that this swap function
void innerSorting(string sorter[], int entry)
{
string temporary[50];
^^^^^^^^^^^^^^^^^^^^
temporary[entry] = sorter[entry];
sorter[entry] = sorter[entry + 1];
sorter[entry + 1] = temporary[entry];
}
should not use an array of strings. It can be written like
void innerSorting(string sorter[], size_t entry)
{
string temporary;
^^^^^^^^^^^^^^^^^
temporary = sorter[entry];
sorter[entry] = sorter[entry + 1];
sorter[entry + 1] = temporary;
}