Bubble sort with Char Pointers

I'm a bit stuck. I need this program use character pointers to display the information that the user chooses from a grocery list in a .txt file. I need it to bubble sort in alphabetical order but I'm having a hard time finding my mistakes as it just crashes and breaks.

Here's my .h file for Grocerys
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
//Header file which contains class (GroceryItem) definition

#include<iostream>
#include<string>
using namespace std;

#ifndef GROCERYITEM_H
#define GROCERYITEM_H

class GroceryItem

{
public:
	GroceryItem();//default constructor
	~GroceryItem(); // Destructor
	GroceryItem(const GroceryItem&); // Copy Constructor
	int operator == (GroceryItem&); // Overloaded Equality operator
	GroceryItem& operator = (GroceryItem&); // Overloaded Assignment
	int operator < (GroceryItem&);  // Overloaded < operator 
	int operator > (GroceryItem&);  // Overloaded > operator 
	
	void set_item_name(char* = " ");
	void set_item_price(double = 0.0 );
	void set_qty_on_hand(int = 0);
	void set_qty_purchased(int = 0 );
	char* get_item_name();
	double get_item_price();
	int get_qty_on_hand();
	int get_qty_purchased();
private:
	char * item_name; 
	double item_price;
	int qty_on_hand;
	int qty_purchased;
};

#endif


Here's my .cpp file for groceries.
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
135
136
137
138
139
140
141
142
143
144
145
146
147
//Member-functions for the GroceryItem class

#include<iostream>
#include "Grocery (2).h"
#include<string>
using namespace std;


GroceryItem::GroceryItem()
{	
	
	item_name = new char[strlen(" ") +1];
	strcpy(item_name, " ");
	item_price = 0.0;
	qty_on_hand = 0;
	qty_purchased = 0;
}


GroceryItem::~GroceryItem()
{
	delete [] item_name;
}


GroceryItem::GroceryItem(const GroceryItem& grocery_in)
{	
	item_name = new char[strlen(grocery_in.item_name) + 1];
	strcpy(item_name, grocery_in.item_name);
	item_price = grocery_in.item_price; 
	qty_on_hand = grocery_in.qty_on_hand;
	qty_purchased = grocery_in.qty_purchased;
}


//overload equality operator
int GroceryItem::operator == (GroceryItem& compare)
{
	if( strcmp(item_name, compare.item_name)==0)
		return 1;
	else
		return 0; 
}


//overloaded < operator
int GroceryItem::operator < (GroceryItem& compare)
{
	if( strcmp(item_name, compare.item_name) < 0)
		return 1; 
	else
		return 0; 
}

//overloaded > operator
int GroceryItem::operator > (GroceryItem& compare)
{
	if( strcmp(item_name, compare.item_name) > 0)
		return 1; 
	else
		return 0; 
}


GroceryItem& GroceryItem::operator = (GroceryItem& compare)
{
	if(this == &compare)
		return *this;
	else
		{
			delete[] item_name;
			strcpy(item_name, compare.item_name);
			item_price = compare.item_price;
			qty_on_hand = compare.qty_on_hand;
			qty_purchased = compare.qty_purchased;
			return *this;
		}
}


void GroceryItem::set_item_name(char* name)//use char* instead of char name[30]. Why??
{
	item_name = new char[strlen(name) + 1];
	strcpy(item_name, name);
}

void GroceryItem::set_item_price(double price)
{
	if( price > 0)
		item_price = price;

	if( price <= 0)
	{
		item_price = 0;
		cout << "Price cannot be negative. Price has been set to zero" << endl;
	}
}

void GroceryItem::set_qty_on_hand(int onhand)
{
	if( onhand > 0)

		qty_on_hand = onhand;

	if( onhand <=0 )
	{
		onhand = 0;
		cout << "Quantity on hand cannot be negative. Quantity on hand has been set to zero" << endl;
	}
}

void GroceryItem::set_qty_purchased(int purchased)
{
	if( purchased > 0)

		qty_purchased = purchased;

	if( purchased <= 0 )
	{
		purchased = 0;
		cout << "Quantity purchased cannot be negative. Quantity purchased has been set to zero" << endl;
	}

}

