In-Class Exercise

Input and Output


This exercise works on an array of objects, accepting input for each element and displaying the data stored in each element in formatted fields.  The input includes a description in the form of a C-style null-terminated string.


Given Information

The following code is a solution to the Handout on Member Functions and Privacy.

Client Module

h2.h

 // Member Functions and Privacy
 // h2.h

 #define NO_TRANSACTIONS 3

h2.cpp

 // Member Functions and Privacy
 // h2.cpp

 #include "h2.h"
 #include "Transaction.h"

 int main() {
     Transaction tr;

     for (int i = 0; i < NO_TRANSACTIONS; i++) { 
         tr.enter();
         tr.display();
     }
 }

Transaction Module

Transaction.h

 // Member Functions and Privacy
 // Transaction.h

 struct Transaction {
   private:
     int acct;
     char type;
     double amount;
   public:
     void enter();
     void display() const; 
 };

Transaction.cpp

 // Member Functions and Privacy
 // Transaction.cpp

 #include <iostream>
 using namespace std;
 #include "Transaction.h"

 void Transaction::enter() {

     cout << "Enter the account number : ";
     cin  >> acct;
     cout << "Enter the account type (d for debit, c for credit) : "; 
     cin  >> type;
     cout << "Enter the account amount : ";
     cin  >> amount;
 }

 void Transaction::display() const {

     cout << "Account " << acct;
     cout << ((type == 'd') ? " Debit $" : " Credit $") << amount; 
     cout << endl;
 }

Your Task

Incorporate into the above code the following enhancements:

  1. allocate memory for an array of objects
  2. input a textual description
  3. output the data in formatted fields

Client Module

In your updated implementation file for the client module fill in the missing code:

  1. allocate memory for an array of Transactions
  2. identify the element in each call to a member function
 // Input and Output
 // h3.cpp

 #include <iostream>
 using namespace std;
 #include "h3.h"
 #include "Transaction.h"

 int main() {
     Transaction tr                  ;

     cout << "Enter " << NO_TRANSACTIONS << " Transactions" << endl; 
     for (int i = 0; i < NO_TRANSACTIONS; i++)
         tr   .enter();
     cout << endl;

     cout << "   Account Description             Credit     Debit" << endl; 
     for (int i = 0; i < NO_TRANSACTIONS; i++) {
         tr   .display();
         cout << endl;
     }

Transaction Module

Transaction.h

Add a data member that holds a description of the Transaction in a C-style null-terminated string of no more than 20 characters:

 // Input and Output
 // Transaction.h

 struct Transaction {
   private:
     int acct;
     char type;


     double amount;
   public:
     void enter();
     void display() const;
 };

Transaction.cpp

Fill in the missing code:

  1. include the header file for manipulators that take arguments
  2. discard the newline left in the buffer before reading the textual description
  3. accept the description in a C-style string of up to 20 characters
  4. format the output to display:
    • account - right justified in a field of 10
    • a single space
    • description - left justified in a field of 20
    • credit amount - right justified in a field of 10 with 2 decimal places
    • debit amount - right justified in a field of 20 with 2 decimal places
 // Input and Output
 // Transaction.cpp

 #include <iostream>

 using namespace std;
 #include "Transaction.h"

 void Transaction::enter() {

     cout << "Enter the account number : ";
     cin  >> acct;



     cout << "Enter the account type (d for debit, c for credit) : "; 
     cin  >> type;
     cout << "Enter the account amount : ";
     cin  >> amount;
 }

 void Transaction::display() const {




     if (type == 'd')
         cout <<         << amount;
     else
         cout <<         << amount;
 }



  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License