Begineer - Needs Help - printing and formating issues..


" 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


"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;
	}	

}



I'm also new to sorting and writing a program with an array of struct records,

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 reading and looking up info online but not quite getting there. modified my code a few 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 ---------------------- */
A few things.

The declaration of the "list" array should probably be done outside the definition of the struct, as you had done previously. You don't really want a static array...

CustomerRec list[MAX_CUSTOMERS]; // array named: "list" to hold customer records.

Secondly, you are trying to pass "list" (which is of type CustomerRec[]) when SSort is expecting a parameter of type double[]. You're also not passing it properly, you're passing element 25 of the array, which does not exist (0-24 are the elements in the array).

I also am not sure what the ElementNums is inside the struct for, it needs to be outside the struct. You can keep it as a global variable if you want but preferably make it local to main() function. Initialize it to 0 and every time you read a customer into the "list" array, increment it by 1.

You're also going to have to change your SSort function because you can't just say things like:

if (a[i] < a[minplace])

because a[i] is now a CustomerRec object, which is not something that can be compared using standard comparison operators. So you're going to have to add logic to work with the different cases, i.e sorting by zip, sorting by balance, etc. I would suggest passing another variable to SSort, type string, with values like "ZIP" and "BALANCE" and then using a case (or if/else) statement to determine which logic to use. Use comparisons like:

if (a[i].zipcode < a[minplace].zipcode)

Also, your sort algorithm is wrong. It should be this, found from Wikipedia:
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
/* a[0] to a[n-1] is the array to sort */
int iPos;
int iMin;
 
/* advance the position through the entire array */
/*   (could do iPos < n-1 because single element is also min element) */
for (iPos = 0; iPos < n; iPos++) {
    /* find the min element in the unsorted a[iPos .. n-1] */
 
    /* assume the min is the first element */
    iMin = iPos;
    /* test against all other elements */
    for (i = iPos+1; i < n; i++) {
        /* if this element is less, then it is the new minimum */  
        if (a[i] < a[iMin]) {
            /* found new minimum; remember its index */
            iMin = i;
        }
    }
 
    /* iMin is the index of the minimum element. Swap it with the current position */
    if ( iMin != iPos ) {
        swap(a, iPos, iMin);
    }
}


Your first loop should go from pass=0 to pass=size, not pass=size-1. Your swapping should also only take place if minplace != pass.

Sooo...

Change the SSort function header to:

void SSort (CustomerRec a[ ], int size, string sortType);

Change the SSort function call to:

SSort(list, elementNums, "ZIP" /*or "BALANCE"*/);

As for your output, it's been like 5 years since I've done formatting like this but I think you can just increase "setw(7)" to something like "setw(15)" and it should right justify. You'll have to extend the other lines though. Either that or decrease the "setw(30)" for the last name.

I know this is a lot, tell me if you're confused by something.
Last edited on
thank you for responding.. yes you were right i did have to move the declaration of list. the sort call worked good and unfortunately i did have rewrite the whole sort functions.

here is my new calls and sort function (desending order) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

//sorts array in desending order for the Amounts.txt document
	SSort(list, record);


void SSort(CustomerRec list[], CustomerRec& record) 
{
	for(int i=record.ElementNums; i>0; i--)
	{
		for(int j=0; j<i-1; j++)
		{
			if ( list[j].outstandingbalance < list[j+1].outstandingbalance )
			{
				CustomerRec temp = default_Rec;
				temp = list[j];
				list[j] = list[j+1];
				list[j+1] = temp;
			}
		}
	}
}




but if there is any other advise you can offer on formatting the output text it would be great.. still having issues..


HERE IS MY NEW OUTPUT:



        NAME                             AMOUNT OWED
     Mark Otter                            1217.19 
     John Smith                             429.52 
     David Bender                            373.50 
     Tim Bud                                 0.00 
                                           ------- 
                                 TOTAL     2020.21









Last edited on
Topic archived. No new replies allowed.