How can I change the value that already input to file?

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();
}


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
void Menu()
{
	int account_number;
	Account myAccount;
	char menu_option;
	do
	{
		system("cls");
		cout << "\n\n\n\tMenu";
		cout << "\n\n\t1. Tao tai khoan";//create account
		cout << "\n\n\t2. Nop tien";//top up money
		cout << "\n\n\t3. Rut tien";//withdraw
		cout << "\n\n\t4. Tra cuu tai khoan";//search account
		cout << "\n\n\t5. Danh sach tai khoan";//listofAccount
		cout << "\n\n\t6. Xoa tai khoan";//Delete account
		cout << "\n\n\t7. Sua tai khoan";//Change account 
		cout << "\n\n\t8. Thoat";//exit 
		cout << "\n\n\tChon menu (so tu 1 den 8): ";
		cin >> menu_option;
		system("cls");
		switch (menu_option)
		{
		case '1':
			myAccount.CreateAccount();  // reads from user
			SaveToFile(myAccount);  // saves to file
			break;
		case '2':
			cout << "\n\n\tChon so tai khoan: ";
			cin >> account_number;
			DepositOrWithdraw(account_number, 1);
			break;
		case '3':
			cout << "\n\n\tChon tai khoan: ";
			cin >> account_number;
			DepositOrWithdraw(account_number, 2);
		case '4':
			cout << "\n\n\tNhap so tai khoan: ";
			cin >> account_number;
			SearchAndPrintAccount(account_number);
			break;
		case '5':
			PrintAllAccountInFormat();
			break;
		case '6':
			cout << "\n\n\t Nhap so tai khoan: ";
			cin >> account_number;
			DeleteAccount(account_number);
			break;
		case '7':
			cout << "\n\n\tNhap so tai khoan: ";
			cin >> account_number;
			SearchAndModifyAccount(account_number);
			break;
		case '8':
			Eject();
			break;
		default:cout << "\a";
		}
		cin.ignore();
		cin.get();
	} while (menu_option != '8');

}


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
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";
}


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
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;
			}
			else if (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";
}

Last edited on
Or do I need to use truncate here? Not quite sure about that!
Last edited on
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(...).

The same applies to Withdraw(int amount).
I think that what I want in those functions.

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.
Last edited on
Here I changed it like this in DepositOrWithdraw()
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
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
			}
			else if (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.
Does I misunderstand anything here?
Yes you need to change Deposit() and Withdraw() as well. Currently the doing basically nothing.
#coder777

Sure, I'm done with it too.
1
2
3
4
void Account::Deposit(int amount)
{
	balance_ += amount;
}


1
2
3
4
void Account::Withdraw(int amount)
{
	balance_ -= amount;
}


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?
Last edited on
Actually the position of reading and writing is independent. For reading you use tellg/seekg for the positon. For writing it is tellp/seekp.

If you want to overwrite data you just read you need to set the read position as the write position:
1
2
3
4
5
6
7
8
9
10
11
12
		const std::streampos pos = file.tellg();
		file.read(reinterpret_cast<char *> (&account), sizeof(Account));
		if (account.GetAccountNumber() == account_number)
		{

...

			file.seekp(pos);
			file.write(reinterpret_cast<char*> (&account), sizeof(Account));
			cout << "\n\n\t Giao dich thuc hien thanh cong";
			found = true;
		}
#coder777

Wow it worked! Could I get the link that explain those syntax? That will help me a lot to understand!
Here is the link of documantation for fstream:

http://www.cplusplus.com/reference/fstream/fstream/?kw=fstream
Topic archived. No new replies allowed.