Please help, I'm getting compilation errors

I have been trying to fix the multiple compilation errors I'm getting with "no operator found" on Microsoft visual studio. This is a project from school. I basically need to make a program to keep track of the fishes in a fish store.

Can you guys help me?

any advice is greatly appreciated

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
 // TUAN"S FISH STORE

// This programn will keep track of all your wonderful fishies  :))

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// fish strcture for each fish
struct Fish
{
	string name;
	int tank;
	int quantity;
	double price;
};

// Prototypes for all functions
void displayMenu();
void displayInventory( Fish, int, int );
void readInventory( Fish, int, int );
void writeInventory( Fish, int, int );
void addInventory( Fish, int, int );
void removeInventory( Fish, int, int );
void searchInventory( Fish, int, int );
void updateInventory( Fish, int, int );
void updateTank( Fish, int, int );

int main( )
{
    int choice;		//users choice for the menu
	const int MAX_SIZE = 100;	// Size of array
	Fish myInventory;	//Fish tag initiated to myInventory
	int currentSize = 0;	
	 
     displayMenu();
     cout << "\nPlease select an option: ";
     cin >> choice;
     
	 switch (choice)
     {
	 case 1: displayInventory(myInventory, MAX_SIZE, currentSize);
                      break;
			case 2: readInventory(myInventory, MAX_SIZE, currentSize);
                      break;
			case 3: writeInventory(myInventory, MAX_SIZE, currentSize);
                      break;
			case 4: addInventory(myInventory, MAX_SIZE, currentSize);
                      break;
			case 5: displayInventory(myInventory, MAX_SIZE, currentSize);	// display then remove	
					removeInventory(myInventory, MAX_SIZE, currentSize);
                      break;
			case 6: searchInventory(myInventory, MAX_SIZE, currentSize);
                      break;
			case 7: updateInventory(myInventory, MAX_SIZE, currentSize);
                      break;
			case 8: updateTank(myInventory, MAX_SIZE, currentSize);
                      break;
			case 9: exit(0);
                      break;
     }
     return 0;

}

void displayMenu()
{
	cout << "Tuan's Fishies\n";
	cout << "***************\n";
	cout << "Please chose one of the options below\n";
	cout << "1.  Display the Inventory\n";
	cout << "2.  Read Fish Inventory from a File\n";
	cout << "3.  Write Fish Inventory to a File\n";
	cout << "4.  Add Fish to Inventory\n";
	cout << "5.  Remove Fish from Inventory\n";
	cout << "6.  Search for Fish\n";
	cout << "7.  Update Quantity\n";
	cout << "8.  Update Tank/Location\n";
	cout << "9.  Exit\n";
}


void displayInventory( Fish myArray[], int max, int currtSize)
{
	cout << "This is the Inventory\n";

	for (int index = 0; index <= max; index++)
	{
		cout << "fish index number" << (index+1) << endl;
		cout << myArray[index].name << endl;
		cout << myArray[index].tank << endl;
		cout << myArray[index].quantity << endl;
		cout << myArray[index].price << endl << endl;
	}

}	

void readInventory(	Fish myArray[], int max, int currtSize = 0)
{
	ifstream inputFile;
	string filename;

	cout << "Please enter the file name\n";
	getline(cin,filename);

	while ((myArray[currtSize].price) > 0)		// find the next empty subscript
	{
		currtSize++;
	}

	inputFile.open(filename);

	if (inputFile)
	{
		cout << "Reading data from the file\n";
		while (currtSize < max)
		{
			inputFile >> myArray[currtSize].name;
			inputFile >> myArray[currtSize].tank;
			inputFile >> myArray[currtSize].quantity;
			inputFile >> myArray[currtSize].price;
			currtSize++;
		}
	}

	inputFile.close();
}

void writeInventory( Fish myArray[], int max, int currtSize)
{
	ofstream outputFile;
	int count;
	string filename;

	cout << "Please enter the desired file name\n";
	cin  >> filename;

	filename = filename + ".dat";

	outputFile.open(filename);
	cout << "Writing data to the file\n";

	for (count = 0; count < max; count++)
	{
		outputFile << myArray[count].name << endl;
		outputFile << myArray[count].tank << endl;
		outputFile << myArray[count].quantity << endl;
		outputFile << myArray[count].price << endl;
	}
	outputFile.close();
	cout << "Finished Writing data to the file\n";
}

void addInventory( Fish myArray[], int max, int currtSize = 0)	
{																			// need to sort the arrays by fish name

	while (myArray[currtSize].quantity > 0){
		currtSize++;
	}

	cout << "Enter the fish name\n";
	cin  >>	myArray[currtSize].name;
	cout << "Enter the fish tank number\n";
	cin  >> myArray[currtSize].tank;
	cout << "Enter the fish quantity\n";
	cin  >> myArray[currtSize].quantity;
	cout << "Enter the fish price\n";
	cin  >> myArray[currtSize].price;
}

void removeInventory( Fish myArray[], int max, int currtSize)
{
	int index;

	cout << "Please enter the fish index number you wish to remove from the inventory\n";
	cin  >> index;

	myArray[index].name.clear();	//might work if i just have myArray[index].clear()
	myArray[index].tank = 0;
	myArray[index].quantity = 0;
	myArray[index].price = 0;

	cout << "Fish index " << index << " has been removed\n";
}

