" Purpose of Program: I am trying to
sort the array of struct Customer Rec data
by the .outstandingBalance element and then trying to print out into a txt file with the function below: "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void PrintCustomerAmounts(ofstream& outfile2, CustomerRec& record)
{
double TotalAmountOwed = 0.00;
//print out NAME, and Amount Owed
outfile2 << setw(12) << "NAME" << setw(40) << "AMOUNT OWED" << endl;
for (int count = 0; count < record.ElementNums; count++)
{
outfile2 << setw(5) << " " << list[count].firstname << " "
<< left << setw(30) << list[count].lastname << left << setw(3) << " "
<< setprecision(2) << fixed << setw(7) << right << list[count].outstandingbalance << " " << endl;
}
// then calculate a total output..
for (int k = 0; k < record.ElementNums; k++)
{
TotalAmountOwed = TotalAmountOwed + list[k].outstandingbalance;
}
outfile2 << setw(51) << " ------- " << endl;
outfile2 << setw(38) << "TOTAL" << setw(12) << TotalAmountOwed << endl;
}
|
HERE IS THE OUTPUT I AM GETTING FROM MY FUNCTION:
NAME AMOUNT OWED
John Smith 429.52
Mark Otter 1217.19
David Bender 373.50
Tim Bud 0.00
-------
TOTAL 2020.21
|
" I am
having formatting issues with my output text too
somewhere in the function,
void PrintCustomerAmounts
and I am not sure how to fix that either.. "
MY OUTPUT SHOULD LOOK LIKE THIS;
NAME AMOUNT OWED
Mark Otter 1217.19
John Smith 429.52
David Bender 373.50
Tim Bud 0.00
-------
TOTAL 2020.21
|
My other Problem: i'm trying to figure out how to pass the array 'List' into the void function 'SSort' so that i can sort the elements in the struct CustomerRec either by
list[].zipcode or by list[].outstandingBalance.
I've been trying different things and reading online but not quite getting there. modified my code a several times and still not able to find a way to pass my array of structs into the sort function correctly.
I originally had this array inside its own struct called CustomerData like this:
1 2 3 4 5 6 7
|
struct CustomerData
{
CustomerRec list[MAX_CUSTOMERS];
int ElementNums;
}
|
then I took it out of that struct placed it by itself and moved ElementNums into CustomerRec:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
struct CustomerRec
{
string firstname;
string lastname;
int housenumber;
string streetname;
string streetdesignation;
string city;
string state;
int zipcode;
double outstandingbalance;
string transactionType;
int ElementNums;
};
CustomerRec list[MAX_CUSTOMERS]; // array named: "list" to hold customer records.
|
now I have it setup like my code below. It still feels like I am just missing something thats right there in front of me...
Any help with figuring this out would be awesome!!!
I highlighted what I believe to be the key code in
bold and I believe this is where I am having the problems ...
here is the relevant code that i have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int MAX_CUSTOMERS = 25; //max number of entries in ARRAY
struct CustomerRec
{
string firstname;
string lastname;
int housenumber;
string streetname;
string streetdesignation;
string city;
string state;
int zipcode;
double outstandingbalance;
string transactionType;
int ElementNums;
}list[MAX_CUSTOMERS]; // array named: "list" to hold customer records
void ReadCustomers(ifstream& infile1, CustomerRec& record);
void ReadTransactions(ifstream& infile2, CustomerRec& record);
void CalcInterestNoTrans(CustomerRec& record);
void SSort(double a[ ], int size)
void PrintCustomerData(ofstream& outfile1, CustomerRec& record);
void PrintCustomerAmounts(ofstream& outfile2,CustomerRec& record);
void PrintCustomerMailings(ofstream& outfile3, CustomerRec& record);
/* this section is my function calls that I have listed in: Main */
CustomerRec record;
record.ElementNums = 0;
//calls functions to read in files
ReadCustomers(infile, record);
ReadTransactions(infile2, record);
//prints out customer records before calculating interest
// on non trasnsactioned records to "thisMonth.txt" file
PrintCustomerData(outfile, record);
//searches array LIST and indentifies records that had
// no transactions then charges 1.5% interest to outstandingBalance
CalcInterestNoTrans(record);
SSort(list[MAX_CUSTOMERS], record.ElementNums);
//prints out customer names with new amounts owed
// to output file "amounts.txt"
PrintCustomerAmounts(outfile2, record);
//prints out customer and address info
// to output file "mailings.txt"
PrintCustomerMailings(outfile3, record);
/* -------------------------- end of main section ---------------------- */
|
"HERE IS MY SORT FUNCTION CODE:"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void SSort (double a[ ], int size)
{
double temp; // temp place to store element during swap
int minplace; // index of currently smallest element
// for each place in array
for (int pass = 0; pass < size-1;pass++)
{
minplace = pass; // assume minimum already at index to fill
for (int i = pass+1; i < size; i++) // look through rest of array for smaller elt
{
if (a[i] < a[ minplace]) // if found a smaller one
minplace = i; // keep its index as the smallest so far
}
temp = a[pass]; // swap current value with minimum value
a[pass] = a [minplace];
a[minplace] = temp;
}
}
|