Please help with classes!

Hello,

I am trying to use classes for the first time. I have 3 files: a header file that defines the class, a cpp file that defines the member-functions, and a cpp file that creates and manipulates the class objects (and includes the main function). I have provided the code I am using for these three files, and the data file I am using. I receive a slew of error messages when I try to run this. What could I be doing wrong?

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
 //Grocery.h
//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
	void set_item_name(string);
	void set_item_price(int);
	void set_qty_on_hand(int);
	void set_qty_purchased(int);
	string get_item_name();
	int get_item_price();
	int get_qty_on_hand();
	int get_qty_purchased();
private:
	string item_name;
	int item_price;
	int qty_on_hand;
	int qty_purchased;
};

#endif



//Grocery1.cpp
//Member-functions for the GroceryItem class

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

GroceryItem::GroceryItem()
{
	item_name = " ";
	item_price = 0;
	qty_on_hand = 0;
	qty_purchased = 0;
}

void GroceryItem::set_item_name(string name)
{
	item_name = name;
}

void GroceryItem::set_item_price(int 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;
	}
	
}

string GroceryItem::get_item_name()
{
	return item_name;
}


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


//Grocery2.cpp
//Create and manipulate GroceryItem class objects

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

#include<fstream>

const int NUM_ITEMS = 10;
const int SIZE_A = 30;

int main()
{
	GroceryItem grocery_items[NUM_ITEMS];
	
	ifstream inFile;
	inFile.open("Grocery.txt");
	
	int k;
	for(k = 0; k < NUM_ITEMS; k++)
	{
		
		inFile.getline(grocery_items[k].get_item_name, SIZE_A);
		inFile >> grocery_items[k].get_item_price() >> grocery_items[k].get_qty_on_hand() >> grocery_items[k].get_qty_purchased();
	}

	
	cout<<"The grocery items along with their respective price, qty on hand, and qty purchased 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()<<" "<<grocery_items[k].get_qty_purchased();
	}
	

	char answer;
	const int SIZE = 30;
	char item[SIZE];
	int qnty;

	for(k = 0; k < NUM_ITEMS; k++)
	{
		cout<<"Please indicate the grocery items you want: "<<endl;
		cin.getline(item, SIZE);
		cout<<"Please the quantity of the items wanted: "<<endl;
		cin>>qnty;
		grocery_items[k].set_item_name;
		grocery_items[k].set_qty_purchased;
		cin.ignore(256,'\n');	
	}
	
	int Total = 0;

	for(k = 0; k < NUM_ITEMS; k++)
	{
		cout<<"The customer purchased: "<<grocery_items[k].get_qty_purchased<<" "<<grocery_items[k].get_item_name;

		Total = Total + grocery_items[k].get_item_price * grocery_items[k].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;
	
}

//Grocery.txt
//Data 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

What is the actual name of your header file, groceryitem.h or grocery.h?

1
2
#ifndef GROCERYITEM_H
#define GROCERYITEM_H 


#include "Grocery.h"
Line 139: get_item_name is a function name (pointer). Since item_name is private, you're going to need to to read the item name into a temporary string, then call set_item_name with the temporary string.

Line 140: You're trying to input into getter functions. You can't do that.

Line 164,165: These are setters. You're not calling them as functions.

Line 173,175: These are getters, but you're not calling them as functions.

Topic archived. No new replies allowed.