Member Avatar for sakura_fujin

hello!
I badly need to submit this program within an hour from now so any help out there who could help debug my code, i would really appreciate it. there's something wrong with my qsort() syntax but i can't figure out what . spent 5 hours trying every possible ways for it but got no luck.
PLS HELP ME!!!

Here's the code:

#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <math.h>
#include <iomanip>

#define DESIRED_HEIGHT 180 //Polly's desired height in cm
#define DESIRED_WEIGHT 75 //Polly's desired weight in kg

using namespace std;

int compare_suitor();

struct suitorData{
    char firstName[30];    //maximum length of suitor's firstname
    char lastName[30]; //maximum length of suitor's lastname         
    int height;            
    int weight;            
};

int main(){
    int i; 
    int height=0;
    int weight=0;
    int numberOfSuitors=0;
    char firstName[30];
    char lastName[30];
    ifstream inSuitorFile("suitors.txt", ios::in);
    if(!inSuitorFile){
       cerr<<"File could not be opened!";
       exit(1);
    }
    suitorData suitor[20]; //maximum number of suitors
    while(inSuitorFile>>suitor[numberOfSuitors].firstName>>suitor[numberOfSuitors].lastName>>
          suitor[numberOfSuitors].height>>suitor[numberOfSuitors].weight){
          cout<<lastName<<","<<" "<<firstName <<" "<<height <<" "<<weight <<endl;
          suitor[numberOfSuitors].height = abs(height - DESIRED_HEIGHT);
        if (weight > DESIRED_WEIGHT)
            suitor[numberOfSuitors].weight = weight - DESIRED_WEIGHT;
        else
            suitor[numberOfSuitors].weight = - weight;

        numberOfSuitors ++;
    }
    //qsort(reinterpret_cast<char*>(suitor), numberOfSuitors,
              //sizeof(suitorData ), compare_suitor());
    qsort(suitor, numberOfSuitors, sizeof(suitorData), compare_suitor); //either of the two isn't working... what's wrong?!?

    for (i=0; i<numberOfSuitors; i++){ 
        cout<<suitor[i].lastName, suitor[i].firstName);
    }
    system("pause");
    return 0;
}

int compare_suitor(suitorData *a, suitorData *b)
{   
    int result;            //result of comparsion 
    if (a->height < b->height) 
       return(-1);
    if (a->height > b->height) 
       return(1);
    if (a->weight < b->weight) 
       return(-1);
    if (a->weight > b->weight) 
       return(1);

    if ((result=strcmp(a->lastName,b->lastName)) != 0) 
       return result;

    return(strcmp(a->firstName,b->firstName));
}

ps. you need to create a file 'suitors.txt' for it to work. (data firstnames, surnames, height and weight)

thanks!

the compare function should have this signature:

int (*)(const void*, const void*)

a. declare the function with the right signature.
b. in the definition, cast the args to const suitorData*, then compare.

why use qsort() ? use the c++ sort() that's in algorithm header file. I don't know if qsort() works with vectors.

qsort would work with std::vector (if the elements are POD).
qsort( &vec.begin(), ...

Member Avatar for sakura_fujin

still doesn't work....

whenever i follow the right syntax, errors are multiplying itself...

our prof requires us to use qsort()

i tried something new and my only error now is linker error what does that mean???

what is the link error?

Member Avatar for sakura_fujin

[Linker error] undefined reference to `compare_suitor(void const*, void const*)'
ld returned 1 exit status

modify the function from

int compare_suitor(suitorData *a, suitorData *b)
{   
    int result;            //result of comparsion 
    if (a->height < b->height) 
    // ...

to

int compare_suitor(const void*aa, const void* bb)
{   
   const suitorData* a = static_cast<const 
suitorData*>(aa) ;
   const suitorData* b = static_cast<const 
suitorData*>(bb) ;
    int result;            //result of comparsion 
    if (a->height < b->height) 
    // ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.