Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When I tried answer posted on cramster, it kept going until it overloaded. I wan

ID: 3634888 • Letter: W

Question

When I tried answer posted on cramster, it kept going until it overloaded.
I want to know what is problem in this programming code.

Please revise this code.
============================================================================
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

#include <fstream>
using std::ofstream;

#include<cstdlib>

void createOldMaster()
{
ofstream oldmast;
char nam[15];
int accNumber;
double balance;

oldmast.open("oldmast.dat");

if(!oldmast.is_open())
{
cout<<"Unable to open the oldmaster file.";
exit(1);
}

cout <<" Please enter the following details of the personal";
while (true)
{
cout << " Acc.Number (enter 0 to end): ";
cin >> accNumber;

if(accNumber == 0)
break;
else
{
cout <<" Name:";
cin >> nam;
cout << " Balance:";
cin >> balance;

oldmast<<accNumber<<""<<nam<<""<<balance<<endl;
}
}
}

void createTransactions()
{
ofstream trans;
int accNumber;
double balance;

trans.open("trans.dat");

if(!trans.is_open())
{
cout<<"Unable to open the transactions file.";
exit(1);
}

cout<<" Please enter the following details of the personal";
while(true)
{
cout<<" Acc.Number(enter 0 to end):";
cin >> accNumber;

if(accNumber==0)
break;
else
{
cout<<" Balance:";
cin>>balance;

trans<<accNumber<<""<<balance<<endl;
}
}
}

int main()
{
createOldMaster();
createTransactions();

return 0;
}

------------------------------------------------------------------------------------------------------

Explanation / Answer

Dear,
Complete code

#include <iostream>
using namespace std;
#include <iomanip>
using std::setprecision;
using std::setiosflags;
#include <fstream>

using std::ofstream;
using std::ifstream;
#include <cstdlib>
#include <ctime>
int main()
{
const char *firstNames[] = { "Walter", "Alice", "Alan", "Mary", "Steve",
"Gina", "Tom", "Cindy", "Ilana", "Pam" },
*lastNames[] = { "Red", "Blue", "Yellow", "Orange", "Purple",
"Green", "Violet", "White", "Black", "Brown" };
ofstream outOldMaster( "oldMast.dat" ),
outTransaction( "trans.dat" );
int z;
srand( time( 0 ) );
if ( !outOldMaster ) {
cerr << "Unable to open oldmast.dat ";
exit( EXIT_SUCCESS );
}
if ( !outTransaction )
{
cerr << "Unable to open trans.dat ";
exit( EXIT_SUCCESS );
}
// write data to "oldmast.dat"
cout << setiosflags( ios::fixed | ios::showpoint )
<< "Contents of "oldmast.dat": ";
outOldMaster.setf( ios::fixed | ios::showpoint );
for ( z = 1; z < 11; ++z ) {
int value = rand() % 10, value2 = rand() % 50;
outOldMaster << z * 100 << ' ' << firstNames[ z - 1 ] << ' '
<< lastNames[ value ] << ' ' << setprecision( 2 )
<< ( value * 100 ) / ( value2 / 3 + 4.32 ) << ' ';
cout << z * 100 << ' ' << firstNames[ z - 1 ] << ' '
<< lastNames[ value ] << ' ' << setprecision( 2 )
<< ( value * 100 ) / ( value2 / 3 + 4.32 ) << ' ';
}
// write data to "trans.dat"
cout << " Contents of "trans.dat": ";
outTransaction.setf( ios::fixed | ios::showpoint );
for ( z = 1; z < 11; ++z ) {
int value = 25 - rand() % 50;
outTransaction << z * 100 << ' ' << setprecision( 2 )
<< ( value * 100 ) / ( 2.667 * ( 1 + rand() % 10 ) )
<< ' ';
cout << z * 100 << ' ' << setprecision( 2 )
<< ( value * 100 ) / ( 2.667 * ( 1 + rand() % 10 ) ) << ' ';
}

outTransaction.close();
outOldMaster.close();
ifstream inMaster( "oldmast.dat" ), inTrans( "trans.dat" );
ofstream newMaster( "newMast.dat" );
if ( !inMaster ) {
cerr << "Unable to open oldmast.dat ";
exit( EXIT_SUCCESS );
}
if ( !inTrans ) {
cerr << "Unable to open trans.dat ";
exit( EXIT_SUCCESS );
}
if ( !newMaster ) {
cerr << "Unable to open newmast.dat ";
exit( EXIT_SUCCESS );
}
int account, mAccount;
double balance, mBalance;
char mFirst[ 20 ], mLast[ 20 ];
cout << "Processing... ";
inTrans >> account >> balance;
while ( !inTrans.eof() ) {
inMaster >> mAccount >> mFirst >> mLast >> mBalance;
while ( mAccount < account && !inMaster.eof() )
inMaster >> mAccount >> mFirst >> mLast >> mBalance;
if ( mAccount > account ) {
cout << "Unmatched transaction record: account " << account << ' ';
inTrans >> account >> balance;
}
if ( mAccount == account ) {
mBalance += balance;
newMaster << mAccount << ' ' << mFirst << ' ' << mLast
<< ' ' << mBalance << ' ';
}
inTrans >> account >> balance;
}
newMaster.close();
inTrans.close();
inMaster.close();
return 0;
}

Hope this will help you..

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote