Ok, I have been trying to work this thing for a week. It is a program that is supposed to read from 2 files (.dat) and write to a third file (.dat) then update one of the first files.
i.e.

read oldmast.dat
read trans.dat

compare data

write data and changes to newmast.dat

update oldmast.dat to match info in newmast.dat

I know how to make it read the entire files, but I don't know how to make it pull only what I need to compare. I'm not looking for handouts, just a little help(don't want confusion on this).

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
	string line;

	int accountNumber;
	string firstName;
	string lastName;
	double currentBalance;
	double dollarAmount;
	
	ifstream inOldMaster("oldmast.dat", ios::in);
	ifstream inTransaction("trans.dat", ios::in);
	ofstream outNewMaster("newmast.dat", ios::in);


	//oldmast.dat
	if(!inOldMaster)
	{
		cerr << "File could not be opened." << endl;
		exit(1);
	}

	cout << "Contents of oldmast.dat \n\n";

	cout << left << setw(10) << "Account" << setw(13) << "First Name" << setw(13) << "Last Name" << setw(13) << "Balance" << endl << fixed << showpoint;

	while(inOldMaster >> accountNumber >> firstName >> lastName >> currentBalance)
	{
		cout << left << setw(10) << accountNumber << setw(13) << firstName << setw(7) << lastName << setw(13) << setprecision(2) << right << currentBalance << endl;
	}
	inOldMaster.close();

	cout << "\n\n\n\n";

	//trans.dat
	if(!inTransaction)
	{
		cerr << "File could not be opened." << endl;
		exit(1);
	}

	cout << "Contents of trans.dat \n\n";

	cout << "Account" << setw(15) << right << "Dollar Amount" << endl;

	while(inTransaction >> accountNumber >> dollarAmount)
	{
		cout << accountNumber << setw(13) << setprecision(2) << right << dollarAmount << endl;
	}

	cout << "\n\n\n";



	return 0;
}

contents of oldmast.dat
accountNumber firstName lastName currentBalance
100 john doe 234.43
200 sally sue 124.52
......

contents of trans.dat
accountNumber dollarAmount
100 100.00
200 -25.99
......

It is supposed to match the account numbers and add the dollar amount to the current balance to create a new current balance.

It is also supposed to print out a statement for account numbers that do not have a match, BUT I will try to get that myself first. I'm just trying to get it to do the other part of matching and adding. Thanks in advance for the help.

Recommended Answers

All 4 Replies

8 hrs and nobody knows how to help me out? I kinda need it. My brain is about fried from doing this for a week.

You only gave us half the information. The starting file data. It helps to give us the ending file data so we know the target we're shooting at.

You also didn't tell us exactly what the "little help" you needed was. Just that you needed help. Explain in detail your problem.

It is a program that is supposed to read from 2 files (.dat) and write to a third file (.dat) then update one of the first files.

I know how to make it read the entire files, but I don't know how to make it pull only what I need to compare.

It is supposed to match the account numbers and add the dollar amount to the current balance to create a new current balance.

I thought these pretty much said what I needed help with.

I need to know how to make it pull just the information that I need to compare. Then from there I have to add the currentBalance to the dollarAmount, but I will work on the math part once I am able to pull the information that I need.

> I need to know how to make it pull just the information that I need to compare.

For input, use std::ifstream

To read line by line from the stream, use std::getline

To extract the information from the line, use std::istringstream

For example,

std::ifstream old_file( "oldfile.dat" ) ;
    std::string line ;
    while( std::getline( old_file, line ) )
    {
        int ac_number ;
        std::string first_name ;
        std::string last_name ;
        double balance ;

        std::istringstream istm(line) ;
        if( istm >> ac_number >> first_name >> last_name >> balance )
        {
            // do something with the data
        }
    }

See: http://www.arachnoid.com/cpptutor/student2.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.