Having trouble reading in new data into fstream

I am constructing a program that is basically a bank app. I know how to read in NEW "accounts" into a fstream file but the second option is to enter transactions into an EXISTING account. So far I was able to verify that the account (file) exists, but every time I try and just add data to the existing data, it ends up all mixed up and not at all what I want. I've tried fstream::app but I don't think I'm using it correctly. The following code is what I currently have but I've tried different ways also. (Sorry code is long)


//variables
int accountNumber;
string date, description;
float ammount;
string suffix=".dat";

//intro
cout << endl << "ACCOUNT TRANSACTION" << endl << endl;


//Checking if account exists
cout << "Enter your 3-digit account number: ";
cin >> accountNumber;

stringstream ss;
ss << accountNumber << ".dat";
string filename = ss.str();

fstream inB;
inB.open(filename.c_str());

if (inB.fail())
{
cout << endl << "ERROR: That account does not exist." << endl;
}


else
{
inB.close();

cout << "Enter the date (mm/dd/yyyy): ";
cin >> date;

cout << "Enter transaction title: ";
cin >> description;

cout << "Enter transaction ammount : $";
cin >> ammount;

fstream outB;
outB.open(filename.c_str(), fstream::app);

outB << endl << date << endl << description << endl << ammount << endl;

outB.close();

//read new file back out (test)
stringstream ss;
ss << accountNumber << ".dat";
string filename = ss.str();
string line;

ifstream inB;
inB.open(filename.c_str());

if (inB.is_open())
{
while ( getline(inB, line))
{
cout << line << endl;
}
}


}
How does the input file look like ?
How does the output look like ?
How should the output look like ?
I actually figured it out. I changed my code to:

cout << "Enter transaction title: ";
cin.ignore();
getline(cin, description);

instead of:

cout << "Enter transaction title: ";
cin >> description;

and it actually fixed my problem. I don't understand why. But here is the full instructions for what I was trying to accomplish: :

If the user selects B., request the Account Number, Date, Description, and Amount from the user. Open the file with the file name “xxx.dat” where xxx is the Account Number requested from the user. If the file does not open, alert the user and return to the menu. The file should be opened in read/write mode and set to append data so as to not overwrite existing data. Read the last line in the file, which should be the balance of the account the last time a transaction was added. Write the Date, Description, Amount, and calculated new balance, each of these four data elements on a separate line in the file. The calculated new balance can be found from the balance read from the file plus/minus the Amount. Close the file. Return to the menu. As an example, when step 3 is complete, the file might look like:

11/01/2017
Initial Deposit
1000
1000
11/14/2017
Titan Bookstore Online -245.67
754.33
Last edited on
Registered users can post here. Sign in or register to post.
  • 452471514 2018-02-18
  • 5299351513 2018-02-18
  • 3036511512 2018-02-18
  • 4224301511 2018-02-18
  • 6975481510 2018-02-18
  • 8259761509 2018-02-18
  • 7007851508 2018-02-18
  • 7796301507 2018-02-18
  • 567811506 2018-02-18
  • 2107511505 2018-02-18
  • 8923811504 2018-02-17
  • 6039601503 2018-02-17
  • 8788931502 2018-02-17
  • 4539021501 2018-02-17
  • 661851500 2018-02-17
  • 9152461499 2018-02-17
  • 4757661498 2018-02-17
  • 981131497 2018-02-17
  • 4385321496 2018-02-17
  • 5046771495 2018-02-17