-2

I am developing a C++ banking system. I am able to get the float, newbal, values correctly and when I try to write to file, there is no data in the file.

else if (x == 2)
{
    cout << "You have selected option number 2. Deposit.\n";
    cout << "Please enter you account ID: ";
    cin >> ID;
    file.open("C:\\Users\\Raggulddon\\Desktop\\C++ supplement\\Cust_" + ID + ".dat", ios:: in | ios::out | ios::binary);

    if (!file)
    {
        cout << "Sorry the requested account could not be located.\n";
    }
    else
    {
        file >> firstname >> lastname;
        cout << endl << firstname << " " << lastname << endl;
        cout << "-----------------------------------\n";
        string line;
        while (getline(file, line))
        {

            // stringstream the getline for line string in file
            istringstream iss(line);

            if (iss >> date >> amount)
            {
                cout << date << "\t\t$" << showpoint << fixed << setprecision(2) << amount << endl;
                famount += amount;
            }

        }
        cout << "Your balance is $" << famount << endl;
        cout << "How much would you like to deposit today: $";
        cin >> amountinput;

        float newbal = 0;
        newbal = (famount += amountinput);


        cout << "\nYour new balance is: $" << newbal << ".\n";
        file << date << "\t\t" << newbal; //***This should be writing to file
        but it doesn 't.
            
file.close();

The text file looks like this:

Tony Gaddis

05/24/12 100

05/30/12 300

07/01/12 -300

// Console Output looks like this

Tony Gaddis

05/24/12 100

05/30/12 300

07/01/12 -300

Your balance is: #1

How much wuld you like to deposit: #2

Your new balance is: #1 + #2

write to file

close file.

// exits to main loop::::

How can I make it write to file and save it, and why is this happening.

I tried doing it with ostringstream as well considering how I used istringstream for the input. But it didn't work either:

float newbal=0;
newbal = (famount += amountinput);

ostringstream oss(newbal);
oss << date << "\t\t" << newbal;

I am trying to self teach C++ so any relevant information would be kindly appreciated.

Community
  • 1
  • 1

1 Answers1

0

If you want to write a text file, you should not use "ios::binary", when opening the file.

Simon
  • 1,616
  • 2
  • 17
  • 39