![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
PFO Founder
![]() ![]() |
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. |
|
|
|
|
|
#2 |
|
PFO Founder
![]() ![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|