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

Modify the following program to overload the >> so that writing an object of typ

ID: 3732086 • Letter: M

Question

Modify the following program to overload the >> so that writing an object of type AltMoney can be done using >>. Your input file, in_file.dat, must have four integer values which represent, respectively the dollars and cents part of the first amount and the dollars and cents part of the second amount.
Example: for input $50.34 and $86.73, you have:
50 34
86 73

//This program adds money of two different people
// It reads the amounts for two people from an
// input file in_file.dat and writes the result into a file out_file.dat
#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
class AltMoney
{
public:
AltMoney();
friend void read_money(istream& ins, AltMoney& m);
friend AltMoney operator +(AltMoney m1, AltMoney m2);
friend void write_money(ofstream& ous, AltMoney m);
private:
int dollars;
int cents;
};
void read_money(istream& ins, AltMoney& m);
void get_streams(ifstream& ins, ofstream& ous);
void write_money(ofstream& ous, AltMoney m);
int main( )
{
ifstream ins;
ofstream ous;
AltMoney m1, m2, sum;
get_streams(ins, ous);
read_money(ins, m1);
ous << "The first money is:";
write_money(ous, m1);
read_money(ins, m2);
ous << "The second money is:";
write_money(ous, m2);
sum = m1+m2;
ous << "The sum is:";
write_money(ous, sum);
ins.close();
ous.close();
return 0;
}
AltMoney::AltMoney()
{
}
void write_money(ofstream& ous, AltMoney m)
{
ous << "$" << m.dollars << ".";
if(m.cents <= 9)
ous << "0"; //to display a 0 on the left for numbers less than 10
ous << m.cents << endl;
}
AltMoney operator +(AltMoney m1, AltMoney m2)
{
AltMoney temp; int extra = 0;
temp.cents = m1.cents + m2.cents;
if(temp.cents >=100)
{
temp.cents = temp.cents - 100;
extra = 1;
}
temp.dollars = m1.dollars + m2.dollars + extra;
return temp;
}
void read_money(istream& ins, AltMoney& m)
{
int d, c;
ins >> d;
ins >> c;
if( d < 0 || c < 0)
{
cout << "Invalid dollars and cents, negative values ";
exit(1);
}
m.dollars = d;
m.cents = c;
}
void get_streams(ifstream& ins, ofstream& ous)
{
ins.open("in_file.dat");
if(ins.fail())
{
cout << "Failed to open the input file. ";
exit(1);
}
ous.open("out_file.dat");
if(ous.fail())
{
cout << "Failed to open the output file. "; exit(1);
}
}

Explanation / Answer

here is your modified program : ------------->>>>>>>>>

#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
class AltMoney
{
public:
AltMoney();
friend void read_money(istream& ins, AltMoney& m);
friend AltMoney operator +(AltMoney m1, AltMoney m2);
friend ofstream& operator<<(ofstream& ous, AltMoney m);
private:
int dollars;
int cents;
};
void read_money(istream& ins, AltMoney& m);
void get_streams(ifstream& ins, ofstream& ous);
ofstream& operator<<(ofstream& ous, AltMoney m);
int main( )
{
ifstream ins;
ofstream ous;
AltMoney m1, m2, sum;
get_streams(ins, ous);
read_money(ins, m1);
ous<<"The first money is:";ous<<m1;
read_money(ins, m2);
ous<<"The second money is:";ous<<m2;
sum = m1+m2;
ous<<"The sum is:";ous<<sum;
ins.close();
ous.close();
return 0;
}
AltMoney::AltMoney()
{
}
ofstream& operator<<(ofstream& ous, AltMoney m)
{
ous << "$" << m.dollars << ".";
if(m.cents <= 9)
ous << "0"; //to display a 0 on the left for numbers less than 10
ous << m.cents << endl;

return ous;
}
AltMoney operator +(AltMoney m1, AltMoney m2)
{
AltMoney temp; int extra = 0;
temp.cents = m1.cents + m2.cents;
if(temp.cents >=100)
{
temp.cents = temp.cents - 100;
extra = 1;
}
temp.dollars = m1.dollars + m2.dollars + extra;
return temp;
}
void read_money(istream& ins, AltMoney& m)
{
int d, c;
ins >> d;
ins >> c;
if( d < 0 || c < 0)
{
cout << "Invalid dollars and cents, negative values ";
exit(1);
}
m.dollars = d;
m.cents = c;
}
void get_streams(ifstream& ins, ofstream& ous)
{
ins.open("in_file.dat");
if(ins.fail())
{
cout << "Failed to open the input file. ";
exit(1);
}
ous.open("out_file.dat");
if(ous.fail())
{
cout << "Failed to open the output file. "; exit(1);
}
}

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