Update data in a file and Delete Data In a file

Hello Every one
i hope every one is doing well:)

i write a program for banking in c++ it is a sample program which has 2 options

1st: option is for create an account through which i am taking some information like account_no, the name of customer and the balance and store it in a file name (Accounts.txt)

2nd: option is for login through which the user will have to give the account_no and it will show the name and the balance of that account number:

in the 2nd option it has 3 other options

1st: option is for deposit.
2nd: option is for withdraw.
3rd: option is for delete an account.

but i am stuck in these 3 option
For deposit and withdraw i write a code but it is not working
and for the delete account option i don't have any idea to how to do that.

Here is my code if any one can help Me

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
string name,account_no,account_no_check,ballance;
cout<<"\tCreate Account\n\n\t";

cout<<"\tEnter Customer Account Number: ";
cin>>account_no_check;
					
fstream create("Accounts.txt",ios::app|ios::in|ios::out);
while(std::getline(create,name)&&std::getline(create,account_no)&&std::getline(create,ballance))
					
{
	if(account_no_check==account_no)
	{
		system("cls");
		cout<<account_no;
		cout<<name;
		cout<<ballance;
		string choose;
		create.close();
		cout<<"\n\n\n\tEnter 1 For Deposite Money\n\tEnter 2 For Withdraw Money";
		cin>>choose;
		if(choose=="1")
		{
			float withdraw,Money;
			cout<<"Amount to withdraw: ";
			cin>>withdraw;
			Money=std::stof(ballance);
			Money=withdraw+Money;
			fstream temp("temp.txt",ios::binary|ios::in|ios::out);
			temp<<account_no<<'\n';
			temp<<name<<'\n';
			temp<<Money<<'\n';
			temp<<'\n';
			temp.close();
			create.open("Accounts.txt",ios::binary|ios::in|ios::out);
			while(std::getline(create,name)&&std::getline(create,account_no)&&std::getline(create,ballance))
			{
				temp<<account_no<<'\n';
				temp<<name<<'\n';
				temp<<ballance<<'\n';
				temp<<'\n';
			}
			remove("Accounts.txt");
			rename("temp.txt","Account.txt");
			return 0;	
		}
		else
		{
			if(choose=="2")
			{
				float deposite,Money;
				cout<<"amount: ";
				cin>>deposite;
				Money=std::stof(ballance);
				Money=Money-deposite;
				fstream temp("temp.txt",ios::binary|ios::in|ios::out);
				temp<<account_no<<'\n';
				temp<<name<<'\n';
				temp<<Money<<'\n';
				temp<<'\n';
				temp.close();
				create.open("Accounts.txt",ios::binary|ios::in|ios::out);
				while(std::getline(create,name)&&std::getline(create,account_no)&&std::getline(create,ballance))
				{
					temp<<account_no<<'\n';
					temp<<name<<'\n';
					temp<<ballance<<'\n';
					temp<<'\n';
				}
				remove("Accounts.txt");
				rename("temp.txt","Account.txt");
				return 0;
			}
			else
			{
				if(choose=="3")
				{
					cout<<"Delete Account";
				}
			}
	else
	{
		cout<<"incorrect name or password";
		return 0;
	}
}
Last edited on
> For deposit and withdraw i write a code but it is not working
be more descriptive, the excerpt that you've posted does not allow us to test it
you should post enough code to reproduce your issue, including input files
may also show the expected and erroneous behaviour


> 1st: option is for deposit.
but you wrote
1
2
3
4
5
if(choose=="1")
{
   float withdraw;
   //...
   Money=withdraw+Money;
don't joke around.

also, try to not repeat yourself, the code for deposit and withdraw are almost equal.


> and for the delete account option i don't have any idea to how to do that.
¿are you records fixed size? ¿do you know about .read(), .write() in file streams?
if not, then copy the whole file (like you are doing in deposit and withdraw) but ignore the account_no record
What do you mean
like it has a full codes and I already mentioned it that I can't do these things which I write wrong and also for delete I really don't have any idea if am asking something on this platform it means I am stuck in it or I can't do it and this platform is for helping if you want to help me you should provide an example of the problem to me or can give me a solid material through which I can help
if you can help me ok otherwise you can leave
Someone will help me I am sure
Last edited on
What do you mean
like it has a full codes

Here are the errors I recieved when I tried to compile the code you've given:
https://godbolt.org/z/7bKrGd
Hello mbozzi thank you for helping me

Actually I didn't give the create account part in this code because I don't have problem with create account and login only the problem is to withdraw and deposit and the delete option which have already mentioned in program.
So if you can help me or can give me an example through which I can take help or can you send me the code similar to my program so I will take some help from it.
Ever onward with another Cheggs incident.
Hello againtry

No actually I am stuck in this and it's not working properly
> fstream create("Accounts.txt",ios::app|ios::in|ios::out);
Keep it simple,

ifstream infile("Accounts.txt");
If you want to read from the file.

ofstream outfile("Accounts.txt");
If you want to write to the file.

Trying ios::binary|ios::in|ios::out on a text file is making things unnecessarily hard.

> remove("Accounts.txt");
You also need to explicitly close the file (or let the constructed object go out of scope) before you start trying to move/rename/delete the file itself.
Hello Saleem c

Can you please provide a me an example are solid material through which I can get help in my code
Topic archived. No new replies allowed.