void searchInventory( Fish myArray[], int max, int currtSize)
{
	string name;
	int index = 0;
	bool foundName = false;

	cout << "Please enter the name of the fish you are looking for\n";
	getline(cin,name);

	while (foundName = false && index < max)
	{	
		if(name == myArray[index].name)
		{
			foundName = true;
		}
		index++;
	}
	if (foundName = true) 
		{
			cout << "The fish has been found\n";
			cout <<	myArray[index - 1].name;
			cout << myArray[index - 1].tank;
			cout << myArray[index - 1].quantity;
			cout << myArray[index - 1].price;
		}
	else 
		cout << "No fish with the same name has been found\n";
}

void updateInventory( Fish myArray[], int max, int currtSize)
{
	int index;
	int choice;
	displayInventory(myArray, max, currtSize);
	
	cout << "Please enter the fish index number you wish to take a look at\n";
	cin  >> index;

	cout <<	myArray[index-1].name;
	cout << myArray[index-1].tank;
	cout << myArray[index-1].quantity;
	cout << myArray[index-1].price;

	cout << "How would you like to change the quantity\n";
	cout << "1. New total quantity\n";
	cout << "2. Decrease by one\n";
	cout << "3. Increase by one\n";
	cin  >> choice;

	switch (choice)
	{
	case 1:
		cout << "Please enter the new total quantity\n";
		cin  >> myArray[index].quantity;
			while (myArray[index].quantity < 0)
			{
				cout << "please enter a positive integer\n";
				cin  >> myArray[index].quantity;
			}
			cout << "Your new fish quantity is " << myArray[index].quantity << endl;
		break;
	case 2:
		if (myArray[index].quantity = 0)
			cout << "You currently have 0 fish of this breed";
		else
		{
		myArray[index].quantity = myArray[index].quantity - 1;
		cout << "Your new fish quantity is " << myArray[index].quantity << endl;
		}
		break;
	case 3: 
		myArray[index].quantity = myArray[index].quantity = 1;
		cout << "Your new fish quantity is " << myArray[index].quantity << endl;
		break;
	}
}

void updateTank( Fish myArray[], int max, int currtSize)
{
	int index;
	displayInventory(myArray, max, currtSize);

	cout << "Please enter the fish index number you wish to take a look at\n";
	cin  >> index;

	cout << myArray[index-1].name;
	cout << myArray[index-1].tank;
	cout << myArray[index-1].quantity;
	cout << myArray[index-1].price;

	cout << "Please enter the new tank number for the fish";
	cin  >> myArray[index-1].tank;

	cout << myArray[index-1].name << " has been transfered to tank " << myArray[index-1].tank << endl;
}


EDIT: I fixed the errors now I'm getting new errors
Last edited on
post the errors
Line 211 & 258 displayInventory(); <- This function takes arguments you try to call it without them.

There are some other things but I am assuming you have those methods worked out in another file.
Last edited on

I took a screen shoot of my errors since there were a lot
http://imgur.com/eLWKq14
replying to mobotus:

I just have a source and header file

Also for displayInventory() I thought I can call it w/o an argument if the function doesn't use arguments

nvm these functions have arguments. I've fixed them.
Last edited on
I think most of my errors has to do with the string in my structure

a lot of the errors is related to this myArray[index].name
You're missing #include <string>

Line 109: You're using the wrong form of getline. With a string, the form should be:
getline (cin, filename);

Line 121: myarray is type Fish, but type Fish does not overload the >> operator.

Line 143: myarray is type Fish, but type Fish does not overload the << operator.

Lines 171-173: tank and quantity are ints. ints does not have a clear() function. price is a double. doubles do not have a clear() function.

Line 189: You can't pass a string to strcmp. Since you're trying to compare two strings, simply use the equality operator.
if (name == myArray[index].name)

Line 87: displayInventory takes three arguments. Line 211,258: You're calling displayInventory with no arguments.

Your function prototypes at lines 9-17 do not match your implementations. i.e. Your prototypes state the function expects a single object of type Fish, while your implementations state that the function accepts an array of type Fish.



Note: Lines 70-272 are function implementations and do NOT belong in a header file.


Last edited on
Thank you so much much for the clarification AbstractionAnon!

The only thing I didn't understand was:
Line 121: myarray is type Fish, but type Fish does not overload the >> operator.

Line 143: myarray is type Fish, but type Fish does not overload the << operator.

Would you kindly explain it to me?

Also, for lines 171-173 is there a way I can clear the double and int variable or will I have to set them to 0.
Only built in types (int, long, float, double, char, etc) define the << and >> operator. For other types (like Fish), you have to define those operators yourself for your type. For example, if I say cout << myarray[i];, what does that mean? cout doesn't know how to write an instance of type Fish. You have to provide the code to do that. The idom for that is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Fsih
{
...
  friend ostream & operator << (ostream & os, const Fish & f);
};

ostream & operator << (ostream & os, const Fish & f)
{  // Here I can format Fish however I want to the output stream
    os << f.name; 
    os << f.tank;
    os << f.quantity;
    os << f.price;
    return os;
}

//  Now, I can write the following
  cout << myarray[i];  // Fish's overload of the << operator is called. 


Overloading the >> operator works the same way.


Lines 171-173: Simply set them to 0.
Last edited on
Topic archived. No new replies allowed.