The ID was sorted but the contents aren't

Hello there, I'm trying to build a sorting program that will sort product ID's and it's contents. I'm using the product ID as a base for the sorting. But there is one problem. The ID's already been sorted but the contents aren't how do i get the price and product name loaded with it's ID every time the bubble sort runs?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
#include <iostream>
#include <fstream>
using namespace std;

void Sort(int sortarray[], int sizeofarray, int sortorder);

int main()
{

    char productName[30][30];
    int productID[30];
    double price[30];
    int counter=0;
    int sortorder;

    ifstream infile("productfile.txt");

    while (infile >> productID[counter])
    {
        infile.get();
        infile.getline(productName[counter, 29])
        infile >> price[counter];
        counter++;
    }
    cout << "Press the following to sort via product ID" << '\n'
    << "1. For ascending order" <<  '\n'
    << "2. For descending order" << endl;
    cin >> sortorder;
    
    Sort(productID, counter, sortorder);

    return 0;
}

void Sort(sortarray[], int sizeofarray, sortorder)
{
    int temp;
    if (sortorder==1)
    {
        for (int i=0; i<=sizeofarray; i++)
        {
            for (int j=1; sortarray[j] < sortarray[i]; j++)
            {
                temp=sortarray[i];
                sortarray[i]=sortarray[j];
                sortarray[j]=temp;
            }
        }
    }
    else if (sortorder==2)
    {
        for(int i=0; i<=sizeofarray; i++)
        {
            for (int j=1; sortarray[j] > sortarray[i]; j++)
            {
                temp=sortarray[j];
                sortarray[j]=sortarray[i];
                sortarray[i]=temp;
            }
        }
    }
}
put your data into an array of structs and then sort it
^^ Yes, you want a structure of some type:
1
2
3
4
5
6
7
8
struct Product
{
  char name[30];
  double price;
  int id;
};

Product products[30];


Another method is to have another array, which is simply an array of indices:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int ids[]  = { 1,4,3,2 }; // unsorted ids
int idxs[] = { 0,1,2,3 }; // initialize indecies

mySortFunction(  ids, idxs ); // pass both to sort function

// result of sort
// ids  = { 1,4,3,2 };  // the same
// idxs = { 0,3,2,1 }; // sorted in terms of id

// Now you can act like the arrays are sorted:
cout << The sorted names << std::endl;  // my quote button breaks sometime

for ( int i = 0; i < 4; i++ )
  cout << names[ idxs[i] ] << std::endl;

I've tried what you've said but it started giving me alot of errors like

In function 'void Sort_func(Product*, int, int)':|
54|error: cannot convert 'Product' to 'int' in assignment|
56|error: no match for 'operator=' in '*(Sort_prod + ((sizetype)(((unsigned int)j) * 24u))) = temp'|
56|note: candidate is:|
|6|note: Product& Product::operator=(const Product&)|
|6|note: no known conversion for argument 1 from 'int' to 'const Product&'|
|66|error: cannot convert 'Product' to 'int' in assignment|
|68|error: no match for 'operator=' in '*(Sort_prod + ((sizetype)(((unsigned int)i) * 24u))) = temp'|
|68|note: candidate is:|
|6|note: Product& Product::operator=(const Product&)|
|6|note: no known conversion for argument 1 from 'int' to 'const Product&'|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

struct Product
{
    string name;
    double price;
    int prodID;
};

void Sort_func(Product Sort_prod[], int sizeofArray, int sortorder);

int main ()
{
    int counter=0;
    int sortorder;
    Product prod_var[10];

    ifstream infile("Productfile.txt");
    while (infile >> prod_var[counter].prodID)
    {
        infile.get();
        getline(infile,prod_var[counter].name);
        infile >> prod_var[counter].price;
        counter++;
    }
    cout << char(240) << "Press 1 to arrange the productsIDs in ascending order" << endl;
    cout << char(240) << "Press 2 to arrange it in descending order" << endl;
    cin >> sortorder;
    Sort_func(prod_var, counter, sortorder);

    for (int i=0; i < counter; i++)
    {
        cout << prod_var[counter].prodID << endl;
        cout << prod_var[counter].name << endl;
        cout << prod_var[counter].price << endl;
        counter++;
        cout << endl;
    }

}

void Sort_func(Product Sort_prod[],int sizeofArray,int sortorder)
{
    int temp;
    if (sortorder==1)
    {
     for (int i=0; i < sizeofArray; i++)
     {
         for (int j=1; Sort_prod[i].prodID >  Sort_prod[j].prodID; j++)
         {
             temp=Sort_prod[i];
             Sort_prod[i]=Sort_prod[j];
             Sort_prod[j]=temp;
         }
     }
    }
    else if (sortorder==2)
    {
        for (int i=0; i < sizeofArray; i++)
        {
            for(int j=1; Sort_prod[i].prodID < Sort_prod[j].prodID; j++)
            {
                temp=Sort_prod[j];
                Sort_prod[j]=Sort_prod[i];
                Sort_prod[i]=temp;
            }
        }
    }
}

Product temp;
Topic archived. No new replies allowed.