Incorrect Operands and Undefined Identifier

I'm writing a program to read in a Master.txt file and then update it through a Transaction.txt file that contains various transaction types [Adds (A), Deletes (D), and Edits (E1-E4)]. The records in both files are in ascending order based on Item#. Ultimately, the original Master.txt and updated Master file (Master2.txt) will be merged to reflect all valid transactions, and an errorLog.txt file will be created to indicate all invalid transactions. I feel I have all of the code written correctly, but I am still getting errors on my operands and identifiers. If someone can please proofread my code and point me in the right direction I would really appreciate it!
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct invRecord
{
	string delDate;
	int MID, qtyOnHand, qtyOrdered;
	float price, priceChng;
	bool includeinNewMaster;
};

struct transRecord
{
	string actionCode, delDate;
	int TID, qtyOnHand, qtyOrdered;
	float price, priceChng;
};

void checktoAdd(invRecord inventory[], string actionCode);
void checktoDelete(invRecord inventory[], string actionCode);
void checktoEdit1(invRecord inventory[], string actionCode);
void checktoEdit2(invRecord inventory[], string actionCode);
void checktoEdit3(invRecord inventory[], string actionCode);
void checktoEdit4(invRecord inventory[], string actionCode);

int main()
{
	//Read in records from Master.txt and count them
	invRecord inventory[100];
	ifstream readIn1;
	readIn1.open("Master.txt");
	int count = 0;

	//No need to sort the records, as they are already in proper order
	
	while (!readIn1.eof())
	{
		readIn1 >> inventory[count].MID >> inventory[count].qtyOnHand >> inventory[count].qtyOrdered >> inventory[count].delDate >> inventory[count].price >> inventory[count].priceChng;
		inventory[count].includeinNewMaster = true;
		count = count + 1;
	}
	
	ifstream readIn2;
	ofstream writeFile;
	transRecord transaction;
	int index = 0;
	
	while (!readIn2.eof())
	{
		readIn2.open("Transactions.txt");
		readIn2 >> transaction.actionCode >> transaction.TID >> transaction.qtyOnHand >> transaction.qtyOrdered >> transaction.delDate >> transaction.price >> transaction.priceChng;

		checktoAdd(inventory, transaction.actionCode);
		checktoDelete(inventory, transaction.actionCode);
		checktoEdit1(inventory, transaction.actionCode);
		checktoEdit2(inventory, transaction.actionCode);
		checktoEdit3(inventory, transaction.actionCode);
		checktoEdit4(inventory, transaction.actionCode);

		index++;
	}
		//Need to get the next record from Transactions.text; how do we do that?

//Cleanup: 
//Merge Master.txt with Master2.txt and create NewMaster.txt
//Read in Master2.txt into an array of structs (transRecords).

ifstream readIn;
transRecord master2[100];
readIn.open("Master2.txt");
int count2 = 0;
readIn >> master2[count2];

while (!readIn.eof())
{
	count2++;
	readIn >> master2[count2];
}

int size1 = count;
int size2 = count2;

writeFile.open ("NewMaster.txt");
int i=0, j=0;

while ((i<count) && (j<count2))
{
	if ((inventory[i].MID < master2[j].TID) && (inventory[i].includeinNewMaster = true))
		{
			writeFile << inventory[i++];
		}
		else writeFile << master2[j++];
		{
			if (i == count)
			{
				while (count2 <= size2)
				writeFile << master2[count2];
			}
			else
			{
					while (count <= size1)
					writeFile << inventory[count];
			}
		}
};

readIn.close();	
readIn1.close();
readIn2.close();
writeFile.close();
return 0;
}

void checktoAdd(invRecord inventory[], string actionCode)
{
	if (transaction.actionCode = A)
	{
		if (inventory[index].MID = transaction.TID)
		{
			writeFile.open ("ErrorLog.txt");
			writeFile << "Record " << inventory[index].MID << " already exists." << endl;
			writeFile.close;
		}
		else
		{
			writeFile.open ("Master2.txt");
			writeFile << inventory[index] << endl;
			writeFile.close;
		}
	}
	return;
}

