DEPOSIT AND WITHDRAW PROBLEM

Hello Guys
i am trying to create a simple banking program which has 2 options

1st Deposit
2nd Withdraw

but i am stuck with these 2 options i tried many times but i am not getting a satisfied result

here is my code if anyone can help me i will be very thankful.

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
#include<iostream>
#include<fstream>
#include<windows.h>
using namespace std;
main()
{
  string choice,name,password,money,space,check_name,check_password;
  cout<<"\tEnter 1 For Create Account\n\tEnter 2 For Login Account\n\n\tChoice: ";
  cin>>choice;
  if(choice=="1")
  {
    system("cls");
    cout<<"\t\t\tCreate Account\n\n\n";
    cout<<"\tEnter Your Name: ";
    cin>>name;
    cout<<"\tEnter Your Password: ";
    cin>>password;
    cout<<"\tEnter Your ballance to deposite: ";
    cin>>money;
    ofstream accounts("accounts.txt",ios::app|ios::in|ios::out);
    accounts<<name<<'\n';
    accounts<<password<<'\n';
    accounts<<money<<'\n';
    accounts.close();
   }
   else
   {
     if(choice=="2")
     {
	system("cls");
	cout<<"\t\t\tLogin Account\n\n\n";
	cout<<"\tEnter Your Name: ";
	cin>>check_name;
	cout<<"\tEnter Your Password: ";
	cin>>check_password;
	ifstream accounts("accounts.txt",ios::app|ios::in|ios::out);
	while(getline(accounts,name)&&getline(accounts,password)&&getline(accounts,money))
	{
	  if(name==check_name && password==check_password)
	  {
	    system("cls");
	    cout<<"\twellcome\t"<<name<<"\tBallance is\t"<<money<<"\n\n\n";
	    cout<<"\n\n\n\n\n\t1 For Deposite\n\t2 For Withdraw\n\n\tChose: ";
	    cin>>choice;
	    if(choice=="1")
	    {
	      cout<<"\n\tDeposite";
	      return 0;

			
	    }
	    else
            {
	      if(choice=="2")
	      {
		cout<<"\n\tWithdraw";
		return 0;
		
		
	      }
	      else
	      {
		system("cls");
		cout<<"invalid selection";
		return 0;
	      }
	    }
	  }
	  else
	  {
	    system("cls");
	    cout<<"Invalid name or password";
	  }
	}
     }
     else
    {
	system("cls");
	cout<<"invalid selection";
        return 0;
    }
  }
}
Hello adeel zamann,

I have to start with what you mean by "satisfied result"?

Your program has some problems beyond the fact that choice 2 needs a lot more work.

I would start with getting "choice" 1 to work before you move on.

If you have an input file to start with you need to post it, so everyonw can start with the same indformation.

The "Windows.h" header is not needed, but the "string" header file is.

You may want to consider defining "choice" as a "char" or an "int". Or as a string you could use
if (choice[0] == '1')

I rearranged the "cout " statement. You may find it easier to use:
1
2
3
4
5
6
7
cout <<
    "\n"
    " 1 For Create Account\n"
    " 2 For Login Account\n"
    " 3 For Exit\n"
    "  Enter Choice: ";
cin >> choice;

It produces the output of:

 1 For Create Account
 2 For Login Account
 3 For Exit
  Enter Choice:


I do not know if you want choice 3 or not.

ofstream accounts("accounts.txt", ios::app | ios::in | ios::out);. In this the "iso::in" is not need and is ignored because the "o" in "ofstream" says it is for output, so you can not use it for input. The oposit is true for the "ifstream".

Writing to your output stream can be done in 1 line like this:
1
2
3
4
accounts
    << name << '\n'
    << password << '\n'
    << money << '\n';


Whether it is for input or output. When it comes to the name what are you expecting to be entered? The formatted cin >> name will take from the input buffer up to a white space or "\n" whichever comes first, so if you want a name with a space in it consider using "getline".

In choice 2 you read "money" as a string, but never convert into a numeric value. It is much more difficult to add to a string than it is to a numeric variable.

You could define "money" as an "int" or a "double" and read, write and input to a numeric variable. It would save on some coding.

There is a start for now.

Andy
Hello Andy
Thank you for helping me

It's just only the deposit and withdraw problem in this program can you just help me in this
Adeel,

Have you learned about structures of classes?

How about functions?

I'm asking because the code would a lot easier using one or both of these.
Hi Dhayden

Actually I am studying in home I don't have the solid notes or a guider so I am just learning from a book I am not understanding both of these

Actually I did everything correct in this program but only stuck in the withdraw and deposit option
Line 5: All functions must have a type, so this should be int main()

