Question

In program P83.cpp, make the above changes, save the program as ex83.cpp, compile and run the...

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;
}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the values for hours, minutes and seconds are a legal military time (i.e. 00 00 00 to 23 59 59) the program should display the formatted results (i.e. 12 34 56 should be displayed as 12:34:56). If there's an error for any of the entered values, an exception should be thrown and an error message should be displayed. Note, there are three exception conditions, one...
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
Something is either messed up in my operator overload <<, covertopostfix function, or my main output....
Something is either messed up in my operator overload <<, covertopostfix function, or my main output. Cannot figure it out. please help. Please comment your changes too. Program below is supposed to be outputting like this: InFix is:   A+B-C Post fix is:   A B + C - InFix is:   A+C Post fix is:   A C + InFix is:   x*(y+z)-(w+t) Post fix is:   x y z + * w t + - InFix is:   A+B*(C+D)-E/F+G+H Post fix is:   A B C...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...
Adding large numbers with linked list Requirement - in C++ - use file for the input...
Adding large numbers with linked list Requirement - in C++ - use file for the input (nums.txt) - (recommended) use only one linked list to hold intermediate answer and final answer. You may use another one to reverse the answer. - store the num reversely in the linked list. For example, the num 123 is stored as 3 (at first node), 2 (at second node) and 1 (at third node) in the linked list. - write a function that performs...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
The decimal values of the Roman numerals are: M D C L X V I 1000...
The decimal values of the Roman numerals are: M D C L X V I 1000 500 100 50 10 5 1 Remember, a larger numeral preceding a smaller numeral means addition, so LX is 60. A smaller numeral preceding a larger numeral means subtraction, so XL is 40. Assignment: Begin by creating a new project in the IDE of your choice. Your project should contain the following: Write a class romanType. An object of romanType should have the following...
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT