Sorting help

May 7, 2014 at 8:53pm
I have a class set up to take in an item name, weight, and cost. I want to be able to sort the output different ways. Bubble sort the weight, Selection Sort by name, and Insertion sort by cost. I have no idea where to go now and I could use some help with this.

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
#include <iostream>
#include <iomanip>
#include <conio.h>
#include "Inventory.h"
#define MAX_REC 10

using namespace std;

//Class to hold inventory variables/funcitons
class Inventory{
private:

	char itemName[15];
	double Cost;
	float Weight;

public:
	
	void getData();
	void showData();
	
};

//Gathers data from user to populate the inventory
void Inventory :: getData(){
	cout << "\nEnter Item Name: ";
	cin >> itemName;
	cout << "Enter Cost: ";
	cin >> Cost;
	cout << "Enter Weight: ";
	cin >> Weight;
};


void Inventory :: showData(){
	cout << endl;
	cout.width(15);
	cout.setf(ios::left, ios::adjustfield);
	cout << itemName;

	cout.width(8);
	cout << Cost;

	cout.width(15);
	cout.setf(ios::right, ios::adjustfield);
	cout.setf(ios::showpoint);
	cout.setf(ios::fixed, ios::floatfield);
	cout.precision(2);
	cout << Weight;

};

int main(){
	Inventory record[MAX_REC];
	int i, n;
	//clrscr();

	cout << "\n=====Inventory Management=====\n";
	cout << "\nHow many Records to be created : ";
	cin >> n;

	cout << "Enter " << n << " Records\n";
	for(i=0;i<n;i++)
		record[i].getData();

	cout<<"\n\n---Stock Information---\n";
    cout<<"\n"<<setw(8)<<"Item Name"
        <<setw(10)<<"Cost"
        <<setw(19)<<"Weight"<<endl;
    cout<<"-------------------------------------------";

    for(i=0;i<n;i++)
        record[i].showData();

    _getch(); 

}
May 7, 2014 at 9:23pm
So I'm assuming I need to do an array for this then?


Edit: I guess I should rephrase this. My confusion is whether or not I can implement those sorts into my current code or if it needs to be changed to be able to use it?
Last edited on May 7, 2014 at 9:30pm
May 7, 2014 at 9:39pm
you've already made an array
Inventory record[MAX_REC];

but the access to each object's data members needs to be performed using class functions, since they're private - record[0].itemName will be forbidden

create get functions for your class members
Last edited on May 7, 2014 at 9:40pm
May 7, 2014 at 9:59pm
Say for the sake of just making it easier to use record[0].itemname, could I make the variables public?


Edit: Stupid question because I know I can. Just poor coding style
Last edited on May 7, 2014 at 10:10pm
May 8, 2014 at 12:09am
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
	////Bubble Sort Code////
	int tempHolder = -1;
	int end = 10;
	int length = 10;

	for(int counter = length - 1; counter > 0; counter--)
	{
		for(int index = 0; index < end; index++)
		{
			if (record[weight] > record[index + 1])
			{
				tempHolder = record[index +1];
				record[index + 1] = record;
				record[index] = tempHolder;
			}
		}

		for(int index = 0; index < 10; index++)
		{
			cout << record[index] << ", ";
		}

		cout << endl;

		end--;
	}

	for(int index = 0; index < 10; index++)
	{
		cout << record[index] << ", ";
	}
	
	cout << endl;
	//////////////////////// 
May 8, 2014 at 12:10am
I think I'm doing it along the lines of a pre-declared array (or one not based out of a class/functions.
May 8, 2014 at 12:13am
also I should note that I changed the class a little bit since OP


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Class to hold inventory variables/funcitons
class Inventory{
private:

public:
	
	//Placing these here to see if method will work//
	char itemName[15];
	int Cost;
	int Weight;
	/////////////////////////////////////////////////

	void getData();
	void showData();
	
};
May 9, 2014 at 4:59am
Figured out the sorting code.
Topic archived. No new replies allowed.