void checktoDelete(invRecord inventory[], string actionCode)
{
	if (transaction.actionCode = D)
	{
		if (inventory[index].MID = transaction.TID)
		{
			inventory[index].includeinNewMaster = false;
		}
		else
		{
			writeFile.open ("ErrorLog.txt");
			writeFile << "Record " << inventory[index].MID << " does not exist in Master.txt." << endl;
			writeFile.close;
		}
	}
	return;
}

void checktoEdit1(invRecord inventory[], string actionCode)
{
	if (transaction.actionCode = E1)
	{
		if (inventory[index].MID = transaction.TID)
		{
			inventory[index].qtyOnHand = (inventory[index].qtyOnHand + transaction.qtyOnHand);
		}
		else
		{
			writeFile.open ("ErrorLog.txt");
			writeFile << "Record " << inventory[index].MID << " does not exist in Master.txt." << endl;
			writeFile.close;
		}
	}
	return;
}

void checktoEdit2(invRecord inventory[], string actionCode)
{
	if (transaction.actionCode = E2)
	{
		if (inventory[index].MID = transaction.TID)
		{
			(inventory[index].qtyOrdered = transaction.qtyOnHand);
			(inventory[index].delDate = transaction.delDate);
		}
		else
		{
			writeFile.open ("ErrorLog.txt");
			writeFile << "Record " << inventory[index].MID << " does not exist in Master.txt." << endl;
			writeFile.close;
		}
	}
	return;
}

void checktoEdit3(invRecord inventory[], string actionCode)
{
	if (transaction.actionCode = E3)
	{
		if (inventory[index].MID = transaction.TID)
		{
			(inventory[index].delDate = transaction.delDate);
		}
		else
		{
			writeFile.open ("ErrorLog.txt");
			writeFile << "Record " << inventory[index].MID << " does not exist in Master.txt." << endl;
			writeFile.close;
		}
	}
	return;
}

void checktoEdit4(invRecord inventory[], string actionCode)
{
	if (transaction.actionCode = E4)
	{
		if (inventory[index].MID = transaction.TID)
		{
			(inventory[index].price = transaction.price);
			(inventory[index].priceChng = (transaction.price - inventory[index].price));
		}
		else 
		{
			writeFile.open ("ErrorLog.txt");
			writeFile << "Record " << inventory[index].MID << " does not exist in Master.txt." << endl;
			writeFile.close;
		}
	}
	return;
}
Last edited on
My errors are:
>> (Lines 74 and 79)
<< (Lines 92, 94, 99, and 104)
if (transaction.actionCode = A)
if (transaction.actionCode = D)
if (transaction.actionCode = E1-E4)
if (inventory[index].MID = transaction.TID)
writeFile.open ("ErrorLog.txt");
if (transaction.actionCode = E4)

= is assignment
== is comparison.

You probably meant to have == here.

You make this mistake repeatedly, throughout your program.



Also... if you find yourself copy/pasting an entire routine just to make one minor change to it, you're doing it wrong. Rather than create separate routines for E1,E2,E3,E4... just use the same routine and pass which one you want as a parameter.

Duplicating code is bad. Duplicating the same code 6x is even worse.

(EDIT: I guess there are some subtle differences to each of those functions -- but at first glance they all looked identical to me /EDIT)


I am still getting errors on my operands and identifiers


What are the errors?

The thing about error messages is they typically tell you exactly what's wrong and what line the problem is on.
Last edited on
My errors are:
>> (Lines 74 and 79)
<< (Lines 92, 94, 99, and 104)
if (transaction.actionCode = A)
if (transaction.actionCode = D)
if (transaction.actionCode = E1-E4)
if (inventory[index].MID = transaction.TID)
writeFile.open ("ErrorLog.txt");


You're paraphrasing. Don't do that. At least not until you understand what is and isn't important about an error message.

Post the actual error messages you're getting. (copy/paste)
Last edited on
Error 1 error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion) c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 74
103 IntelliSense: no operator ">>" matches these operands c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 74

Error 7 error C2065: 'transaction' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 118
Error 8 error C2228: left of '.actionCode' must have class/struct/union c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 118
Error 9 error C2065: 'A' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 118
109 IntelliSense: identifier "transaction" is undefined c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 118
110 IntelliSense: identifier "A" is undefined c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 118
Error 10 error C2065: 'index' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 120
Error 11 error C2228: left of '.MID' must have class/struct/union c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 120
Error 12 error C2065: 'transaction' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 120
Error 13 error C2228: left of '.TID' must have class/struct/union c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 120
111 IntelliSense: identifier "index" is undefined c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 120
Error 14 error C2065: 'writeFile' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 122
Error 15 error C2228: left of '.open' must have class/struct/union c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 122
112 IntelliSense: identifier "writeFile" is undefined c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 122
Error 16 error C2065: 'writeFile' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 123
Error 17 error C2065: 'index' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 123
Error 18 error C2228: left of '.MID' must have class/struct/union c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 123
Error 19 error C2065: 'writeFile' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 124
Error 20 error C2228: left of '.close' must have class/struct/union c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 124
Error 21 error C2065: 'writeFile' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 128
Error 22 error C2228: left of '.open' must have class/struct/union c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 128
113 IntelliSense: identifier "writeFile" is undefined c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 128
Error 23 error C2065: 'writeFile' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 129
Error 24 error C2065: 'index' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 129
Error 25 error C2065: 'writeFile' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 130
Error 26 error C2228: left of '.close' must have class/struct/union c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 130

That is a sample of my error list. I understand that having separate routines for E1-E4 is not nearly the most efficient or pretty way to do things, but I have only been programming C++ for a few months so it was the easiest way for me to understand.
Last edited on
103 IntelliSense: no operator ">>" matches these operands c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 74


This is telling you your usage of the >> operator is incorrect, in that either the left or right side of it is not a type that can have the >> operator used on it.

The left-side is an ifstream, which definitely works with >>. So the problem must be with the right-side:

readIn >> master2[count2];

master2[count2] is of type transRecord. So this means you failed to overload the >> operator for your transRecord class. You cannot use the >> operator on custom classes/structs until you overload it.

Error 7 error C2065: 'transaction' : undeclared identifier c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 118


This is exactly what it says. "transaction" is undeclared... as in... you have not defined it. So the compiler doesn't know what you're talking about when you say "transaction" because you never told it what "transaction" is. Is it an int? A float? what?


Note that you have 'transaction' declared inside of main... however since it's local to main it can only be seen inside of main (look up what "scope" is. I'm not going to get into details but it basically means that a variable can't be seen outside of the {braces} it was defined in).

Since line 118 is not part of main... 'transaction' no longer exists... and therefore the compiler doesn't know what it is anymore.

Error 8 error C2228: left of '.actionCode' must have class/struct/union c:\users\cameron\documents\visual studio 2010\projects\pa89\pa89\pa89.cpp 118


This is related. It doesn't know what 'transaction' is, so it also doesn't know what 'actionCode' is.


I'm going to stop looking there as the rest of your errors are probably similar.


EDIT:

Also... there is a lesson to be learned here. Compile often. Until you get confident enough, you really should be hitting the "compile" button after every 3 or 4 line of code you write. Fix the errors are they come up.

Otherwise... what happens is what happened to you. You wrote a whole page before compiling, then got blasted with a million errors. Now you're overwhelmed.
Last edited on
Ok, thank you very much for the help. Everything you said makes sense and they are thing that I have learned before but forgot to consider when writing this code. Ok, I will compile often. It's difficult to want to stop and compile, but I can see exactly what you're saying and I now understand how important it is.
Topic archived. No new replies allowed.