How do I retain function value?

I have to pass parameters into my function so that it can be called right? But since I have a while loop, even after assigning a value to balance, my function call gets set to zero again and it forgets the assigned value of balance.

Since my instructions are to have a program exit and only call those two function in main, I have to do everything else in the two functions I'm allowed to use. But I'm stumped. I don't know how to keep balance retained so that the next time a user wants to add or take money out that value changes accordingly. Help plz?

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

#include "stdafx.h"
#include <iostream>
#include <iomanip> 

using namespace std; 

enum transType { SETUP=1, DEPOSITE, WITHDRAW, EXIT};

int showMenu(double balance);
double transaction(double amount, double balance, transType trans);

int menuSwitch; 
int quit; 

int _tmain(int argc, _TCHAR* argv[])
{
	while(quit!=4){
	
	showMenu(0);
	transaction(0,0,SETUP);// the values of zero just reset balance to zero, losing my ability to add and subtract from balance correctly. 

	}
	
	return 0;
}
int showMenu(double balance){
	 
	cout<<"Your Online Checking Account System"<<endl;
	cout<<"-------------------------------------------"<<endl; 
	cout<<"Select an option:"<<endl<<endl; 
	cout<<"  1. Set up the account."<<endl; 
	cout<<"  2. Deposit Funds into your Account."<<endl; 
	cout<<"  3. Withdraw Funds out of your Account."<<endl; 
	cout<<"  4. Exit"<<endl; 
	cout<<endl<<">>";
	cin>>menuSwitch;



	return 0;
}
double transaction(double amount, double balance, transType trans){
	
	double withdraw = balance-amount;
	 double deposite = balance+amount; 
	 
	switch (menuSwitch){
		case SETUP:
			cout<<"Enter the balance: ";
			cin>>balance;
			cout<<endl<<"Your current balance is: "<<balance<<endl<<endl;
			break; 
		case DEPOSITE:
			cout<<"Enter the amount of deposit: ";
			cin>>amount;
			cout<<"Your current balance is: "<<balance+amount<<endl<<endl;
			break;
		case WITHDRAW:
			cout<<"Enter the amount of withdraw: ";
			cin>>amount; 
			if(amount>balance){
				cout<<"*** Insufficient funds."<<"Your current balance is: "<<balance<<endl<<endl; 
			}
			else cout<<"Your current balance is: "<<balance-amount<<endl<<endl;
			break;
		case EXIT:
			cout<<"Have a Nice Day."<<endl;
			quit=4;
			break;
		
}
	return balance; 
}
Last edited on
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
//

#include "stdafx.h"
#include <iostream>
#include <iomanip> 

using namespace std; 

enum transType { SETUP=1, DEPOSITE, WITHDRAW, EXIT};

int showMenu(double balance);
double transaction(double amount, double balance, transType trans);

int menuSwitch; 
bool quit=true; 

int _tmain(int argc, _TCHAR* argv[]){
	
	int amount=0,balance=0; 
	while(quit=true){
	
	showMenu(balance);
	switch (menuSwitch){
		case DEPOSITE:
			cout<<"Enter the amount of deposit: ";
			cin>>amount;
			cout<<"Your current balance is: "<<transaction(amount,balance,DEPOSITE)<<endl<<endl;
			break;
		case WITHDRAW:
			cout<<"Enter the amount of withdraw: ";
			cin>>amount; 
			if(amount>balance){
				cout<<"*** Insufficient funds."<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl; 
			}
			else cout<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
			break;
		case EXIT:
			cout<<"Have a Nice Day."<<endl;
			quit=false;
			break;

	}
	
	return 0;
}
}
int showMenu(double balance){
	 
	cout<<"Your Online Checking Account System"<<endl;
	cout<<"-------------------------------------------"<<endl; 
	cout<<"Select an option:"<<endl<<endl; 
	cout<<"  1. Set up the account."<<endl; 
	cout<<"  2. Deposit Funds into your Account."<<endl; 
	cout<<"  3. Withdraw Funds out of your Account."<<endl; 
	cout<<"  4. Exit"<<endl; 
	cout<<endl<<">>";
	cin>>menuSwitch;
	switch (menuSwitch){
		case SETUP:
			cout<<"Enter the balance: ";
			cin>>balance;
			cout<<endl<<"Your current balance is: "<<balance<<endl<<endl;
			break; 
	}

	return balance;
}
double transaction(double amount, double balance, transType trans){
	double withdraw = balance-amount;
	double deposite = balance+amount;
	if(trans=DEPOSITE){
		return deposite; 
	}
	else
		return withdraw; 
	
	 
	 
	
		
}
	//return balance;  


I'm trying that but for some reason now it won't loop. It just executes once.
Last edited on
Nonlin wrote:
even after assigning a value to balance

I can't see anywhere in your code where you're assigning anything to balance

Also, don't forget; the comparison operator is == not =.
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
 
#include <iostream>
using namespace std;


enum transType
{
    SETUP = 1,
    DEPOSITE,
    WITHDRAW,
    EXIT
};


bool quit = 0; 


int    showMenu(int menuSwitch, int amount, double balance);
double transaction(double amount, double balance, transType trans);


int main()
{
    int menuSwitch, amount = 0;

    double balance = 0.0;

	while(!quit)
    {
   	    showMenu(menuSwitch, balance, amount);
    }

	return 0;
}


