Banking question

So I am trying to make a banking system that gives you choices on what you want. The problem is I want to enter in option 2 and look for existing accounts through a text document,but don't exactly know how to do it. For example, if I enter in an existing account named Jason it will say "Found account for Jason".

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
 #include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void display(char& anwser);
void N_account(char& anwser,string & name);
void Exist(string & name, char& anwser);


int main()
{
	int start_money;
	string name;
	char anwser;
	display(anwser);
	N_account(anwser,name);
	Exist(name, anwser);
}

void display(char& anwser)
{
	cout << setw(65) << "=================" << endl;
	cout << setw(65) << "Banking Managment" << endl;
	cout << setw(65) << "=================" << endl;
	cout << setw(60) << "1.New account" << endl;
	cout << setw(65) << "2.Existing account" << endl;
	cout << setw(57) << "3.Deposit" << endl;
	cout << setw(56) << "4.Withdraw" << endl;
	cout << setw(62) << "5.Close account" << endl;
	cin >> anwser;
}

void N_account(char& anwser,string & name)
{
	if (anwser == '1')
	{
			ofstream outfile;
			outfile.open("Accounts.txt",std::ofstream::out|std::ofstream::app);
			cout << "Enter in first and last name for new account:";
			cin.ignore();
			getline(cin, name);
			outfile << name;
			outfile << '\n';
			outfile.close();

	}	
}

void Exist(string & name,char & anwser)
{
	if (anwser == '2')
	{
		ifstream infile;
		infile.open("Accounts.txt");
		cout << "Enter in your account:";
		cin.ignore();
		getline(cin, name);
		while (infile >> name)
		{
			cout << name << endl;
		}
		
	}
}
Last edited on
Lines 59-60: You're using name for two different things.
1) The name you want to search for.
2) The name you read from the file. This will overlay the name you entered on line 59.

Lines 60-63: These lines will simply display every name in the file.

Perhaps something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool Exist (char answer)
{	string name_to_search;
	string name_from_file;
	
	if (answer == '2')
	{	ifstream infile;
		infile.open ("Accounts.txt");
		cout << "Enter in your account:";
		cin.ignore();
		getline(cin, name_to_search);
		while (infile >> name_from_file)
		{	if (name_from_file == name_to_search)
			{	cout << name_to_search << endl;
				return true;
			}
		}
		return false;	// name_to_search not found 
	}
}
Okay it's starting to make sense now, but when I put in an existing account it doesn't acknowledge it. Does it work as intended on your end? I don't think the if statement is going through.
Topic archived. No new replies allowed.