In program P83.cpp, make the above changes, save the program as ex83.cpp, compile and run the program and make sure the correct output is produced. Your input file, in_file.dat, will have four integer values representing 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 may
have:
50 34
86 73
Once your program has successfully compiled and run, overload the write_money function as well.
#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\n";
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. \n";
exit(1);
}
ous.open("out_file.dat");
if(ous.fail())
{
cout << "Failed to open
the output file. \n";
exit(1);
}
}
We will overload the >> so that writing an object of type AltMoney can be done using >>. In order to overload >> to read an object of type AltMoney, you need to make the following changes in the program.
1) In the class AltMoney, replace the read_money function with
the operator >> as described below:
class AltMoney
{
public:
AltMoney();
friend istream&
operator >>(istream& ins, AltMoney& m);
friend AltMoney operator
+(AltMoney m1, AltMoney m2);
friend void
write_money(ofstream& ous, AltMoney m);
private:
int dollars;
int cents;
};
2) Prototype (comes before the main function)
istream& operator >>(istream& ins, AltMoney&
m);
3) In the main:
get_streams(ins, ous);
ins >> m1;
ous << "The first money is:";
write_money(ous, m1);
ins >> m2;
ous << "The second money is:";
4) replace read_money function with:
istream& operator >>(istream& ins, AltMoney&
m)
{
int d, c;
ins >> d;
ins >> c;
if( d < 0 || c < 0)
{
cout << "Invalid dollars and cents, negative values\n";
exit(1);
}
m.dollars = d;
m.cents = c;
return ins;
}
The file is modified according to the specification and given below. When you run the code it expects that you have a file named in_file.dat. The program does not produce any output on screen but generates a file named out_file.dat. Please check out_file.dat for output from the program.
in_file.dat
========
50 34
86 73
ex83.cpp
=======
#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
class AltMoney
{
public:
AltMoney();
friend istream& operator >>(istream& ins,
AltMoney& m);
friend AltMoney operator +(AltMoney m1, AltMoney m2);
friend void write_money(ofstream& ous, AltMoney m);
private:
int dollars;
int cents;
};
istream& operator >>(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);
ins >> m1;
ous << "The first money is:";
write_money(ous, m1);
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;
}
istream& operator >>(istream& ins, AltMoney&
m)
{
int d, c;
ins >> d;
ins >> c;
if( d < 0 || c < 0)
{
cout << "Invalid dollars and cents, negative values\n";
exit(1);
}
m.dollars = d;
m.cents = c;
return ins;
}
void get_streams(ifstream& ins, ofstream& ous)
{
ins.open("in_file.dat");
if(ins.fail())
{
cout << "Failed to open the input file. \n";
exit(1);
}
ous.open("out_file.dat");
if(ous.fail())
{
cout << "Failed to open the output file. \n";
exit(1);
}
}
out_file.dat (produced by program)
===========
The first money is:$50.34
The second money is:$86.73
The sum is:$137.07
Get Answers For Free
Most questions answered within 1 hours.