int showMenu(int menuSwitch, int amount, double balance){

	cout<<"Your Online Checking Account System"<<endl;
	cout<<"-------------------------------------------"<<endl;
	cout<<"Select an option:"<<endl<<endl;
	cout<<"  1. Set up the account."<<endl;
	cout<<"  2. Deposit Funds into your Account."<<endl;
	cout<<"  3. Withdraw Funds out of your Account."<<endl;
	cout<<"  4. Exit"<<endl;
	cout<<endl<<">>";
	cin>>menuSwitch;

	switch (menuSwitch){
		case SETUP:
			cout<<"Enter the balance: ";
			cin>>balance;
			cout<<endl<<"Your current balance is: "<<balance<<endl<<endl;
			break;

    	case DEPOSITE:
			cout<<"Enter the amount of deposit: ";
			cin>>amount;
			cout<<"Your current balance is: "<<transaction(amount,balance,DEPOSITE)<<endl<<endl;
			break;

		case WITHDRAW:
			cout<<"Enter the amount of withdraw: ";
			cin>>amount;
			if(amount>balance){
				cout<<"*** Insufficient funds."<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
			}
			else cout<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
			break;
		case EXIT:
			cout<<"Have a Nice Day."<<endl;
			quit=true;
			break;

        default:
            cout << "Invalid choice! Please, try again. \n";
            break;
	}
}


double transaction(double amount, double balance, transType trans){
	double withdraw = balance-amount;
	double deposite = balance+amount;
	if(trans==DEPOSITE){
		return deposite;
	}
	else
		return withdraw;

}
	//return balance; 


It's not a far cry from the original, but here are few suggestions

- Don't mix "logic with drawing". showMenu() should do just that; show the possible options to the user. It should not handle any input. This will also allow you to distribute your variables better so you don't have to use globals.

- You need to add an option which will return the current status of the account (i.e, how much money are left after each operation).
Last edited on
Unfortunately I can't change anything about the two functions showMenu() and transaction(). I have to follow the restriction of the instructions.

Also balance is being assigned via user input cin>>balance.

Thisis my updated code and it almost does what is should do. Just that withdraw doesn't work as its supposed to. Something is off with my if loops.

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
#include "stdafx.h"
#include <iostream>
#include <iomanip> 

using namespace std; 

enum transType { SETUP=1, DEPOSITE, WITHDRAW, EXIT};

int showMenu(double balance);
double transaction(double amount, double balance, transType trans);

int menuSwitch; 
int quit=0; 

int _tmain(int argc, _TCHAR* argv[]){
	
	double amount=0,balance=0; 
	while(quit!=4){

	showMenu(balance);
	switch (menuSwitch){
		case SETUP:
			cout<<"Enter the balance: ";
			cin>>balance;
			cout<<endl<<"Your current balance is: "<<balance<<endl<<endl;
			break; 
		case DEPOSITE:
			cout<<"Enter the amount of deposit: ";
			cin>>amount;
			cout<<"Your current balance is: "<<transaction(amount,balance,DEPOSITE)<<endl<<endl;
			break;
		case WITHDRAW:
			cout<<"Enter the amount of withdraw: ";
			cin>>amount; 
			if(amount>balance){
				cout<<"*** Insufficient funds."<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl; 
			}
			else cout<<"Your current balance is: "<<transaction(amount,balance,WITHDRAW)<<endl<<endl;
			break;
		case EXIT:
			cout<<"Have a Nice Day."<<endl;
			quit=4;
			break;

	}
	
	
}
	return 0;
}
int showMenu(double balance){
	
	cout<<"Your Online Checking Account System"<<endl;
	cout<<"-------------------------------------------"<<endl; 
	cout<<"Select an option:"<<endl<<endl; 
	cout<<"  1. Set up the account."<<endl; 
	cout<<"  2. Deposit Funds into your Account."<<endl; 
	cout<<"  3. Withdraw Funds out of your Account."<<endl; 
	cout<<"  4. Exit"<<endl; 
	cout<<endl<<">>";
	cin>>menuSwitch;

	return balance;

}
double transaction(double amount, double balance, transType trans){
	double withdraw = balance-amount;
	double deposite = balance+amount;
	if(trans==DEPOSITE){
		return deposite; 
	}
	if(trans==WITHDRAW){
		if(amount>balance){
				return balance; 
			}
		else 
			return withdraw; 
	}
	
	 
	 
	
		
}
Assign the value returned by the function to balance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// ...
case DEPOSITE:
    cout<<"Enter the amount of deposit: ";
    cin>>amount;
    balance = transaction(amount,balance,DEPOSITE) ;
    cout<<"Your current balance is: " << balance << "\n\n" ;
    break;
    
case WITHDRAW:
    cout<<"Enter the amount of withdraw: ";
    cin>>amount;
    if(amount>balance)
    {
        cout<<"*** Insufficient funds."<<"Your current balance is: " << balance << "\n\n" ;
    }
    else 
    {
        balance = transaction(amount,balance,WITHDRAW) ;
        cout<<"Your current balance is: " << balance << "\n\n" ;
    }
    break;
// ... 


You might also want to add a check for the amount being negative.
Oh wow, thanks. So simple... It bothers me how simple this fixes are. I just have very little coding experience.

Thanks again!
Topic archived. No new replies allowed.