Add and minus from the text file arent give the right value.

I'm going to try explain the best way i can.

I'm creating a bank system.

So I know to make a deposit to the balance, which add ups what is the balance to i have add.

When i run the deposit function, its work well in some ways. If balance(text file) has the value 10, add i addSum 20, the balance will become 30, same as the text file will become 30. so its work well to add positive number.

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
double deposit(double balance)

{

	double addSum = 0;

			system("CLS");
			cout<< "Welcome to deposit."<<endl;
			cout<<"Enter a sum you wish to add to your account:";
			
				 fstream myfile;
				 myfile.open ("money.txt");
			
				 cin>> addSum;

				 balance = addSum + balance;
			  
				 myfile << balance;
				
				 cout << balance << '\n';

			system("CLS");  
			return balance;
				 
	
}


When I withdraw from 30 which is the balance, then i takeSum, for example i take away 30. The balance will become 30 - 30 = 0
When i make another withdraw from example -150, it will be -150.

Which shows correct.

But when i make a deposit from -150 and i addSum 130, the balance shows -500, and it should had been -20.




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
double withdraw(double balance)

{
double takeSum = 0;

			system("CLS");
			cout<< "Welcome to withdraw."<<endl;
			cout<<"Enter a sum you wish to take away from your account:";
			
				cout << balance << '\n';

				 ofstream myfile;
				 myfile.open ("money.txt");
			
				 cin>> takeSum;

				 balance = balance - takeSum;
			  
				 myfile << balance;
				
				 cout << balance << '\n';

			system("CLS");  
			return balance;
}


My case statement to understand better.
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
int main()
			
{
	double balance = 0;
	balance = currentBalance(balance);
	menu(balance);
	return 0;
}		

	// menu function

	int menu(double balance)
{
	
		int menuOption = 0;
			
					cout<<" \n---------------------------------------------------"<<endl;
					cout<<"If you require help, press 1 for unscreen help. \n"<<endl;
		
					cout<<"Please choose from the following option \n"<<endl;
		
					cout<<"Press 2 for to check balance."<<endl;
					cout<<"Press 3 for to make a Withdraw."<<endl;
					cout<<"Press 4 for to make a Deposit."<<endl;
					cout<<"Press 9 to exit"<<endl;
		
					
	cin>>menuOption;
	
	switch(menuOption)
	{
		case 1:
			unscreen();
			menu(balance);
			break;
				
		case 2: 
			readBalance(balance);
			menu(balance);
			break;

		case 3:
			balance = withdraw(balance);
			readBalance(balance);
			menu(balance);
			break;			
					
		case 4:
			balance = deposit(balance);
			readBalance(balance);
			menu(balance);
			break;
							
		case 9:
			exit(0);
			
		default:
			cout<<"You did not choose from the menu options, reloading the menu!"<<endl;
			main();	

			return 0;
	}
		
}


What is causing this problem, also when function deposit and withdraw close, it goes to readBalance function, should go to menu.

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
double readBalance(double balance)
{

		int option;

		system("CLS");
		cout<<"Welcome to balance."<<endl;
		cout<<"Your balance is:"<<endl;
 			
				string path = "money.txt";							 // Storing your filename in a string
				ifstream fin;										 // Declaring an input stream object

				 fin.open(path);									 // Open the file
				 if(fin.is_open())									 // If it opened successfully
					{
				 fin >> balance;									// Read the values and
																	// store them in these variables
			     fin.close();										// Close the file
					}

		cout << balance << '\n';


		cout<<"\n"<<endl;
		cout<<"Press 0 for further action or press 9 to exit." <<endl;
			
			cin >> option;

			if (option == 9)
			{
			exit(0);
			}

			else if (option == 0 )
			{
			return balance;
			}

			else
			{
			exit(0);
			}
			
}
I've fixing the problem to return to the menu, not return to the readBalance function.
Topic archived. No new replies allowed.