I almost have it working however, it is seperating the last names that start in caps and the ones that do not.
Example file
LastName FirstName DaysofRental BalanceDue
Smith Joe 15 100.50
Doe John 10 95.20
Anderson Paul 30 20.00
O'Donell Miriam 10 24.30
Foster Sam 30 15.00
Zom Pete 10 20.00
Mock Chilly 100 30
smitty Chris 200 200
xu Conor 1 200
anilo steve 0 0
What "Sorted" file is outputing
LastName FirstName DaysofRental BalanceDue
Anderson Paul 30 $20.00
Doe John 10 $95.20
Foster Sam 30 $15.00
Mock Chilly 100 $30.00
O'Donell Miriam 10 $24.30
Smith Joe 15 $100.50
Zom Pete 10 $20.00
anilo steve 0 $0.00
smitty Chris 200 $200.00
xu Conor 1 $200.00
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string.h>
using namespace std;
const int STRINGSIZE = 30;
const int LISTSIZE = 10;
const int HEADSIZE = 4;
typedef char STRING30[STRINGSIZE];
typedef STRING30 NAMES[LISTSIZE];
int main(int argc, char** argv)
{
ofstream outfile;
outfile.open("sorted.txt");
int count,
count2,
mindex;
int rdays[LISTSIZE],
hrdays;
double baldue[LISTSIZE],
totalbaldue,
hbaldue,
tempnum;
NAMES first,
last,
header;
STRING30 mname,
tempname;
ifstream in;
in.open("invoice1_test1.txt");
//Input Section
if(in.is_open())
{
in >> header[0]
>> header[1]
>> header[2]
>> header[3];
int count = 0;
while(!in.eof())
{
in >> last [count]
>> first[count]
>> rdays[count]
>> baldue[count];
count++;
}
in.close();
}
else
{
cout << "File failed to open" << endl;
}
for(count = 0; count < LISTSIZE; count++)
{
mindex = count;
strcpy(mname, last[count]);
for(count2 = count; count2 < LISTSIZE; count2++)
{
if(strcmp(last[count2], mname) == -1)
{
mindex = count2;
strcpy(mname, last[count2]);
}
}
strcpy(tempname, last[count]);
strcpy(last[count], mname);
strcpy(last[mindex], tempname);
strcpy(tempname, first[count]);
strcpy(first[count], first[mindex]);
strcpy(first[mindex], tempname);
tempnum = rdays[count];
rdays[count]= rdays[mindex];
rdays[mindex]= tempnum;
tempnum = baldue[count];
baldue[count] = baldue[mindex];
baldue[mindex] = tempnum;
}
outfile << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
outfile << left << setw(20) << header[0] << setw(20) << header[1] << setw(20) << header[2] << setw(20) << header[3] << endl;
for(int count = 0; count<LISTSIZE; count++)
{
outfile << left << setw(20) << last[count] << setw(20) << first[count] << setw(20) << rdays[count] << "$" << setw(20) << baldue[count] << endl;
}
Use if (strcmpi(last[count2], mname) < 0);
for the comparison, instead of if (strcmp(last[count2], mname) ==-1);
strcmpi()
functions same as strcmp()
but it is not case sensitive.
Also add exit(1)
if ifstream in
fails to open.