char* GroceryItem::get_item_name()
{
	return item_name;
}


double GroceryItem::get_item_price()
{
	return item_price;
}

int GroceryItem::get_qty_on_hand()
{
	return qty_on_hand;
}

int GroceryItem::get_qty_purchased()
{
	return qty_purchased;
}



And here's my main.
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
135
136
137
//Create and manipulate GroceryItem class objects

#include<iostream>
#include "Grocery (2).h"
using namespace std;

#include<fstream>

const int NUM_ITEMS = 10;

int main()
{
	GroceryItem grocery_items[NUM_ITEMS];

	ifstream inFile;
	inFile.open("Grocery.txt");

	int k, result;
	char* tempy = new char;
	double temp;
	int stock;
	for(k = 0; k < NUM_ITEMS; k++)
	{
		inFile.getline(tempy,256,'\n');
        grocery_items[k].set_item_name(tempy);
		inFile >> temp;
		grocery_items[k].set_item_price(temp);
		inFile >> stock;
		grocery_items[k].set_qty_on_hand(stock);
		inFile.ignore();
		
	}


	cout<<"The grocery items along with their respective price, and qty on hand are: "<<endl;

	for(k = 0; k < NUM_ITEMS; k++)
	{
				cout<<grocery_items[k].get_item_name()<<" "<<grocery_items[k].get_item_price()
				<<" "<<grocery_items[k].get_qty_on_hand()<<endl;
	}


	char* item = new char;
	int want;
	char answer;

	do
	{
		cout<<"Please indicate the grocery items you want: ";
		cin.getline(item,30,'\n');	
		cout<<endl<<"Please the quantity of the items wanted: ";
		cin>>want;		
		cout<<endl<<"Would you like to continue purchasing items? Please enter Y for yes or N for no."<<endl;
		cin.ignore();
		cin>>answer;
		for( k = 0; k < NUM_ITEMS; k++)
		{
			result = strcmp(grocery_items[k].get_item_name(),item);
			if(!result)
			{
				grocery_items[k].set_qty_purchased(want);
			}
		}
		cin.ignore();
	}while(answer=='y'||answer=='Y');

	double Total = 0;



	for(int m = 0; m < NUM_ITEMS; m++)
	{
		cout<<"The customer purchased: "<<endl;

		
		
		int i,j;
		int minName;
		char*  tempName1, *tempName2; 
		GroceryItem temp;
		double tempPrice;
		int tempQty_on_hand;
		int tempQty_purchased;
		
		
		for( j = 0; j < NUM_ITEMS-1; j++)
		{
			minName = j;
		for( i = j + 1; i < NUM_ITEMS; i++)
			{
				tempName1 = grocery_items[minName].get_item_name();
				tempName2 = grocery_items[j].get_item_name();
				if(tempName1 > tempName2)
					minName = i;
			}
			
			temp = grocery_items[minName];
			grocery_items[minName] = grocery_items[j];
			grocery_items[j] = temp;
		} //Tried to do a bubble sort but it didn't work

			
			if(grocery_items[m].get_item_name() == grocery_items[m-1].get_item_name())
			{	
				cout<<grocery_items[m].get_item_name()<<" "<<grocery_items[m].get_item_price()
				<<" "<<grocery_items[m].get_qty_on_hand()<<endl;
			}
		else if(grocery_items[m].get_item_name() < grocery_items[m-1].get_item_name()) 
			{
				cout<<grocery_items[m].get_item_name()<<" "<<grocery_items[m].get_item_price()
				<<" "<<grocery_items[m].get_qty_on_hand()<<endl;
			}
		else if(grocery_items[m].get_item_name() > grocery_items[m-1].get_item_name()) 
			{
				char* x[30]  = {grocery_items[m-1].get_item_name()};
				grocery_items[m].set_item_name(x[30]);
				double y  = grocery_items[m-1].get_item_price();
				grocery_items[m].set_item_price(y);
				int z = grocery_items[m-1].get_qty_on_hand();
				grocery_items[m].set_qty_on_hand(z);

				cout<<grocery_items[m].get_item_name()<<" "<<grocery_items[m].get_item_price()
				<<" "<<grocery_items[m].get_qty_on_hand()<<endl;
			}
		
		Total = Total + grocery_items[m].get_item_price() * grocery_items[m].get_qty_purchased();
	}


	cout<<"The customer's total bill is: "<<Total<<endl;
	cout<<"The order will be delivered by the end of the day."<<endl;

	inFile.close();
	system("pause");
	return 0;
}


