sorting corresponding vectors

I am creating an inventory. Each item consists of an item name and number. I have created separate vectors for the name and number. I am trying to use insertion sort to sort the numbers (which is the order I want the items to be in) but it isn't working correctly for some reason unknown to me. I also need to sort the corresponding name in the same order (in the other vector). for example, if Numbers[0]=1002 / Names[0]=cheese and Numbers[1]= 1001 / Names[1]=hotdogs, when I sort it Numbers[i] will switch, but I need Names[i] to switch also. Thank you so much!!! The code I have so far is below:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void menuSelection(int &selection, vector<string> &Names);
void performSelection(int n, int i, vector<string> &Names, vector<string> &Numbers, string numberInput, string nameInput, int &selection );

int main(int argc, char *argv[])
{
int selection;
int n;
string numberInput;
string nameInput;
vector<string> Names; // set to 15???
vector<string> Numbers;
int i;


do
{
menuSelection(selection , Names );
performSelection(n, i, Names, Numbers, numberInput, nameInput, selection);
} while (selection !=0);

system("PAUSE");
return EXIT_SUCCESS;
}

void menuSelection(int &selection, vector<string> &Names)
{
cout << "1. Load Inventory" << endl;
cout << "2. Add Item" << endl;
cout << "3. Search for Item" << endl;
cout << "4. List Items" << endl;
cout << "5. Save Inventory" << endl;
cout << "6. End Program" << endl;
cout << "Number of Items in Inventory: "<< Names.size() << endl;
cout << endl;
cout << "Enter the number of the action you would like to do: " << endl;
cin >> selection;
}

void performSelection(int n, int i, vector<string> &Names, vector<string> &Numbers, string numberInput, string nameInput, int &selection )
{
if (selection==1)
{
ifstream inputFile;
string filename;
string line;
cout << "Enter the filename where the inventory is stored:" << endl;
cin >> filename;
inputFile.open(filename.c_str());
if (inputFile.fail())
{
cerr << "File opening failed.\n";
system ("PAUSE");
exit (1);

}
else
{
cout << "The file opened" << endl;
}




}

if (selection==2)
{
cout << "Enter the number of the item: ";
cin >> numberInput;
Numbers.insert(Numbers.end(), 1, numberInput);
cout << "Enter the name of the item: ";
cin >> nameInput;
Names.insert(Names.end(), 1, nameInput);

int j; //insertion sorting begins
for(int i = 1; i < Numbers.size(); i++)
{
j = i - 1;

while((i-1) >= 0 && Numbers[j] > Numbers[i])
{
Numbers[j + 1] = Numbers[j];
j = j - 1;
}

Numbers[j + 1] = Numbers[i];
} //insertion sorting ends

for(n = 0 ; n < Numbers.size() ; n++ )
{
cout << Numbers[n] << endl;
}
}
if (selection == 6)
{
system ("PAUSE");
exit (1);
}
}
Last edited on
A better way to do this, and one that will help you develop some other abilities, would be to create a struct/class that stores the number and the string for each item in the inventory, and then overload the operator> for the new struct/class.
It's very hard to sort a vector while maintaining associativity with data in other vectors. The easiest way to do it is to not use other vectors. Create your own object to contain the data you need, then build a vector of those objects. Then if you are using the sort algorithm (which you aren't, but I would recommend) you can write a predicate function that will dictate how your objects will be sorted.
ok so i used: sort(Numbers.begin(), Numbers.end());
So how would I maintain associativity with the other vector?
A better way to do this, and one that will help you develop some other abilities, would be to create a struct/class that stores the number and the string for each item in the inventory, and then overload the operator> for the new struct/class.
Topic archived. No new replies allowed.