Question Function and variables.

Hello everybody, and thank you in advance.

I have a school project in which I had to write an ATM program where the user can use: deposit, withdraw, balance, and quit. The entered number cannot be a negative and must use at least 2 functions.

I have written a program that work very well, however my professor said there is a problem with the program, because I declare variables inside the main program and again outside.

This is her exact language: "You should not declare the variables outside the functions. If a function needs access to a variable from the main program that
function should take that variable as an argument. As an example,
since print_balance function needs the balance, it should take balance
as an argument. "

I am not sure what she means by that or how to fix it, 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
  #include <iostream>
using namespace std;


void print_menu();
void print_balance();
double choice_opt(char);

int main()
{
    // variable
    
    char choice;
    double deposit, withdraw;
    double balance = 5000.00;
    double positive_amount;
    
    
    //Body
    cout << "Welcome"<<endl;
    
    // Initial balance
    cout << "Your current balance is " << balance << " dollars."<<endl;
    
    
    while(choice != 'Q' || choice != 'q')
    {
    	if (choice == 'Q' || choice == 'q')
    		{
    			return 0;
    		}
    	else
			{
				print_menu();
    			cout << "Please make your choice:" << endl;
    			cin >> choice;
    			choice_opt(choice);
			}	

    	

	}
    
    
 
    return 0;
}

char choice;
double deposit, withdraw;
double balance = 5000.00;
double positive_amount;


    
void print_menu(void)
{
     cout << "Here are your options: " << endl;
     cout << "Enter:" << endl; 
     cout << "(D) or (d) to make a deposit, " << endl;
     cout << "(W) or (w) to make a withdrawal, " << endl;
     cout << "(B) or (b) to view the current balance, " << endl;
     cout << "(Q) or (q) to quit." << endl;
}

double choice_opt(char choice)
{
	if (choice == 'D' || choice == 'd')
        {
            cout <<"Please enter the amount you would like to deposit: "<< endl;
            cin >> deposit;
            while (deposit < 0)
			{
				cout << "Please enter a positive number:"<< endl;
				cin >> deposit;
			}
            
			balance = balance + deposit;
            cout << "Your transaction is complete.  Your current balance is:  " << balance <<" dollars."<< endl;
			
        }
        
    
   	else if (choice == 'W' || choice == 'w')
         {
         	
            cout <<"Please enter the amount you would like to withdraw: "<< endl;
            cin >> withdraw;
            
            while(withdraw <0)
            {
            	cout << "Please enter a positive number:"<< endl;
				cin >> withdraw;
			}
			
			
			balance = balance - withdraw;
            cout << "Your transaction is complete.  Your current balance is:  " << balance <<" dollars."<< endl;
			
            
         }
         
    else if(choice == 'B' || choice == 'b')
   		{
    		cout<<"Your current balance is: "<<balance<<endl;
    	}
    else if(choice == 'Q' || choice == 'q')
    	{
    		return 0;
		}
	else
		{
			cout<< "Please make the proper choice"<< endl;
		}
}

void print_balance(double balance)
{
     cout << "Your current balance is: " << balance<< endl;
}

Lines 49-52: Why are you declaring these variables here? You've already declared them as local variables at lines 13-16.

Line 66: Why does choice_opt return a double? And what double does it return (other than 0)?

Lines 78,97: Which balance are you updating? Hint: It's not the balance from main.

Line 117: Where is print_balance ever called?
I am aware of all of these problem however (outside of line 117) I am not sure how to fix them.

Lines 49-52: Why are you declaring these variables here? You've already declared them as local variables at lines 13-16.

If I delete them, then my function gives an error, variable is not defined

Line 66: Why does choice_opt return a double? And what double does it return (other than 0)?

its doubel so when users choice Q it returns 0, which in turn quits the program

You're declaring GLOBAL VARIABLES you don't need to do that. Declare them within each function that you want to use them?
In your double choice() function declare withdraw; and deposit;
If I delete them, then my function gives an error, variable is not defined

That's not a reason to make them global. Do you understand that when you change the value of balance at line 78, you're not changing the value of balance at line 15?

Do you understand how to pass a variable by reference?

Note that the variables deposit and withdraw are not used outside of choice_opt(). Therefore they should be locals within choice_opt(). Always make variables as local as possible. Globals are discouraged, especially when the have the same name as locals. I dare say that when you modify balance in your program, you don't understand which variable named balance you're modifying.

its double so when users choice Q it returns 0, which in turn quits the program
You're missing my point. Line 37 where you call choice_opt(), you're ignoring the return value. Therefore, what is the point of returning a value at all?

Lines 26,28: choice is an uninitialized variable. The first time through the loop, the results of the while and if condition are undefined.


I have just run your program and was wondering if you are aware that you can withdrawn more money then you have, i don't know if it is meant to through an error at that point but i though i would just make you are.

Consider using an if statement to check that the amount to be withdrawn is <= the amount in your current balance.
Abstract Anon, thank you for your feed back, I new to C++ and we only started programming about 3 weeks ago, so I am still learning how things work. I will look into your suggestion and see if I can figure things out.

schoolboyhacks, thank you as well, I was aware of the issue, and have added statement in my revised code.

Your professor is talking about passing values by reference or by value.
If you pass a parameter by reference to a function, it will change that value for the rest of the program.
If you pass by value, it will change that value only for the scope of that function.
To pass by reference you need to include the ampersand sign (&) in your prototype and function declaration.

So lines 49-52 are all global variables.
1
2
3
4
char choice;
double deposit, withdraw;
double balance = 5000.00;
double positive_amount;

It's as if you declared them before the main function. (Delete them)

I believe you need to pass the balance by reference to your choice_opt function.

So your prototype should be:
double choice_opt(char, double&);
Your function call should be:
choice_opt(choice,balance);
Your function definition should be:
double choice_opt(char choice, double& balance).

Also delete double deposit, withdraw from main() and only include it in your choice_opt function. Like so:
1
2
3
double choice_opt(char choice)
{
double withdraw,deposit;


That should help a lot.

You don't need to pass by reference to the print_balance function.
Just make sure the prototype is properly declared.
void print_balance(double);
And the call is properly declared.
void print_balance(balance);
Last edited on
you have to understand something called scope.
Topic archived. No new replies allowed.