Classes and Text files

I am trying to get a function to read account numbers from a bankaccount.txt. I only want it to read one account at a time but when I insert the account number it brings up a list off all the previous account numbers that have been added to the text file, here is the code that i am using -

I would only like 1 line/account read at a time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void BankAccount::displayBankAccount(void) // Account Display
{
	cout << "Please enter an Account Number: ";
	cin >> AccountNumber;
	string line;
	ifstream myfile ("bankDatabase.txt", ios::in);
	if (myfile.is_open())
	{
		while ( myfile.good() )

		{	 
			getline (myfile,line);
			cout << line << endl;
		}	 

		myfile.close();

	}
}
Last edited on
This will read just one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void BankAccount::displayBankAccount(void) // Account Display
{
	cout << "Please enter an Account Number: ";
	cin >> AccountNumber;
	string line;
	ifstream myfile ("bankDatabase.txt", ios::in);
	if (myfile.is_open())
	{
		 
			getline (myfile,line);
			cout << line << endl;
		        myfile.close();

	}
}
Thanks Moschops, but now it only reads the first account number whatever number I enter, any ideas?
Last edited on
You said you wanted it to read one account at a time - it does now read only one account at a time (the same account, every time, which is one account, at a time).

If that's not what you want, you'll have to tell us what you do want. Do you want it to read all the account numbers? If not, which account numbers do you want it to read? What counts as a "time". Each time the function is called? Each time the program is run? Something else?
Last edited on
I want it to read the details of the account number that gets input, not the same account details of whatever account number is input, I thought this seemed fairly obvious!
I think he wants to input an account number in the function and have it return details of that account. Would that be about right?

Otherwise, the cin operation in the function is completely pointless.
I thought this seemed fairly obvious!

Not remotely. Welcome to the world of poorly defined requirements; the cause of the majority of software cost bloat, overrun and bugs. :)

I want it to read the details of the account number that gets input

Well then we'll need to know what the text file looks like. Is it like this:

1
2
3
4
5
6
7
8
9
10
11
...
accountNumber
someAccountDetail
someOtherAccountDetail
accountNumber
someAccountDetail
someOtherAccountDetail
accountNumber
someAccountDetail
someOtherAccountDetail
...


or

1
2
3
4
5
...
accountNumber someAccountDetail someOtherAccountDetail
accountNumber someAccountDetail someOtherAccountDetail
accountNumber someAccountDetail someOtherAccountDetail
...


or something else?

Your code needs to do 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
void BankAccount::displayBankAccount(void) // Account Display
{
	cout << "Please enter an Account Number: ";
	cin >> AccountNumber;
	string line;
        int theAccountNumberReadFromTheFile = 0;
	ifstream myfile ("bankDatabase.txt", ios::in);
	if (myfile.is_open())
	{
		        while (AccountNumber != theAccountNumberReadFromTheFile && myfile.good())
                        {
		  	   // read in account number and store into theAccountNumberReadFromTheFile 
                         }
                         
                        if  (AccountNumber == theAccountNumberReadFromTheFile)
                        {
			   cout << line << endl;                        
                        }
                        else
                        {
                            cout << "Not found";
                         }
		        myfile.close();

	}
}
Last edited on
Thanks Moschops, my textfile looks like the second text file demo that you have displayed, I did it this way as I thought it maybe easier to use the getline function when accessing account details. Didnt mean to sound sarcastic when writing the last message, just my naivity i think.

Would I take this same approach when deleting single records from the database?
Last edited on
This, then, might be closer to what you want. To delete lines from a text file, you have to copy the whole thing to a temporary file (except the bits you don't want anymore), delete the original, and rename the new one.


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
void BankAccount::displayBankAccount(void) // Account Display
{
	cout << "Please enter an Account Number: ";
	cin >> AccountNumber;
	string line;
        int theAccountNumberReadFromTheFile = 0;
	ifstream myfile ("bankDatabase.txt", ios::in);
	if (myfile.is_open())
	{
		        while (AccountNumber != theAccountNumberReadFromTheFile && myfile.good())
                        {
		  	   // read in account number and store into theAccountNumberReadFromTheFile 
                             myfile  >> theAccountNumberReadFromTheFile;
                             myfile >> details1;
                             myfile >> details1;
                            .....
                         }
                         
                        if  (AccountNumber == theAccountNumberReadFromTheFile)
                        {
			   cout << theAccountNumberReadFromTheFile  
                                    << details1
                                     << details2  <<  endl;                        
                        }
                        else
                        {
                            cout << "Not found";
                         }
		        myfile.close();

	}
}
Thanks Moschops, this has clarified a lot.
Topic archived. No new replies allowed.