qsort multiple fields on struct array

When I add another field on my struct, it does not sort properly. But if there is just one field it will sort it perfectly.


//-----------------------------------------------------------
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <vector>
#include <search.h>
#include <cstring>
using namespace std;
//-----------------------------------------------------------
struct Astrct {
string input;
string input2;
};
Astrct arry1[3];
//-----------------------------------------------------------
void static print (struct Astrct *array, size_t len)
{
{
size_t i;
for(i=0; i<len; i++)
cout
<< arry1[i].input <<endl
<< arry1[i].input2 <<endl;
}
}
//-----------------------------------------------------------
int static compare(const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return strcmp(*ia, *ib);
}
//-----------------------------------------------------------
void static sort(void)
{
size_t arry1_len = sizeof(arry1) / sizeof(struct Astrct);
puts("*** Struct sorting...");
print(arry1, arry1_len);
cout << "*** Sorted Struct..." <<endl;
qsort(arry1, arry1_len, sizeof(struct Astrct), compare);
print(arry1, arry1_len);
cout <<endl;
}
//-----------------------------------------------------------
int main ()
{
int s = 0;
while (s <= 2)
{
cout << "Enter soemthing here: \n";
getline(cin,arry1[s].input);
cout << "Enter soemthing here: \n";
getline(cin,arry1[s].input2);
s++;
}

sort();

return 0;
}
//-----------------------------------------------------------
Topic archived. No new replies allowed.