And here's the text file
Raisan Bran
3.49
300
Milk
1.49
200
WhiteBread
2.49
50
Butter
2.49
100
Grape Jelly
1.09
50
Peanut Butter
2.49
45
Tomato Soup
.49
200
Cherry Yogurt
.69
250
Vanilla Yogurt
.69
200
Rye Bread
1.49
55



You included <string>, but you aren't using any as far as I can tell.

You don't need to include <iostream> and <string> in every file. If you include them in a header, and you include your header, you don't need to include them again.

Check your function declarations in your header.
Yes I actually Simplified the main. But I'm dying here when it comes to bubble sorting with char*. I have to use it too.

I've changed it to this and the program will always crash when it's time to output the inputs.
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
#include<iostream>
#include "Grocery (2).h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <string>
using namespace std;

#include<fstream>
//void BubbleSort (char* names, int size);

//void strcpy(char* , char* );
//void Swap (char*, int n, int k);


const int NUM_ITEMS = 10;

int main()
{
	GroceryItem grocery_items[NUM_ITEMS];

	ifstream inFile;
	inFile.open("Grocery.txt");

	int k, result;
	char* tempy = new char;
	double temp;
	int stock;
	for(k = 0; k < NUM_ITEMS; k++)
	{
		inFile.getline(tempy,256,'\n');
        grocery_items[k].set_item_name(tempy);
		inFile >> temp;
		grocery_items[k].set_item_price(temp);
		inFile >> stock;
		grocery_items[k].set_qty_on_hand(stock);
		inFile.ignore();
		
	}


	cout<<"The grocery items along with their respective price, and qty on hand are: "<<endl;

	for(k = 0; k < NUM_ITEMS; k++)
	{
				cout<<grocery_items[k].get_item_name()<<" "<<grocery_items[k].get_item_price()
				<<" "<<grocery_items[k].get_qty_on_hand()<<endl;
	}


	char* item = new char;
	int want;
	char answer;

	do
	{
		cout<<"Please indicate the grocery items you want: ";
		cin.getline(item,30,'\n');	
		cout<<endl<<"Please the quantity of the items wanted: ";
		cin>>want;		
		cout<<endl<<"Would you like to continue purchasing items? Please enter Y for yes or N for no."<<endl;
		cin.ignore();
		cin>>answer;
		for( int r = 0; r < NUM_ITEMS; r++)
		{
			result = strcmp(grocery_items[r].get_item_name(),item);
			if(!result)
			{
				grocery_items[r].set_qty_purchased(want);
			}
		}
		cin.ignore();
	}while(answer=='y'||answer=='Y');

	double Total = 0;

	int i, j;
char* temporary = NULL;


for(i = 0; i < NUM_ITEMS; i++)
    for(j = 0; j < NUM_ITEMS-1; j++)
		if(strcmp(grocery_items[i].get_item_name(), grocery_items[i+1].get_item_name()) > 0){
            //swap the two array elements
			strcpy(temporary, grocery_items[j].get_item_name());
            strcpy(grocery_items[j].get_item_name(), grocery_items[j+1].get_item_name());
            strcpy(grocery_items[j+1].get_item_name(), temporary);
        }
	//BubbleSort(grocery_items[10].get_item_name(), 20);
	for(int m = 0; m < NUM_ITEMS; m++)
	{//BubbleSort(grocery_items[m].get_item_name(), 20);
		if(grocery_items[m].get_qty_purchased() == 0)
		{

		}
		else 
		{
		cout<<"The customer purchased: "<<grocery_items[m].get_qty_purchased()<<"  "<<grocery_items[m].get_item_name()<<endl;
		

		
	
		Total = Total + grocery_items[m].get_item_price() * grocery_items[m].get_qty_purchased();
		}
	}


	cout<<"The customer's total bill is: "<<Total<<endl;
	cout<<"The order will be delivered by the end of the day."<<endl;

	inFile.close();
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.