I am not sure why my program isn't writing to the text file.

When I start the program it tells me that the file opens. Then I call the functions that are inherited from other classes and I would assume that it would write to the file and then display them. I was able to get this to work earlier without using the insertion and extraction operators, but I wanted to get it to work with them as well. Does anyone have any ideas?

//A Class for a checking account with a set interest.
class CheckingWithInterest
    : public SavingsAccount, public CheckingAccount
{
    friend ostream& operator<<(ostream& bank_Info_Out, const CheckingWithInterest& wIAcct);
    friend istream& operator>>(istream& bank_Info_In, const CheckingWithInterest& wIAcct);
public:
    CheckingWithInterest();
    void displayWithInterest();
};
//Default constructor for the special accounts
CheckingWithInterest::CheckingWithInterest()
: CheckingAccount(0, 0, 9999, 0), SavingsAccount(0.02)
{

}
// Displays the accounts information
void CheckingWithInterest::displayWithInterest()
{
    BankAccount::displayAccounts();
    SavingsAccount::displaySavAccount();
    CheckingAccount::displayCAccount();
}
//Displays interest rates on the accounts.
ostream& operator<<(ostream& bank_Info_Out, CheckingWithInterest& wIAcct)
{
    bank_Info_Out << setprecision(2);
    ofstream myfile;
    myfile.open("Account_Data.txt");
    if (!myfile.good())
        cout << "File couldn't be OPENED!!" << endl;
    else
    {
        cout << "File OPENED!!" << endl;
        wIAcct.displayWithInterest();
    }
    myfile.close();
    return bank_Info_Out;
}
//Operator that takes in accounts with an interest in the checking account.
istream& operator>>(istream& bank_Info_In, CheckingWithInterest& wIAcct)
{
    ifstream myfile;
    myfile.open("Account_Data.txt", ios::in);
    if (!myfile.good())
        cout << "File couldn't be OPENED!!" << endl;
    else
    {
        cout << "File OPENED!!" << endl;
        wIAcct.enterAccountData();
        wIAcct.getSavInfo();
        wIAcct.getCheckInfo();
    }
    myfile.close();
    return bank_Info_In;
}
/*Main function that makes and displays any number of accounts
it is currently set to a Maximum of 4 accounts for ease of logic
error checking. Change the number to make more bank accounts*/
int main()
{
    cout << fixed << setprecision(2);
    int x = 0;
    const int ERROR = 0;
    const int MAX_ACCTS = 2;
    //Makes an array of bank account objects
    CheckingWithInterest checkIntAccts[MAX_ACCTS];

    while (x < MAX_ACCTS)
    {
        cin >> checkIntAccts[x];
        x++;
    }
    for (x = 0; x < MAX_ACCTS; x++)
    {
        cout << checkIntAccts[x] << endl;
    }

    system("pause");
    return 0;
}

The << operator overload has one simple task -- write to the stream that is the first parmeter to the function. If you add other things to the function then it is doing too much.

I thought so, but my book is saying for the exercise. "Modify the insertion and extraction operators write and read the parts of a CheckingWithInterest seperated by spaces. Use those operators when writing to and reading from a file.

Isn't this what im trying to accomplish with the above code? If I am not thinking about it properly could I get revised on the matter?

I think it wants you to just do this:

ostream& operator<<(ofstream& out, CheckingWithInterest& wIAcct)
{
    // write out the data from wIacct.
    out << ?????
    return out;
}

int main()
{
   ofstream out("filename.txt");
   CheckingWithInterest c;
   out << c;
}
This article has been dead for over six months. Start a new discussion instead.