| | |
Polly's suitors in ONE HOUR!
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 22
Reputation:
Solved Threads: 0
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:
ps. you need to create a file 'suitors.txt' for it to work. (data firstnames, surnames, height and weight)
thanks!
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:
C++ Syntax (Toggle Plain Text)
#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)); }
thanks!
Last edited by sakura_fujin; Mar 26th, 2007 at 11:35 am.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
modify the function from
to
C++ Syntax (Toggle Plain Text)
int compare_suitor(suitorData *a, suitorData *b) { int result; //result of comparsion if (a->height < b->height) // ... [FONT=verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif]
to
C++ Syntax (Toggle Plain Text)
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) // ... [FONT=verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif]
![]() |
Similar Threads
- System clock lose about 4mins per hour!!! (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: C++ seeking data to file problem
- Next Thread: password input
Views: 917 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






