So i'm creating bank system. W
Where I have function that reads the text file (balance), and I make a deposit, which adds what was in the balance = deposit + balance.
But this is what is happening.
In my text file i have number 10. Function balance works fine and reads 10 from the text file.
When I make a deposit add a value to the balance, if i add 7, the new balance is 17. And i check the text file shows me is 17. Which is correct.
The problem is here.
If i make another deposit, without closing the program, for example i add 5 to the balance, the new balance should be 22 = 17(balance) + 5(deposit), because 17 was store on the text file and it was the balance that was store on the text file on the last time. But it shows me 15, it adds the balance that the program first started which was 10(balance) + 5(deposit), but should had had be 17 + 5.
When I close the program the value on the text file, is 15, that was the last sum that i did.
I hope I made this clear.
1 2 3 4 5 6 7 8
|
int main()
{
double balance = 0;
balance = currentBalance(balance);
menu(balance);
return 0;
}
|
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
|
double currentBalance( double balance)
{
cout<<"Welcome to your bank system."<<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';
return balance;
}
// deposit function
double deposit(double balance)
{
int option;
double addSum;
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';
cout<<"\nPress 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);
}
}
|