Undeclared Identifier?

for some reason im getting an undeclared identifier error for the method GetData.
If someone could please help.

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
  // Author:		
// Source file:	
// Description:	
// Compiler used:	

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>
using namespace std; 

// Function Prototypes
	void DisplayTitle();
	double GetBegBal(ifstream&);
	void DisplayBal(double);
	payRec GetData(ifstream&);
	double ProcessCheck(double, double);
	double ProcessDeposit(double, double);
	double ProcessATM(double, double);
	double ProcessSvcChg(double);


//Global Constants
const double	CHARGE = 10,
				ATMFEE =  2;

struct totals{
	double  Credits;
	double  Debits;
	double	Service;
};

struct payRec{
	double transAmt;
	int transCode;
};

int main()
{
	//Variable Declarations
	double balance;
	ifstream fin;
	int transCtr;

	//Declared my structure
	payRec calc;
	totals print;

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	
	//Displays the title.
	DisplayTitle();

	//Opens the File.
	fin.open("C:\\Users\\Kodi\\Desktop\\checkIn.dat");

	//Creates a message if the file failed to open.
	if (fin.fail( ))
    {
        cout << "Input file opening failed.\n";
		system("pause");
        exit(1);
    }

	
	//gets the beginning value.
	//balance = static_cast<double>(GetBegBal(fin));

	//prints the beginning balance.
	cout << "The beginning balance is: " << balance;
	

	//While it is not the end of file.
	while(! fin.eof())
	{
		//pulls the values from the file and puts them in a variable from a structure.
		calc = GetData(fin);

		switch(calc.transCode)
		{
			case 1: 
				balance = ProcessCheck(balance, calc.transAmt);
				print.Debits = print.Debits + calc.transAmt;
				break;
			case 2: 
				balance = ProcessDeposit(balance, calc.transAmt);
				print.Credits = print.Credits + calc.transAmt;
				break;
			case 3: 
				balance = ProcessATM(balance, calc.transAmt); 
				print.Service = print.Service + ATMFEE;
				print.Debits = print.Credits + calc.transAmt;
				break;
		}
		cout << endl;
		DisplayBal(balance);
		if(balance < 0){
			balance = ProcessSvcChg(balance);
			print.Service = print.Service + CHARGE;
			cout << endl;
		}
		system("pause");
	}

	//Closes the file.
	fin.close();

	system("pause");
	return 0;
}



	void DisplayTitle()
	{
		cout << "\n       Check Register\n\n";
	}

	double GetBegBal(ifstream& in)
	{
		double x;
		in >> x;
		return x;
	}

	void DisplayBal(double x)
	{
		cout << "\t\tBalance = $" << setw(10) << x;
	}

	payRec GetData(ifstream& in)
	{
		payRec temp;
		in >> temp.transCode >> temp.transAmt;
		return temp;
	}

	double ProcessCheck(double bal, double amt)
	{
		cout << "\n  Check =    " << setw(10) << amt;
		return (bal - amt);
	}

	double ProcessDeposit(double bal, double amt)
	{
		cout << "\n  Deposit =  " << setw(10) << amt;
		return (bal + amt);
	}
	double ProcessATM(double bal, double amt)
	{
		cout << "\n  ATM     =  " << setw(10) << amt;
		bal = bal - amt;
		DisplayBal(bal);
		bal = bal - ATMFEE;
		cout << "\n  ATM Fee =  " << setw(10) << ATMFEE;
		return (bal);
	}
	double ProcessSvcChg(double bal)
	{
		cout << "\n  Service chg =" << setw(8) << CHARGE;
		bal = bal - CHARGE;
		DisplayBal(bal);
		return (bal);
	}
closed account (18hRX9L8)
This is because you declared the struct payRec after the GetData function. Therefore, at the point you declare GetData, payRec is undefined. Move the struct definitions before / above the function prototype definitions.
Last edited on
I have the struct payRec at the top before my main.....
closed account (18hRX9L8)
The C++ compiler works line by line. Therefore, it reads the header files first which is why your main types are defined already. Then it reads the functions. But when it gets to GetData, it throws an error because you never defined payRec before.

Based on your thinking, you can place anything above main in any order and it should still compile. Try creating a variable above the preprocessors. You will get an error because you variable type is not defined yet.

Therefore, move the struct definitions before / above the function prototype definitions so that payRec is already defined and can be used by GetData.

EX:
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
...

struct totals{
	double  Credits;
	double  Debits;
	double	Service;
};

struct payRec{
	double transAmt;
	int transCode;
};

// Function Prototypes
	void DisplayTitle();
	double GetBegBal(ifstream&);
	void DisplayBal(double);
	payRec GetData(ifstream&);
	double ProcessCheck(double, double);
	double ProcessDeposit(double, double);
	double ProcessATM(double, double);
	double ProcessSvcChg(double);

//Global Constants
const double	CHARGE = 10,
				ATMFEE =  2;

...


Now payRec is defined when you define the GetData function.
Last edited on
Yes, but how is the function prototype supposed to know what a payRect object is? Move lines 35 - 38 to line 13, or write a struct prototype.

ninjad.
Last edited on
OHHHHH!!! thanks guys lol, didnt understand what you meant :P thanks for the help... ive honestly been stuck on that problem for an hour and half -_-
closed account (18hRX9L8)
No problem. Remember to mark as "Solved".
Now im having an issue where it says print is not being initialized......

line 91

but it works for every other print, that is initialized....
Last edited on
I don't get any errors, only three warnings.

1
2
3
(46) : warning C4101: 'transCtr' : unreferenced local variable
(75) : warning C4700: uninitialized local variable 'balance' used
(92) : warning C4700: uninitialized local variable 'print' used


I don't know why this is being treated as en error in your case (strict compilation flags? Different compiler rules?), but the reason it shows up in the log is because the print object hasn't been formally initialized with a constructor.
Topic archived. No new replies allowed.