Passing by reference?

So this is my first project from an "introductory" programming course that I already turned in and was graded but couldn't figure out how to do ONE thing. It is a simple bank program. I have a deposit function, withdrawal function, inquiry function and an exit function. In my "main" function I call a "menu" function that asks them for initial balance and congratulates the user and goes on to ask to re-enter their pin that will determine whether they can continue. The "menu_options" function is what actually will give the options to deposit withdrawal etc. My problem is that for my "inquiry" function I can't print out the pin. I don't know where I need to pass it and if I do by reference which then I imagine I have to change the other functions. "menu_options" is what calls "inquiry".

This is the function I'm having issue with but I'll put the rest of the program which runs fine.
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
 
 void inquiry(double balance, int account)
{
        //function I need help with
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout << "Your account number: " << account << endl;
	cout << "Your PIN: " <<"i can't print pin out. \n";
	cout << "Current balance: $" <<balance <<endl;
	menu_options(balance, account);
}

//all of program
/*
Description: This is a simple simulation of an ATM. It asks the user for an account number and pin.
Pin number should be of correct format before continuing. User only has two times to enter it
correctly. If pin is entered correctly user is presented with four options being the option to deposit,
withdrawal, inquiry, or exit.
*/
#include <iostream>
using namespace std;

//function prototype
int menu(int account, int user_pin);
void menu_options(double user_balance, int account);
void deposit(double balance, int account);
void withdrawal(double balance, int account);
void inquiry(double balance, int account);
void exit();

int main()
{
	
	int pin_number, account_number;
	cout << "***** Welcome to The Bank of. *****\n";
	cout << "Account Creation\n";
	cout << "Enter your account number: ";
	cin >> account_number;
	cout << "Enter your PIN: ";
	cin >> pin_number;
	if (pin_number >= 1000 && pin_number <= 9999)//checks to see if pin is in correct range
	{
		menu(account_number, pin_number);//if pin is in correct range "menu" function is called 
			//and passes account and pin numbers
	}
	else
	{// second chance to enter correct pin
		cout << "Invalid PIN number. Use a number between 1000 and 9999\n";
		cout << "Enter your PIN again: ";
		cin >> pin_number;
		if (pin_number >= 1000 && pin_number <= 9999)//checks to see if pin is in correct range
		{
			menu(account_number, pin_number);//if pin is in correct range "menu" function is called 
			//and passes account and pin numbers
		}
		else
		{
			cout << "Sorry. You can't create an account.\n"
				<< "Thank you for using  ATM.\n";
		}
	}
	return 0;
}

//function declaration
int menu(int account, int user_pin)//by this time user entered pin correctly
{
	double balance;
	int pin;
	cout << "Enter the initial balance:";
	cin >> balance;
	cout << "Congratulations!\n";
	cout << "Your account " << "(" << account << ")" << " was successfully created.\n";
	cout << "Enter your PIN to continue: ";
	cin >> pin;
	if (pin != user_pin){//program agains asks user to enter correct pin with the chance to enter again
		//if wrong
		cout << "Incorrect PIN. You can try one more time.\n";
		cout << "Enter the PIN:";
		cin >> pin;
		if (pin != user_pin){//compares against correct pin 
			cout << "Incorrect PIN. Your account is suspended.\n"
				"Contact the bank directly.\n"
				"Thank you for using CSUMB ATM.\n";
		}
		else{//call 
			menu_options(balance,account);
		}
	}
	else{
		menu_options(balance,account);
	}
	return pin;
}

void menu_options(double user_balance, int account)
{
	char choice;
	cout << "Select your transaction: \n";
	cout << "     1: deposit\n";
	cout << "     2: withdrawl\n";
	cout << "     3: inquiry\n";
	cout << "     4: exit\n";
	cin >> choice;
	switch (choice)//option is based on what number user enters
	{
	case '1':
		deposit(user_balance, account);
		break;
	case '2':
		withdrawal(user_balance, account);
		break;
	case '3':
		inquiry(user_balance, account);
		break;
	case '4':
		exit();
		break;

	default:
		cout << "Warning: invalid option\n";
		menu_options(user_balance,account);
		break;
	}
}

//deposit function
void deposit(double balance, int account)
{
	int deposited_amount;
	cout << "How much do you want to deposit? ";
	cin >> deposited_amount;
	balance = balance + deposited_amount;
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout << "Your new balance is $ " << balance << endl;
	menu_options(balance, account);
	//return balance;
}

//double function
void withdrawal(double balance, int account)
{
	int withdrawal_amount;
	cout << "How much do you want to withdraw? ";
	cin >> withdrawal_amount;
	if (withdrawal_amount > balance){
		cout << "This is over your current balance.\n";
		menu_options(balance, account);
	}
	else{
		balance = balance - withdrawal_amount;
	}
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout << "Your new balance is $" <<balance <<endl;
	menu_options(balance, account);
}

//inquiry function
void inquiry(double balance, int account)
{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout << "Your account number: " << account << endl;
	cout << "Your PIN: " <<"i can't print pin out. \n";
	cout << "Current balance: $" <<balance <<endl;
	menu_options(balance, account);
}

//exit function
void exit()
{
	cout << "Thank you for using  ATM.\n";
}

Last edited on
Since you want to display the pin in your inquiry function, you're going to have to pass it as an argument. Doesn't make any difference if you pass it by reference or value since inquiry doesn't change it.

Since inquiry is called from menu_options, menu_options is also going to need to be passed the pin. menu_options gets called from menu, which has the pin, so that's not a problem.
Thanks! Something else I thought of and tried was to get rid of menu altogether but the thing with that is is that I copy and paste the code menu was made of two times because of the if else for the pin check which is why I thought I made menu in the first place. I'll try what you're saying though.
Yeah so I went ahead and said what you said to do. I was over thinking it. I just was annoyed that I couldn't print out the pin.
Topic archived. No new replies allowed.