I've tried nearly everything. Just looking for some tips.
The project is to read data from a file ["racers2011.txt"] into a struct and sort the race times for the males and sort the racetimes for the females. Group the males and the females serperately and output the with their rank and racetime, being their best blue race and their best red race combined. I have read the file in and output it to the new file, but cannot figure out how to sort the file.
If someone could help me out in the slightest it would be greatly appreciated.
Here is the code I have so far (some of the code I have does not compile so I have commented it out):
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
struct Racer_struct
{
int bib;
char sex;
char fname[30];
char lname[30];
double b1, b2, r1, r2;
};
bool connectInFile(ifstream& fin, char infilename[]);
bool connectOutFile(ofstream& fout, char outfilename[]);
void readData(ifstream& fin, Racer_struct racers[], const int& MAX);
//void racerGender(ostream& fout, Racer_struct racers[], const int& MAX);
//double calcTotalTime(Racer_struct racers[], double total[], const int& MAX);
void writeData(ostream& fout, Racer_struct racers[], const int& MAX);
int main()
{
const int MAX = 38;
Racer_struct racers[MAX];
// double total[MAX];
ifstream fin;
ofstream fout;
char in_file[30], out_file[30];
bool opened;
char title[79];
opened = connectInFile(fin, in_file);
cout << opened << endl;
opened = connectOutFile(fout, out_file);
cout << opened << endl;
if(opened)
{
cout << "CONNECTED to: " << in_file << endl;
cout << "WRITING to: " << out_file << endl;
for(int i=0; i<=3; i++)
{
fin.getline(title, 80);
fout << title << "\n";
}
}
readData(fin, racers, MAX);
writeData(fout, racers, MAX);
fin.close();
fout.close();
cout << endl;
return 0;
}
bool connectInFile(ifstream& fin, char infilename[])
{
bool success = true;
cout << "Enter input filename: ";
cin >> infilename;
fin.open(infilename);
if(fin.fail())
success = false;
return success;
}
bool connectOutFile(ofstream& fout, char outfilename[])
{
bool opened = true;
cout << "Enter the filename you wish to write to: ";
cin >> outfilename;
fout.open(outfilename);
if(fout.fail())
opened = false;
return opened;
}
void readData(ifstream& fin, Racer_struct racers[], const int& MAX)
{
char ws;
for(int i=0; i<MAX && fin.peek()!= EOF; i++)
{
fin >> racers[i].bib >> racers[i].sex >> racers[i].fname >> racers[i].lname
>> racers[i].b1 >> racers[i].b2 >> racers[i].r1 >> racers[i].r2;
fin.get(ws);
}
}
/*
void racerGender(ostream& fout, Racer_struct racers[], const int& MAX)
{
for(int i=0; i<MAX; i++)
if(racers[i].sex == 'M')
{
calcTotalTime(racers, total, MAX);
writeData(fout, racers, MAX);
}
else
{
calcTotalTime(racers, total, MAX);
writeData(fout, racers, MAX);
}
}
double calcTotalTime(Racer_struct racers[], double total[], const int& MAX)
{
double total[MAX];
for(int i=0; i<MAX; i++)
if(racers[i].r1 > racers[i].r2 && racers[i].b1 > racers[i].b2)
total[i] = racers[i].r2 + racers[i].b2;
else if(racers[i].r2 > racers[i].r1 && racers[i].b2 > racers[i].b1)
total[i] = racers[i].r1 + racers[i].b1;
else if(racers[i].r1 > racers[i].r2 && racers[i].b2 > racers[i].b1)
total[i] = racers[i].r2 + racers[i].b1;
else
total[i] = racers[i].b2 + racers[i].r1;
return total[i];
}
*/
void writeData(ostream& fout, Racer_struct racers[], const int& MAX)
{
for(int i=0; i<MAX; i++)
{
fout << racers[i].bib << "\t" << racers[i].sex << "\t" << racers[i].fname
<< "\t" << racers[i].lname << "\t" << racers[i].b1 << "\t" << racers[i].b2
<< "\t" << racers[i].r1 << "\t" << racers[i].r2 /*<< "\t" << total[i]*/ << endl;
/* if((i+1)%5)
fout << "\t";
else
fout << endl;
*/
}
}