Files 1

Hello

I implemented this code to the teacher pseudocode and for some reason it doesnt complete the transaction

// Name: Liat Ledder
// Email: ledder.liat@titans.easternflorida.edu
// Purpose:
// To match the master and transaction file records and update the total
// paid for each client by adding the current week's price paid to the cumulative total.

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main( )
{

ifstream masterFile_in;
ifstream transactionFile_in;
ofstream newMasterFile_out;
double mClientNumber, mtotalClientCost, tClientNumber, titemClientCost;
string mClientfName, mClientlName;
cout << "Master File Updating Starting" << endl;
masterFile_in.open("Master.rtf");
transactionFile_in.open("Transaction.rtf");
newMasterFile_out.open("newMasterFile.rtf");
masterFile_in >> mClientNumber;
masterFile_in >> mClientfName;
masterFile_in >> mClientlName;
masterFile_in >> mtotalClientCost;
transactionFile_in >>tClientNumber;
transactionFile_in >>titemClientCost;



while(!(transactionFile_in.eof()))
{
while(!(masterFile_in.eof())&&(mClientNumber < tClientNumber))
{
newMasterFile_out << mClientNumber << " ";
newMasterFile_out << mClientfName << " ";
newMasterFile_out << mClientlName << " ";
newMasterFile_out << mtotalClientCost << endl;
masterFile_in>> mClientNumber;
masterFile_in>> mClientfName;
masterFile_in>> mClientlName;
masterFile_in>> mtotalClientCost;
}
if (masterFile_in.eof())
{
cout << "Error Client ID: "<< tClientNumber << " not in Master File."<<endl;
}
else if (mClientNumber == tClientNumber)
{

mtotalClientCost = mtotalClientCost + titemClientCost;
newMasterFile_out << mClientNumber << " ";
newMasterFile_out << mClientfName << " ";
newMasterFile_out << mClientlName << " ";
newMasterFile_out << mtotalClientCost<<endl;

masterFile_in >> mClientNumber;
masterFile_in >> mClientfName;
masterFile_in >> mClientlName;
masterFile_in >> mtotalClientCost;


}
else if (mClientNumber > tClientNumber)
{
cout << "Error Client ID: "<< tClientNumber << " not in Master File." << endl;
}
transactionFile_in >> tClientNumber;
transactionFile_in >> titemClientCost;


}
while (!(masterFile_in.eof()))
{
newMasterFile_out << mClientNumber << " ";
newMasterFile_out << mClientfName << " ";
newMasterFile_out << mClientlName << " ";
newMasterFile_out << mtotalClientCost<<endl;


masterFile_in >> mClientNumber;
masterFile_in >> mClientfName;
masterFile_in >> mClientlName;
masterFile_in >> mtotalClientCost;
}


cout << "Master File Updating Complete" << endl;

masterFile_in.close();
transactionFile_in.close();
newMasterFile_out.close();

system("PAUSE");
return 0;
}


You should check to insure your files opened properly before you try to read or write to them. Since your loop requires the eof flag to be set before it will exit, it'll never exit if the file didn't open.




Hi

Yes the files open succesfuly.. I wish there was a way I could attach them so you can see but there isnt...Im not dure what to do now:(
Yes the files open succesfuly.

How do you know? You're not checking to insure the files opened correctly, so maybe they didn't. You should always check to insure the files open correctly, failure is a very frequent occurrence.

Last edited on

Yes the files open succesfuly.. I wish there was a way I could attach them so you can see but there isnt...Im not dure what to do now:(

I think you're misunderstanding the idea here. Attaching the files and posting them here wouldn't help, it wouldn't show what happens when your program is executed.

One thing you can do, after a line like this for example:
 
masterFile_in.open("Master.rtf");

is to add something like this:
1
2
3
4
5
6
if ( !masterFile_in.is_open() )
{
    cout << "Error: masterFile_in is not open" << endl;
    system("PAUSE");
    return 1;
}

(Normally I wouldn't suggest using system(anything), but I don't want to distract too much from the main topic.)

You can add similar code after opening the other files. Then at least you'll be able to eliminate one possible cause of problems.

Topic archived. No new replies allowed.