This is my code. The purpose of this code is to top up money and withdraw money from an account (assume that it was created).
But after I choose top up money I could input value but the value that is in the file (in this case the file was named "account.dat") doesn't change at all. Is it because I still do not call correctly the variable to input to it or I already input but the file does not change or may be both?
Thanks for your time and consideration!
1 2 3 4 5 6 7
void Account::Deposit(int amount)
{
int sum = 0;
cout << "Nhap so tien nop: ";
cin >> amount;
sum = sum + balance_ + amount;
}
1 2 3 4 5 6 7
void Account::Withdraw(int amount)
{
int sum = 0;
cout << "Nhap so tien rut: ";
cin >> amount;
sum = sum + balance_ - amount;
}
1 2 3 4
int Account::GetAccountNumber() const
{
return account_number_;
}
1 2 3 4
int Account::GetBalance() const
{
return balance_;
}
1 2 3 4
char Account::GetType() const
{
return type_;
}
1 2 3 4 5 6 7
void Account::PrintAccount() const
{
cout << "\nSo tai khoan: " << GetAccountNumber() ;
cout << "\nTen chu tai khoan: " << name_ ;
cout << "\nLoai tai khoan: " << GetType();
cout << "\nSo du: " << GetBalance();
}
void SearchAndPrintAccount(int account_number)
{
Account account;
bool found = false;
ifstream in_file;
in_file.open("account.dat", ios::binary);
if (!in_file)
{
cout << "Khong mo duoc file, hay an phim enter de quay lai menu" << endl;
system("pause");
return;
}
cout << "\nKet qua tim kiem:\n";
while (in_file.read(reinterpret_cast<char *> (&account), sizeof(Account)))
{
// compare account_number with that read from the file
if (account.GetAccountNumber() == account_number) {
account.PrintAccount();
found = true;
}
}
in_file.close();
if (found == false)
cout << "\n\nKhong tim thay tai khoan";
}
void DepositOrWithdraw(int account_number, int option)
{
int amount; // So tien muon rut/nop duoc nhap tu ban phim
bool found = false;
Account account;
fstream file;
file.open("account.dat", ios::binary | ios::in | ios::out);
if (!file)
{
cout << "Khong mo duoc file, hay an phim enter de quay lai menu" << endl;
system("pause");
return;
}
while (!file.eof() && found == false)
{
file.read(reinterpret_cast<char *> (&account), sizeof(Account));
if (account.GetAccountNumber() == account_number)
{
account.PrintAccount();
if (option == 1)
{
cout << "\n\n\tNop tien ";
cout << "\n\nNhap so tien nop: ";
cin >> amount;
}
elseif (option == 2)
{
cout << "\n\n\tRut tien ";
cout << "\n\nNhap so tien rut: ";
cin >> amount;
}
file.write(reinterpret_cast<char*> (&account), sizeof(Account));
cout << "\n\n\t Giao dich thuc hien thanh cong";
found = true;
}
}
file.close();
if (found == false)
cout << "\n\nKhong tim thay tai khoan";
}
Well, In Deposit(int amount) you should decide whether you want to provide or ask for the amount. Not both.
Further more: sum is a local variable. Changing this local variable has no effect outside the function. I guess what you want is balance_ += amount;.
And it is not even called in DepositOrWithdraw(...).
Why do your Deposit() and Withdraw() functions take an argument amount? You don't use the value passed in at all - you overwrite it with the value input by the user.
void DepositOrWithdraw(int account_number, int option)
{
int amount; // So tien muon rut/nop duoc nhap tu ban phim
bool found = false;
Account account;
fstream file;
file.open("account.dat", ios::binary | ios::in | ios::out);
if (!file)
{
cout << "Khong mo duoc file, hay an phim enter de quay lai menu" << endl;
system("pause");
return;
}
while (!file.eof() && found == false)
{
file.read(reinterpret_cast<char *> (&account), sizeof(Account));
if (account.GetAccountNumber() == account_number)
{
account.PrintAccount();
if (option == 1)
{
cout << "\n\n\tNop tien ";
cout << "\n\nNhap so tien nop: ";
cin >> amount;
account.Deposit(amount);//here
}
elseif (option == 2)
{
cout << "\n\n\tRut tien ";
cout << "\n\nNhap so tien rut: ";
cin >> amount;
account.Withdraw(amount);//here
}
file.write(reinterpret_cast<char*> (&account), sizeof(Account));
cout << "\n\n\t Giao dich thuc hien thanh cong";
found = true;
}
}
file.close();
if (found == false)
cout << "\n\nKhong tim thay tai khoan";
}
But the ouput in the file did not change. Does I misunderstand anything here?
I strongly recommend you use your debugger to step through your code. That will enable you to understand what is really going on, and to see where you might be making mistakes.
It worked but just with current account which is the one that I just input. For example, I create account 1 first and then account 2, so both deposit and withdraw just work for the latest one which is 2 here is it. (Account number, userID, Type, and balance) I input deposit(which means menu_option = 2) and input amount = 1000. It work if I choose account = 2 (the latest one) and appear with balance = 2000 and does not delete the previous account 2.
For account 1, doesn't affected at all even though if I choose number 1 for account parameter it still appear.
So TK Chu TK Loai So du
1 Minh N 1000
2 Nam N 1000
2 Nam N 2000
Why doesn't it delete the older version ? And also why it could not affect to the previous account?