Polly's suitors in ONE HOUR!

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 22
Reputation: sakura_fujin is an unknown quantity at this point 
Solved Threads: 0
sakura_fujin sakura_fujin is offline Offline
Newbie Poster

Polly's suitors in ONE HOUR!

 
0
  #1
Mar 26th, 2007
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:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdio>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <math.h>
  7. #include <iomanip>
  8.  
  9. #define DESIRED_HEIGHT 180 //Polly's desired height in cm
  10. #define DESIRED_WEIGHT 75 //Polly's desired weight in kg
  11.  
  12. using namespace std;
  13.  
  14. int compare_suitor();
  15.  
  16. struct suitorData{
  17. char firstName[30]; //maximum length of suitor's firstname
  18. char lastName[30]; //maximum length of suitor's lastname
  19. int height;
  20. int weight;
  21. };
  22.  
  23. int main(){
  24. int i;
  25. int height=0;
  26. int weight=0;
  27. int numberOfSuitors=0;
  28. char firstName[30];
  29. char lastName[30];
  30. ifstream inSuitorFile("suitors.txt", ios::in);
  31. if(!inSuitorFile){
  32. cerr<<"File could not be opened!";
  33. exit(1);
  34. }
  35. suitorData suitor[20]; //maximum number of suitors
  36. while(inSuitorFile>>suitor[numberOfSuitors].firstName>>suitor[numberOfSuitors].lastName>>
  37. suitor[numberOfSuitors].height>>suitor[numberOfSuitors].weight){
  38. cout<<lastName<<","<<" "<<firstName <<" "<<height <<" "<<weight <<endl;
  39. suitor[numberOfSuitors].height = abs(height - DESIRED_HEIGHT);
  40. if (weight > DESIRED_WEIGHT)
  41. suitor[numberOfSuitors].weight = weight - DESIRED_WEIGHT;
  42. else
  43. suitor[numberOfSuitors].weight = - weight;
  44.  
  45. numberOfSuitors ++;
  46. }
  47. //qsort(reinterpret_cast<char*>(suitor), numberOfSuitors,
  48. //sizeof(suitorData ), compare_suitor());
  49. qsort(suitor, numberOfSuitors, sizeof(suitorData), compare_suitor); //either of the two isn't working... what's wrong?!?
  50.  
  51. for (i=0; i<numberOfSuitors; i++){
  52. cout<<suitor[i].lastName, suitor[i].firstName);
  53. }
  54. system("pause");
  55. return 0;
  56. }
  57.  
  58. int compare_suitor(suitorData *a, suitorData *b)
  59. {
  60. int result; //result of comparsion
  61. if (a->height < b->height)
  62. return(-1);
  63. if (a->height > b->height)
  64. return(1);
  65. if (a->weight < b->weight)
  66. return(-1);
  67. if (a->weight > b->weight)
  68. return(1);
  69.  
  70. if ((result=strcmp(a->lastName,b->lastName)) != 0)
  71. return result;
  72.  
  73. return(strcmp(a->firstName,b->firstName));
  74. }
ps. you need to create a file 'suitors.txt' for it to work. (data firstnames, surnames, height and weight)

thanks!
Last edited by sakura_fujin; Mar 26th, 2007 at 11:35 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Polly's suitors in ONE HOUR!

 
0
  #2
Mar 26th, 2007
the compare function should have this signature:
[inliecode]
int (*)(const void*, const void*)
[/inlinecode]
a. declare the function with the right signature.
b. in the definition, cast the args to const suitorData*, then compare.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Polly's suitors in ONE HOUR!

 
0
  #3
Mar 26th, 2007
why use qsort() ? use the c++ sort() that's in algorithm header file. I don't know if qsort() works with vectors.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Polly's suitors in ONE HOUR!

 
0
  #4
Mar 26th, 2007
qsort would work with std::vector (if the elements are POD).
qsort( &vec.begin(), ...
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 22
Reputation: sakura_fujin is an unknown quantity at this point 
Solved Threads: 0
sakura_fujin sakura_fujin is offline Offline
Newbie Poster

Re: Polly's suitors in ONE HOUR!

 
0
  #5
Mar 26th, 2007
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???
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Polly's suitors in ONE HOUR!

 
0
  #6
Mar 26th, 2007
what is the link error?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 22
Reputation: sakura_fujin is an unknown quantity at this point 
Solved Threads: 0
sakura_fujin sakura_fujin is offline Offline
Newbie Poster

Re: Polly's suitors in ONE HOUR!

 
0
  #7
Mar 26th, 2007
[Linker error] undefined reference to `compare_suitor(void const*, void const*)'
ld returned 1 exit status
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Polly's suitors in ONE HOUR!

 
0
  #8
Mar 26th, 2007
modify the function from
  1. int compare_suitor(suitorData *a, suitorData *b)
  2. {
  3. int result; //result of comparsion
  4. if (a->height < b->height)
  5. // ...
  6. [FONT=verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif]

to

  1. int compare_suitor(const void*aa, const void* bb)
  2. {
  3. const suitorData* a = static_cast<const
  4. suitorData*>(aa) ;
  5. const suitorData* b = static_cast<const
  6. suitorData*>(bb) ;
  7. int result; //result of comparsion
  8. if (a->height < b->height)
  9. // ...
  10. [FONT=verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif]

Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 917 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC