Reputation: 1
I am having some trouble with this program I am writing. I believe that I need to overload the > operator but I am not sure how to do so. Can someone shed some light on this for me?
My code so far (incomplete):
#include <iostream>
#include <fstream> //Includes the file stream for inputting and outputting files.
#include <string> //Fixes the error i get with some operands.
#include <iomanip> //Helps with report formatting.
using namespace std;
void sort(record A[], int size, int sortField);
struct record
{
string CustId, SPID, CustLN, CustFN;
int Q1;
double P1;
int Q2;
double P2;
int Q3;
double P3;
string LOD;
bool ShipRec;
string NCD, PreMethod;
double TotalSales;
};
int main()
{
ifstream InFile("master.txt");
ofstream OutFile1("report1.txt");
ofstream OutFile2("report2.txt");
ofstream OutFile3("report3.txt");
ofstream OutFile11("report11.txt");
ofstream OutFile13("report13.txt");
ofstream OutFile15("report15.txt");
record Customer[100];
while (!InFile.eof())
{
int i = 0;
InFile >> Customer[i].CustId >> Customer[i].SPID >> Customer[i].CustLN >> Customer[i].CustFN >> Customer[i].Q1 >> Customer[i].P1 >> Customer[i].Q2 >> Customer[i].P2 >> Customer[i].Q3 >> Customer[i].P3 >> Customer[i].LOD >> Customer[i].ShipRec >> Customer[i].NCD >> Customer[i].PreMethod;
Customer[i].TotalSales = (Customer[i].Q1 * Customer[i].P1) + (Customer[i].Q2 * Customer[i].P2) + (Customer[i].Q3 * Customer[i].P3);
}
}
void sort(record A[], int size, int sortField)
{
if (sortField = 1)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
else if (sortField = 2)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
else if (sortField = 3)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
else if (sortField = 11)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
else if (sortField = 13)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
else if (sortField = 15)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
}
Upvotes: 0
Views: 212
Reputation: 67812
The sort field is an argument to your function, so you can't write a regular operator<
, because it won't be able to see the value of that argument. (Unless you make it global. Don't do that).
Assuming there isn't a special reason why you have to write your own O(n2) algorithm, the simplest solution is:
class SortByField {
int sortField_;
public:
explicit SortByField(int f) : sortField_(f) {}
// default copy ctor is fine
bool operator() (record const &a, record const &b) const;
};
void sort(record A[], int size, int sortField) {
std::sort(A, A+size, SortByField(sortField));
}
now, all that remains is to write that function call operator. I'm guessing it should look something like this:
bool SortByField::operator() (record const &a, record const &b) const {
switch (sortField_) {
case 1: return a.CustID < b.CustID;
case 2: return a.SPID < b.SPID;
// ...
default: return false; // maybe we should check sortField is valid before this?
}
}
Upvotes: 1
Reputation: 803
I assume that sortField
chooses which field to compare. If that's the case you should change your code to:
if (sortField == 1) // <-- use '==' for comparisson
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size-1; j++) // <-- should probably be size-1
{
if (A[j].CustId > A[j + 1].CustId) // <-- test the sortField
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
and similarly for other sortField
values
Upvotes: 0