Line 20: Like Andy said, make this ofstream accounts("accounts.txt", ios::app);

Okay, here's how you can get it working without much effort. The trick is that you have to write the all the data from accounts.txt to a new file ("accounts.new"). Along the way, you change the balance for the record that matters. Then, after creating accounts.new, you delete accounts.txt and rename accounts.new to accounts.txt.

Change line 36 to:
1
2
        ifstream accounts("accounts.txt");
        ofstream tmp("accounts.new");


Before line 39, convert "money" to a number, so add:
double balance = atof(money);

Change line 48 to
1
2
3
                    double amount;
                    cin >> amount;
                    balance += amount;


Make a similar change to line 57.

Delete line 65

After line 73 add this to write the record to the temp file. Note that this writes all the records, not just the one that was changed:
tmp << name << '\n' << password << '\n' << balance << '\n';

After line 74 add:
1
2
3
4
        accounts.close();
        tmp.close();
        remove("accounts.txt");
        rename("accounts.new", "accounts.txt");


Hello Dhayden

Can you write this in my code and send me the full code I will be very thankful as what you are saying I am understanding but I don't know how to do it
LOL,
What book are you using @adeel?
againtry

Can you please suggest me a best book through which I can learn all things and which have the examples of program in a book I will be very thankful
Can you write this in my code and send me the full code I will be very thankful as what you are saying I am understanding but I don't know how to do it


Something like this:

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	string choice, name, password, money, space, check_name, check_password;

	cout <<
		"\n"
		" 1 For Create Account\n"
		" 2 For Login Account\n"
		" 3 For Exit\n"
		"  Enter Choice: ";

	getline(cin, choice);

	if (choice == "1") {
		cout << "\t\t\tCreate Account\n\n\n";
		cout << "\tEnter Your Name: ";
		getline(cin, name);

		cout << "\tEnter Your Password: ";
		getline(cin, password);

		cout << "\tEnter Your balance to deposit: ";
		cin >> money;

		ofstream accounts("accounts.txt", ios::app);

		accounts << name << '\n';
		accounts << password << '\n';
		accounts << money << '\n';
		accounts.close();
	} else {
		if (choice == "2") {
			cout << "\t\t\tLogin Account\n\n\n";
			cout << "\tEnter Your Name: ";
			getline(cin, check_name);

			cout << "\tEnter Your Password: ";
			getline(cin, check_password);

			ifstream accounts("accounts.txt");

			if (!accounts) {
				cout << "No accounts have been created\n";
				return 0;
			}

			ofstream tmp("accounts.new");

			bool found = false;

			while (getline(accounts, name) && getline(accounts, password) && getline(accounts, money)) {
				double balance = stod(money);

				if (name == check_name && password == check_password) {
					found = true;

					cout << "\tWelcome\t" << name << "\tBalance is\t" << money << "\n\n\n";

					cout << "\n\n\n\n\n\t1 For Deposit\n\t2 For Withdraw\n\n\tChose: ";
					getline(cin, choice);

					if (choice == "1") {
						cout << "\n\tDeposit: ";

						double amount;
						cin >> amount;

						balance += amount;
					} else {
						if (choice == "2") {
							cout << "\n\tWithdraw: ";

							double amount;
							cin >> amount;

							balance -= amount;
						} else {
							cout << "Invalid selection\n";
							return 0;
						}
					}
				}
				tmp << name << '\n' << password << '\n' << balance << '\n';
			}

			accounts.close();
			tmp.close();

			if (!found) {
				cout << "Invalid name or password\n";

			remove("accounts.txt");
			rename("accounts.new", "accounts.txt");
			}
		} else
			if (choice != "3")
				cout << "Invalid selection\n";
	}
}

Last edited on
Can you please suggest me a best book through which I can learn all things and which have the examples of program in a book I will be very thankful


For a beginners C++ book, I would suggest Ivor Horton's
Beginning C++17: From Novice To Professional
https://www.amazon.co.uk/Beginning-C-17-Novice-Professional/dp/1484233654/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1599392272&sr=1-4

Note that the C++20 version is due for release 25 Sept - so I would hold off buying it until then if you're thinking of buying this.
https://www.amazon.co.uk/Beginning-C-20-Novice-Professional/dp/1484258835/ref=sr_1_1?dchild=1&keywords=horton+c%2B%2B&qid=1599392272&s=books&sr=1-1

Also for an on-line resource I would suggest
https://www.learncpp.com/


Can you write this in my code and send me the full code
Programming is like football. The only way you can get good at is to actually do it yourself. You will learn a lot by putting the code in by yourself.
Topic archived. No new replies allowed.