I'm working on a program that takes a list of words entered by the user, ignores the cases (upper and lower) and then sorts them using the function qsort. I'm having an issue with qsort in that I don't know what to pass as the 3rd item qsort(array, sizeOfArray, ??, funcCompare). Can somebody point me in the right direction?
using namespace std;
int compare(const void* , const void*);
const int SIZE = 100;
void main()
{
int i = 0;
int s = 0;
size_t size = 0;
string words;
string list[SIZE];
for (i = 0; i < SIZE; i++)
{
cout << "Please enter a word. Press ^Z to quit: " << endl;
cin >> words;
transform(words.begin(), words.end(), words.begin(), ::tolower);
if (words.length() > size)
{
size = words.length();
}
list[i] = words;
if (cin.eof())
{
s = i;
break;
}
}
qsort(list, s, ?? , compare);
for (int j = 0; j < i; j++)
{
cout << list[j] << endl;
}
}
int compare(const void* p1, const void *p2)
{
char char1, char2;
char1 = *(char *)p1; // cast from pointer to void
char2 = *(char *)p2; // to pointer to int
if(char1 < char2)
return -1;
else
if (char1 == char2)
return 0;
else
return 1;
}
The spot in question in qsort has the '??' Any help you can give is appreciated!
This is an assignment
std::sortandstd::vector<std::string>, obviously.qsort, don't usestd::string. If you really must, use pointers tostd::stringin your array.