Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 3rd, 2004, 12:32 AM   #1
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,667
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
ok im having small problem with my qsort and comparefunction here is my main.cpp and what the output looks like and what it should look like.

main.cpp
// file: main.cpp
// author: Kyle Kjorsvik
// date: 8/31/04
// class: CSIS 352

// This is the file that contains the main function for assignment 1.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include "agecalc.h"

using namespace std;

//****** Define birthdate struct*************************************/
struct birthdate{
  string name;
  string bdmonth;
  string bdday;
  string bdyear;
  int age;
};

//****** Function Prototypes*****************************************/
//int agecalc(string strmonth, string strday, string stryear); // add a record
int comparefunc(const void *arg1, const void *arg2); // sort array of structs

int main() {
	int intmonth;
	int intday;
	int intyear;
  const int maxvalue = 10;
  birthdate bd[maxvalue + 1];
  int i = 0, j;
  char garbage;

  ifstream inFile("data");
  if (inFile.fail()) {
    cerr << "An error occurred. Unable to read input file." << endl;
    exit(1);
  }
	
	
  while(!inFile.eof() && i < maxvalue) {
    getline(inFile, bd[i].name, ',');
 inFile.get(garbage);
 getline(inFile, bd[i].bdmonth, '/');
 getline(inFile, bd[i].bdday, '/');
 getline(inFile, bd[i].bdyear);
    ++i;
  }
  inFile.close();
  
  //sort from oldest to youngest still needs to be done!!!!!
  qsort(bd, maxvalue, sizeof(birthdate), comparefunc);            // <<<<<<<
  
  i = 0;
  while(i < maxvalue) {
 intmonth = atoi(bd[i].bdmonth.c_str());
 intday = atoi(bd[i].bdday.c_str()); 
 intyear = atoi(bd[i].bdyear.c_str());
 AgeCalc a(intmonth, intday, intyear);
 bd[i].age = a.calcage();
  	++i;
  }

  ofstream outFile("output");
  if (outFile.fail()) {
	cerr << "An error occurred. Unable to open output file." << endl;
	exit(1);
  }
	
  i = 0;
  while(i < maxvalue) {
	outFile << setw(25) << left << bd[i].name;
	outFile << setw(3) << right << bd[i].age << endl;
	++i;
  }

  return 0;
}
 
// sort stuff below                                      // <<<<<<<
int comparefunc(const void *arg1, const void *arg2)
{
 	const birthdate *date1 = (birthdate *)arg1;
 	const birthdate *date2 = (birthdate *)arg2;

	if (date1->bdyear < date2->bdyear)
   return -1;
	else if (date1->bdyear > date2->bdyear)
   return 1;
	else {
   if (date1->bdmonth < date2->bdmonth)
   return -1;
   else if (date1->bdmonth > date2->bdmonth)
   return 1;
   else {
   if (date1->bdday < date2->bdday)
  return -1;
   else if (date1->bdday > date2->bdday)
     return 1;
 }
	}
}

output it produces (birthdates arent printed in the output file. i just added to help show how they should be organized
Corey Koskie    31 6/28/1973
Joe Nathan            29 11/22/1974
Shannon Stewart      30 2/25/1974
Matthew LeCroy       28 12/13/1975
Jacque Jones         29 4/25/1975
Torii Hunter           29 7/18/1975
J. C. Romero          28 6/4/1976
Lew Ford             28 8/12/1976
Justin Morneau        23 5/15/1981
Some Young Kid       5 7/9/1999

what it should look like
Corey Koskie    31 6/28/1973
Shannon Stewart      30 2/25/1974
Joe Nathan            29 11/22/1974
Jacque Jones         29 4/25/1975
Torii Hunter           29 7/18/1975
Matthew LeCroy       28 12/13/1975
J. C. Romero          28 6/4/1976
Lew Ford             28 8/12/1976
Justin Morneau        23 5/15/1981
Some Young Kid       5 7/9/1999
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Sep 3rd, 2004, 12:36 AM   #2
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,667
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
already figured it out that didnt take long at all. here is what i did to fix it
original
int comparefunc(const void *arg1, const void *arg2)
{
 const birthdate *date1 = (birthdate *)arg1;
 const birthdate *date2 = (birthdate *)arg2;

if (date1->bdyear < date2->bdyear)
  return -1;
else if (date1->bdyear > date2->bdyear)
  return 1;
else {
  if (date1->bdmonth < date2->bdmonth)
   return -1;
  else if (date1->bdmonth > date2->bdmonth)
   return 1;
  else {
   if (date1->bdday < date2->bdday)
  return -1;
   else if (date1->bdday > date2->bdday)
     return 1;
 }
}
}

fixed
int comparefunc(const void *arg1, const void *arg2)
{
 const birthdate *date1 = (birthdate *)arg1;
 const birthdate *date2 = (birthdate *)arg2;

if (date1->bdyear < date2->bdyear)
  return -1;
else if (date1->bdyear > date2->bdyear)
  return 1;
else {
  if (date1->bdmonth < date2->bdmonth)
   return 1;
  else if (date1->bdmonth > date2->bdmonth)
   return -1;
  else {
   if (date1->bdday < date2->bdday)
  return 1;
   else if (date1->bdday > date2->bdday)
     return -1;
 }
}
}
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC