Sorting information from array with Operator Overlaoding

I have a program in which I take information from a csv, store it in an array, and display it.

I want to display the information a second time but this time I wanted the output sorted via operator overloading and sorted to display it alphabetically from a to z based on ProductName. ( Yes this is an asssignment, but we all learn somehow).

It does not work for some reason.

Any input what I am missing? Help appreciated.

( And I have to use arrays and operator overloading for it:

Sort the Products alphabetically based on the Product Name. Use relational operator overloading within the product class for this

)


*.cpp:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Inventory Report.cpp
//
// Purpose: Write a program that 
//          1. 
//          2. 
//          3. 
//          4. 
//
// Author: Anonymous
// Reference: http://www.cplusplus.com
// Date: 23 July 2011

#include <iostream>		  // header file for console I/O
#include <iomanip>		  // header file for stream manipulators
#include <string>		  // header file used to manipulate and process strings and arrays
#include <fstream>		  // header file used to read data from a file
#include "Product.h"	  // header file for class and function declarations
using namespace std;	  // instructs compiler to use names in standard library


/********************************* main() *******************************/
int main()
{
	// Display author name to the right, title of program, and dashes
	cout << right;
	cout << setw(60) << "Anonymous" << endl;
	cout << "Product Inventory Report - Sort by Product" << endl;
	cout << setfill ('-') << setw(42) << "-" << endl;
	cout << endl << endl;			// extra lines (for legability)

	// Declaration Section
	int arraySize=0;
	char *endp;

	Product inventory[10];


	// Open the file for input
	cout << "Products READ FROM FILE" << endl;
	cout << setfill ('-') << setw(23) << "-" << endl;
	cout << endl << endl;					// extra lines (for legability)

	ifstream inFile("inventory.csv");	// open file stream for input
	
	if (inFile.fail())
	{
		cout << "ERROR opening data file." << endl;
		return -1;
	}

	// read the contents of the file into an array of Product objects
	while (!inFile.eof())
	{
		// input Product record data from file
			getline(inFile, sku, ',');
			getline(inFile, product, ',');
			getline(inFile, category, ',');
			getline(inFile, unitPrice, ',');
			getline(inFile, onHand, ',');
			getline(inFile, ReorderLevel, ',');
			getline(inFile, ReorderLeadTime);
			
			inventory[arraySize].setProductInfo(sku, product, category,strtod(unitPrice.c_str(),&endp),
				atoi(onHand.c_str()), atoi(ReorderLevel.c_str()), atoi(ReorderLeadTime.c_str()));

			++arraySize;
		
	}

	inFile.close(); //close the file

	// Products array header
	cout << left << setfill (' ');
	cout << setw( 8 ) << "SKU #" << setw( 30 ) << "Product Name" << setw( 12 ) << "Category" << setw( 9 ) << "On Hand" << setw( 12 ) << "Unit Price" << setw( 10 ) << "Inv Value" << setw( 10 ) << endl;
	
	// Display ProductInfo
	for (int i=0; i < arraySize; i++)
		inventory[i].displayProductInfo();
		/*experiemntal > */
	cout << endl << endl << endl;	

	cout<< product<<endl;
	/*< experimental*/
	cout << endl << endl << endl;			// extra lines (for legability)

	// Sort the Products alphabetically based on the Product Name
	cout << "Products SORTED BY Product Name" << endl;
	cout << setfill ('-') << setw(31) << "-" << endl;
	cout << endl << endl;			// extra lines (for legability)

	// Products array header
	cout << left << setfill (' ');
	cout << setw( 8 ) << "SKU #" << setw( 30 ) << "Product Name" << setw( 12 ) << "Category" << setw( 9 ) << "On Hand" << setw( 12 ) << "Unit Price" << setw( 10 ) << "Inv Value" << setw( 10 ) << endl;

	// Display sorted products

	/* Test Output Beginning */
			for (int j=0; j < arraySize; j++)
		
			

			inventory[j].displayProductInfo();
	/* Test Output End*/

		cout << endl << endl << endl;

	system("pause");
	return 0;
}


*.h

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Product.h

#ifndef PRODUCT_H
#define PRODUCT_H
#include <cmath>
#include <string>		  // header file used to manipulate and process strings and arrays
using namespace std;	  // instructs compiler to use names in standard library



	string sku;
	string product;
	string category;
	string unitPrice;
	string onHand;
	string ReorderLevel;
	string ReorderLeadTime;

	

	// temp variables used to input Product data from file
	

class Product
{
private:
	string SKU; // SKU
	string ProductName; // ProductName
	string category; // Category
	double unitPrice100; // UnitPrice
	int onHand; // OnHand
	int reorderLevel; // ReorderLevel
	int reorderLeadTime; // ReorderLeadTime

public:
	Product();
	Product(string sku, string product, string Category, double UnitPrice, int OnHand, int ReorderLevel, int ReorderLeadTime);

	void displayProductInfo();

	void setProductInfo(string sku, string product, string Category,
		double UnitPrice, int OnHand, int ReorderLevel,
		int ReorderLeadTime);

	string getProductName();		// for future alphabetic ordering

	bool operator < (Product & aProduct); 
	bool operator > (Product & aProduct);
};

/********************************* Implementation  *******************************/


Product::Product()
{
	// no arg Constructor
}

Product::Product(string sku, string product, string Category,
double UnitPrice, int OnHand, int ReorderLevel,
int ReorderLeadTime)
{
	SKU = sku;
	ProductName = product;
	category = Category;
	unitPrice100 = UnitPrice;
	onHand = OnHand;
	reorderLevel= ReorderLevel;
	reorderLeadTime = ReorderLeadTime;
}

void Product::setProductInfo(string sku, string product, string Category,
double UnitPrice, int OnHand, int ReorderLevel,
int ReorderLeadTime)
{
	SKU = sku;
	ProductName = product;
	category = Category;
	unitPrice100 = UnitPrice;
	onHand = OnHand;
	reorderLevel= ReorderLevel;
	reorderLeadTime = ReorderLeadTime;
}

void Product::displayProductInfo()
{
	cout << left << setw( 8 ) << SKU << setw( 30 ) << ProductName << setw( 11 ) << category << right << setw( 8 ) << onHand << setw( 11 ) << unitPrice100  << setw( 10 ) << fixed << setprecision( 2 ) << onHand*unitPrice100 << endl;
}

string Product::getProductName()
{
	return ProductName;
}

bool Product::operator < (Product & aProduct)
{
 return this-> ProductName < aProduct.ProductName;
}

bool Product::operator > (Product & aProduct)
{
 return this-> ProductName < aProduct.ProductName;
}



#endif 


*.csv

00-101,Dharamsala Tea,Beverages,18.95,11,10,10
00-102,Tibetan Barley Beer,Beverages,19.08,10,25,21
00-103,Aniseed Syrup,Condiments,10.21,8,5,10
00-104,Chef Anton's Cajun Seasoning,Condiments,22,12,10,14
00-105,Chef Anton's Gumbo Mix,Condiments,21.35,10,0,14
00-106,1st Step Multi-vitamin,Supplement,15.95,18,15,7
00-107,Horizon Organic Milk 2%,Beverages,3.89,6,20,2
00-108,Glyco-Maize,Supplement,22.49,10,6,7
00-109,Red Lentils (Hyderabad),Dry Goods,4.19,12,10,18



Any help is appreciated.

Regards,

Mark
Your code doesn't have a call to a sort function of any kind. If you are wondering why it doesn't sort, well there you go. Does the assignment specify the sort algorithm to use?
webJose,

I inserted a sort function now that I used previously and still cannot find what is wrong. Any ideas?

header:
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Product.h

#ifndef PRODUCT_H
#define PRODUCT_H
#include <cmath>
#include <string>		  // header file used to manipulate and process strings and arrays
using namespace std;	  // instructs compiler to use names in standard library



	string sku;
	string product;
	string category;
	string unitPrice;
	string onHand;
	string ReorderLevel;
	string ReorderLeadTime;

	

	// temp variables used to input Product data from file
	

class Product
{
private:
	string SKU; // SKU
	string ProductName; // ProductName
	string category; // Category
	double unitPrice100; // UnitPrice
	int onHand; // OnHand
	int reorderLevel; // ReorderLevel
	int reorderLeadTime; // ReorderLeadTime

public:
	Product();
	Product(string sku, string product, string Category, double UnitPrice, int OnHand, int ReorderLevel, int ReorderLeadTime);

	void displayProductInfo();

	void setProductInfo(string sku, string product, string Category,
		double UnitPrice, int OnHand, int ReorderLevel,
		int ReorderLeadTime);

	string getProductName();		// for future alphabetic ordering

	bool operator < (Product & aProduct); 
	bool operator > (Product & aProduct);
};

/********************************* Implementation  *******************************/


Product::Product()
{
	// no arg Constructor
}

Product::Product(string sku, string product, string Category,
double UnitPrice, int OnHand, int ReorderLevel,
int ReorderLeadTime)
{
	SKU = sku;
	ProductName = product;
	category = Category;
	unitPrice100 = UnitPrice;
	onHand = OnHand;
	reorderLevel= ReorderLevel;
	reorderLeadTime = ReorderLeadTime;
}

void Product::setProductInfo(string sku, string product, string Category,
double UnitPrice, int OnHand, int ReorderLevel,
int ReorderLeadTime)
{
	SKU = sku;
	ProductName = product;
	category = Category;
	unitPrice100 = UnitPrice;
	onHand = OnHand;
	reorderLevel= ReorderLevel;
	reorderLeadTime = ReorderLeadTime;
}

void Product::displayProductInfo()
{
	cout << left << setw( 8 ) << SKU << setw( 30 ) << ProductName << setw( 11 ) << category << right << setw( 8 ) << onHand << setw( 11 ) << unitPrice100  << setw( 10 ) << fixed << setprecision( 2 ) << onHand*unitPrice100 << endl;
}

string Product::getProductName()
{
	return ProductName;
}

template<typename Tvar>
void sort(Tvar list[], int listSize)
{
	for (int i = 0; i < listSize; i++)
	{
		// Find the minimum in the list[i..listSize-1]
		Tvar currentMin = list[i];
		int currentMinIndex = i;

		for (int j = i + 1; j < listSize; j++)
		{
			if (currentMin > list[j])
			{
				currentMin = list[j];
				currentMinIndex = j;
			}
		}

		// Swap list[i] with list[currentMinIndex] if necessary
		if (currentMinIndex != i)
		{
			list[currentMinIndex] = list[i];
			list[i] = currentMin;
		}
	}
}

bool Product::operator < (Product & aProduct)
{
 return this-> ProductName < aProduct.ProductName;
}

bool Product::operator > (Product & aProduct)
{
 return this-> ProductName < aProduct.ProductName;
}



#endif 



cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Inventory Report.cpp
//
// Purpose: Write a program that 
//          1. 
//          2. 
//          3. 
//          4. 
//
// Author: Anonymous
// Reference: http://www.cplusplus.com
// Date: 23 July 2011

#include <iostream>		  // header file for console I/O
#include <iomanip>		  // header file for stream manipulators
#include <string>		  // header file used to manipulate and process strings and arrays
#include <fstream>		  // header file used to read data from a file
#include "Product.h"	  // header file for class and function declarations
using namespace std;	  // instructs compiler to use names in standard library


/********************************* main() *******************************/
int main()
{
	// Display author name to the right, title of program, and dashes
	cout << right;
	cout << setw(60) << "Anonymous" << endl;
	cout << "Product Inventory Report - Sort by Product" << endl;
	cout << setfill ('-') << setw(42) << "-" << endl;
	cout << endl << endl;			// extra lines (for legability)

	// Declaration Section
	int arraySize=0;
	char *endp;

	Product inventory[10];


	// Open the file for input
	cout << "Products READ FROM FILE" << endl;
	cout << setfill ('-') << setw(23) << "-" << endl;
	cout << endl << endl;					// extra lines (for legability)

	ifstream inFile("inventory.csv");	// open file stream for input
	
	if (inFile.fail())
	{
		cout << "ERROR opening data file." << endl;
		return -1;
	}

	// read the contents of the file into an array of Product objects
	while (!inFile.eof())
	{
		// input Product record data from file
			getline(inFile, sku, ',');
			getline(inFile, product, ',');
			getline(inFile, category, ',');
			getline(inFile, unitPrice, ',');
			getline(inFile, onHand, ',');
			getline(inFile, ReorderLevel, ',');
			getline(inFile, ReorderLeadTime);
			
			inventory[arraySize].setProductInfo(sku, product, category,strtod(unitPrice.c_str(),&endp),
				atoi(onHand.c_str()), atoi(ReorderLevel.c_str()), atoi(ReorderLeadTime.c_str()));

			++arraySize;
		
	}

	inFile.close(); //close the file

	// Products array header
	cout << left << setfill (' ');
	cout << setw( 8 ) << "SKU #" << setw( 30 ) << "Product Name" << setw( 12 ) << "Category" << setw( 9 ) << "On Hand" << setw( 12 ) << "Unit Price" << setw( 10 ) << "Inv Value" << setw( 10 ) << endl;
	
	// Display ProductInfo
	for (int i=0; i < arraySize; i++)
		inventory[i].displayProductInfo();
		/*experiemntal > */
	cout << endl << endl << endl;	

	cout<< product<<endl;
	/*< experimental*/
	cout << endl << endl << endl;			// extra lines (for legability)

	// Sort the Products alphabetically based on the Product Name
	cout << "Products SORTED BY Product Name" << endl;
	cout << setfill ('-') << setw(31) << "-" << endl;
	cout << endl << endl;			// extra lines (for legability)

	// Products array header
	cout << left << setfill (' ');
	cout << setw( 8 ) << "SKU #" << setw( 30 ) << "Product Name" << setw( 12 ) << "Category" << setw( 9 ) << "On Hand" << setw( 12 ) << "Unit Price" << setw( 10 ) << "Inv Value" << setw( 10 ) << endl;

	// Display sorted products

	/* Test Output Beginning */
			sort(inventory, arraySize);
			inventory[].displayProductInfo();
	/* Test Output End*/

		cout << endl << endl << endl;

	system("pause");
	return 0;
}


I would appreciate any help with. It is supposed to be sorted alphabetically (ProductName).

Regards,

Mark
Topic archived. No new replies allowed.