hey all

i am studying from a book,and i have to do an exercise.
i dont want code or anything i want tip or someone explain me how to do it.
i have a oldmast.dat file with some records a trans.dat file with records and i have to update the oldmast.dat to a new file,i did that. the files are sequential.

the exercise after i do that asks to be able to solve if an transcation file has 2 entrys with same account.

my code is:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void update(int,double &,ifstream &);
bool check(int,ifstream &);

int main()
{
    ifstream inOldMaster("oldmast.dat",ios::in);
    ifstream inTransaction("trans.dat",ios::in);
    ofstream outNewMaster("newMast.txt",ios::out);
    
    if(!outNewMaster || !inOldMaster || !inTransaction)
    {
        cout<<"file could not be opened\n";
        exit(1);
    }
    
    int accountNumber;
    string first,last;
    double currentBalance;
    
    while(inTransaction>>accountNumber>>currentBalance)
    {
        if(check(accountNumber,inOldMaster))
            cout<<"Unmatched transaction record for account number "<<accountNumber<<endl;
    }
    
    inTransaction.close();
    inOldMaster.close();
    inOldMaster.open("oldmast.dat",ios::in);
    inTransaction.open("trans.dat",ios::in);    
    
    while(inOldMaster>>accountNumber>>first>>last>>currentBalance)
    {
        update(accountNumber,currentBalance,inTransaction);
        outNewMaster<<accountNumber<<' '<<first<<' '<<last<<" $"<<currentBalance<<endl;
    }
    cout<<"input Completed\n";        
}

void update(int account,double &balance,ifstream &trans ) {
    double ammount;
    int accountNumber;
    while(trans>>accountNumber>>ammount) 
    {
        if(account==accountNumber)
        {
            balance+=ammount;
            break;
        }     
    }
}

bool check(int account,ifstream &master) {
    int accountNum;
    double balance;
    string first,last;
    
    while(master>>accountNum>>first>>last>>balance)
    {
        if(accountNum==account)
            return false;        
    }
    return true;
}

the 2 question is can i do write something else instead of close and open files?lines 31-34

and at sequential files how i can fix if an entry exist more than 2 times?

example:
lets say the file trans2.dat has the following
Account Ammount
300 83.89
700 80.78
700 1.53

i am almost sure that i have to use seekg/seekp or tellp/tellg but i ahvent quite understand how to use them.

thanks for your time
someone that studies c++ for hobby

dont declare the ifstream stuff in a function, try declaring everything separate.
in other words, please don't include any arguments in your functions or methods. that wawy things should be clearer. goodluck

This article has been dead for over six months. Start a new discussion instead.