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