Creating and ATM simulation

I keep getting this code between line 55 and 60
Unhandled exception thrown: read access violation.
**ptrBalance** was nullptr.


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
  //bring in our libararies of codes
#include <iostream>// allows input and output information

#include <string>//allows use of string data type
#include<stdio.h>
#include<fstream>//read/write to files
#include <ctime> //time(0)
#include <iomanip>//setprescision()
using namespace std; // allows use of classes and functions

//creates constant values that can not be changed
const int EXIT_VALUE = 5;
const float DAILY_LIMIT = 400.0f;
const string FILENAME = "Wallace_Bank.txt";
//create balance variable
double balance = 0.0;

// prototypes section


int* ptr = nullptr;
double* ptrBalance;
void deposit (double  *ptrBalance);
void withdrawal(double* ptrBlance, float dialyLimit); // overloaded method does not take withdrawal amount
void withdrawal(double* ptrBlance, float dialyLimit, float amount); // overloaded method that takes withdrwal amount.

int main() /*Entry point to applications*/
	//Make a deposit
	
{
	float deposit = 0.0f;
	do
	{
			cout << "\nEnter deposit amount:  ";
		cin >> deposit;
	
		if (cin.fail()) //did they give us a character isnsted of a nujmber?
		{
			cin.clear(); //clears fail state
			cin.ignore(INT16_MAX, '\n'); //clears keyboad buffer
			cout << "\nError. Please use number only.\n" << endl;
			deposit = -1;//set depsit to a "bad" number
			continue;//restart the loop.

		}
		else if (deposit < 0.0f)// check for negative number
			cout << "\nError.Invalid deposit amount.\n" << endl;
	} 
	
	while (deposit < 0.0f);

	//to get the double value located at the pinter programer must dereference it using an asterisk!
	{
		
		*ptrBalance += deposit; //same as : *ptrBalance =*ptrBlance + deposit;

		cout << fixed << setprecision(2) << "\nCurrent ptrBalance: $" << *ptrBalance << endl;//notice the astrisk
	}

	

	{
		//start of functoin
		//look for the starting balance; otherwise generate a randow starting balance
		ifstream iFile(FILENAME.c_str());
		if (iFile.is_open())//if file opens read the balance
		{
			iFile >> balance;
			iFile.close();
		}
		else
		{
			// if it did not open or does not exist create a random number for the starting balance
			srand(time(0));
			const int MIN = 1000;
			const int MAX = 1000;
			
		}
		

		cout << fixed << setprecision(2) << "Starting Balance:$" << balance << endl;

		double* ptrBalance = &balance;// double variable pointer ptrBalance set to address of balance variable.
		short choice = 0; // larger than or == to choice set to 0.
		//start the application loop for short
		//get deposit and validat it.



		do

		{
			//show function for do
			system("cls");//clears the consol screen 
			cout << "Menu\n" << endl;
			cout << "1) Deposit" << endl;
			cout << "2) Withdrawal" << endl;
			cout << "3) check Balance" << endl;
			cout << "4) Quick $40" << endl;
			cout << "5) Exit" << endl;
			cout << "\nEnter your choice: ";
			cin >> choice;




			switch (choice)// run code based on the user's choice
			{

			case 1:
			{
				double* deposit(ptrBalance); //passing a pointer so only four bytes have to go across the system bus!
				break;
			}


			case 2:
			{
				withdrawal(ptrBalance, DAILY_LIMIT); //passing four byte pointer!
				break;

			}


			case 3:
				cout << fixed << setprecision(2) << "\nCurrent Balance: $" << balance << endl;
				break;

			case 4:
				withdrawal(ptrBalance, DAILY_LIMIT, 40.0f);
				break;

			case 5:
				cout << "\nGoodbye...." << endl;
				break;

			default:
			{
				cout << "\n Error. Please select from the menu." << endl;
				break;
			}



				cout << "\nPress any key to continue....";
				break;
				int cdel_getch();

			}
		} while (choice != EXIT_VALUE); //while ture that choice is not equal to EXIT VALUE




		///Make a withdrawal- this overload accepts balance, dailyLimits, and withdrwal amouunt

		while (choice != EXIT_VALUE);

		ofstream oFile(FILENAME.c_str());

		oFile << balance << endl;

		oFile.close();

	}


}

	

	

	



void witdrawal(double* ptrBlance, float dailyLimit, float amount)
		//take away from the account and show the balance
	{
		if (amount > dailyLimit)
		{
			cout << "\nError.Amount exceeds dail limit." << endl;

		}
		else if (amount > * ptrBlance) //notice the asterisk to defeference the pointer!
		{
			cout << "\nError. Insufficient funds." << endl;
		}

		else
		{
			*ptrBlance -= amount; //same as: *ptrBlance =*ptrBlance - amount;
			cout << "\n Here is your cash; $ " << amount << endl;
		}

		cout << fixed << setprecision(2) << "\nCurrent Blance:$" << *ptrBlance << endl;
	}

void deposit(double* ptrBalance)
{
}


///Make a withdrawal
void withdrawal(double* ptrBlance, float dailyLimit)
{
	//get the withdrawal (you should validate this input)
	float amount = 0.0f;
	cout << "\nEnter withdrawal amount: ";
	cin >> amount;
	//call the overloaded method version that takes
	//the balance,dailyLimit, and withdrawal amount
	withdrawal(ptrBlance, dailyLimit, amount);
}

void withdrawal(double* ptrBlance, float dialyLimit, float amount)
{
}


please help if you can... thanks
*ptrBalance += deposit;
¿why is `ptrBalance' a pointer?
¿where do you set its value?
Also, you declare a global variable ptrBalance at line 22, but then hide it with various local variables (e.g. line 83) and function parameters (e.g. at line 200) of the same name.

My advice would be to get out of the habit of using global variables as soon as possible. Delete line 22, and declare a local variable where necessary (which looks like the main() function).
okay I can remove line 21 and 22 then I get an error saying that the line 83 is undefined
i defined it like this
double* ptrBalance;
*ptrBalance += deposit;
now I got this code
now i got an uninitialized memory ugh
> okay I can remove line 21 and 22 then I get an error saying that the line 83 is undefined
¿undefined what? post the error message verbatim
also, if you change your code, then post the updated code.
1
2
double* ptrBalance;
*ptrBalance += deposit;


And what do you think ptrBalance is pointing to when you attempt to dereference it with the second of those two lines?
Topic archived. No new replies allowed.