0

Here is my code:

`

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Register
{
//Private members
private:
    //Creating an array for all the Prices
    static double Price[];

    //Declaring RegNUM and RecNUM
    int RegNUM, RecNUM;

    //Print out Item name in a loop
    static string items[];

    //Creates a Receipt Structure
    struct Receipt
    {
        //Takes in the number of orders and stores items bought
        int Orders[5];
        double Total = 0;
    };
    //Creating a pointer (RecPTR)
    Receipt *RecPTR;

//Public members
public:
    //Register Constructor
    Register(int,int);

    //Member functions
    void getOrder(int);
    void RecRETURN();

    //Operators
    Register operator+(const Register &right);
    Register operator=(const Register &right);
};

//Setting the prices for the items within an array
double Register::Price[5] = { 10.99, 19.99, 8.99, 4.99, 10.99 };

//Setting names of the items in order to be displayed in a loop
string Register::items[5] = { "Hammers", "Levels", "Screwdrivers", "Tape Measures", "Wrenches" };

//Constructor with default number of receipt (10) and default register number (0)
Register::Register(int defRec = 10, int defReg = 0)
{
    //Initialize RecPTR
    RecPTR = new Receipt[defRec];

    //Initialize orders to 0
    for (int j = 0; j < defRec; j++)
    {
        //Setting orders to 0
        for (int i = 0; i < 5; i++)
            RecPTR[j].Orders[i] = 0;
    }

    //Setting Register Number (RegNUM)
    RegNUM = defReg;

    //Setting the # of receipts
    RecNUM = defRec;
}

//getOrder function --- input order number to store in right receipt
void Register::getOrder(int OrderNUM)
{
    int numItem;

    //Displays order number and register number
    cout << "\nHow many of each item do you want to order"
    << "for order no. " << OrderNUM << " in cash register "
    << RegNUM << "?\n";


    //Getting amount for 5 different items
    for (int i = 0; i < 5; i++)
    {
        //Ask user for the amount of each item bought
        cout << items [i] << "?:  ";
        cin >> numItem;

        //Storing info. with the structure
        RecPTR[OrderNUM-1].Orders[i] += numItem;

        //Calculation for the total
        RecPTR[OrderNUM - 1].Total += (RecPTR[OrderNUM-1].Orders[i] * Price[i]);
    }

    //Calculate total price including the tax
    RecPTR[OrderNUM - 1].Total = RecPTR[OrderNUM - 1].Total * 1.13;

}

//Member Function to Return Receipts
void Register::RecRETURN()
{
    //Displaying receipts for both Register 1 and 2
    if (RegNUM <= 2)
    {
        //Displaying the items purchased for each order
        for (int j = 0; j < 2; j++)
        {
            //Displaying receipts
            cout << "\nThe total tools ordered on receipt no. "
            << j + 1 << " in cash register "
            << RegNUM << " are:\n";
            cout << items[0] << ":" << setw(13) << RecPTR[j].Orders[0] << endl;
            cout << items[1] << ":" << setw(14) << RecPTR[j].Orders[1] << endl;
            cout << items[2] << ":" << setw(8) << RecPTR[j].Orders[2] << endl;
            cout << items[3] << ":" << setw(7) << RecPTR[j].Orders[3] << endl;
            cout << items[4] << ":" << setw(12) << RecPTR[j].Orders[4] << endl;
            cout << "Total with Tax:" << setw(6) << "$" << fixed << setprecision(2)
            << RecPTR[j].Total << endl;
        }

        //Displaying the totals on the registers
        cout << "\nThe total for register " << RegNUM << " is:\n"
        << RegNUM << " are:\n";
        cout << items[0] << ":" << setw(13) << RecPTR[0].Orders[0] + RecPTR[1].Orders[0] << endl;
        cout << items[1] << ":" << setw(14) << RecPTR[0].Orders[1] + RecPTR[1].Orders[1] << endl;
        cout << items[2] << ":" << setw(8) << RecPTR[0].Orders[2] + RecPTR[1].Orders[2] << endl;
        cout << items[3] << ":" << setw(7) << RecPTR[0].Orders[3] + RecPTR[1].Orders[3] << endl;
        cout << items[4] << ":" << setw(12) << RecPTR[0].Orders[4] + RecPTR[1].Orders[4] << endl;
        cout << "Total with Tax:" << setw(6) << "$" << fixed << setprecision(2)
        << RecPTR[0].Total + RecPTR[1].Total << endl;

    }
    //Display register 3 info.
    else
    {
        //Displaying the total for Register 3
        cout << "\nThe two registers combined into register "
        << RegNUM << " are:\n";
        cout << items[0] << ":" << setw(13) << RecPTR[0].Orders[0] << endl;
        cout << items[1] << ":" << setw(14) << RecPTR[0].Orders[1] << endl;
        cout << items[2] << ":" << setw(8) << RecPTR[0].Orders[2] << endl;
        cout << items[3] << ":" << setw(7) << RecPTR[0].Orders[3] << endl;
        cout << items[4] << ":" << setw(12) << RecPTR[0].Orders[4] << endl;
        cout << "Total with Tax:" << setw(6) << "$" << fixed << setprecision(2)
        << RecPTR[0].Total << endl;
    }


}

// (+) Operator
Register Register::operator+(const Register &right)
{
    Register tmp;
    for (int j = 0; j < RecNUM; j++)
    {
        for (int i = 0; i < 5; i++)
        {
            /********************************************
             Adds reg 1,2 for receipt 'j' for each order
             they are then stored in receipt 0 of tmp
             ********************************************/
            tmp.RecPTR[0].Orders[i] += RecPTR[j].Orders[i] + right.RecPTR[j].Orders[i];
        }
        //Register 1 and 2s totals are added to the tmp total
        tmp.RecPTR[0].Total += RecPTR[j].Total + right.RecPTR[j].Total;
    }

    //Returns tmp
    return tmp;
}

// (=) Operator
Register Register::operator=(const Register &right)
{
    //Temporary Register
    Register temp;
    for (int i = 0; i < 5; i++)
    {
        //Storing the orders into the temporary object
        RecPTR[0].Orders[i] = right.RecPTR[0].Orders[i];

    }
    //Storing the total into the temporary object
    RecPTR[0].Total = right.RecPTR[0].Total;

    //Returns tmp
    return temp;
}



int main()
{
    //Creating the 3 register instances
    Register Reg1(10,1), Reg2(10,2), Reg3(0,3);

    cout << "Welcome to Hal's Hardware!\n";
    cout << "I see we have a number of customers\n";
    cout << "There are two cash register available\n\n";

    //Calls getOrder and passes the receipt number
    Reg1.getOrder(1);
    Reg1.getOrder(2);
    Reg2.getOrder(1);
    Reg2.getOrder(2);

    //Calling RecRETURN
    Reg1.RecRETURN();
    Reg2.RecRETURN();

    //Adding totals for Reg1 and Reg2, storing them in Reg3
    Reg3 = Reg1 + Reg2;

    //Calling RecRETURN
    Reg3.RecRETURN();

}

My error is line 50 (Register::Register(int defRec = 10, int defReg = 0)) I get addition of default argument on redeclaration makes this constructor a default constructor
I am using xcode and it wont run, but in visual it does and says nothing. I would like it to run in xcode.

Is there anyway of fixing this?`

4
Contributors
4
Replies
40
Views
1 Year
Discussion Span
Last Post by David W
0

The default arguments should be in the class declaration (header), not in the definition. Some compilers may accept this, but many will not.

0

And also ...

for starters ...

you need a destructor (to plug your memory leak) ...
and probably a copy constructor ...
and your code for the overloaded assignment operator is 'interesting' :)

Fix the above first ...

then (any) other errors will manifest more clearly :)

0

But if you are using ... not excessively large,

'pre-fixed max-size receipt tapes' ...
on each of your simulated 'cash-registers' ...

probably a much better idea than messing about with dynamic memory,
would be to just use an array,
pre-fixed at compile time,
to hold each 'max-sized receipt tape'

Maybe (an edit of your code) could be ... a little something like this:

// classCashRegister3.cpp //

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

using namespace std;

////////////////////////////////////////////////////
// done here ... so can easily change and re-compile
////////////////////////////////////////////////////
const int MAX_NUM_RECEIPTS = 10;
const double TAX_RATE = 1.13;

struct Item
{
    string name;
    double price;
} ;

const Item ITEMS[] =
{
    { "Hammers", 10.99 }, { "Levels", 19.99 },
    { "Screwdrivers", 8.99 }, { "Tape Measures", 4.99 },
    { "Wrenches", 10.99  }
} ;

const int NUM_ITEMS = sizeof(ITEMS) / sizeof(Item) ;
////////////////////////////////////////////////////


// a utility used here to facilitate VALID integer input //
int takeInInt( const string msg )
{
    int val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << "ERROR! Integers ONLY here please ...\n";
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}
///////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////
class Register
{
private:

    struct Receipt
    {
        double total; // to hold dollar total for each order

        // to hold the quantity of each item ordered for an order, i.e. a receipt
        int quantityOrderedItem[NUM_ITEMS];

        // default ctor...
        Receipt() : total(0)
        {
            for( int i = 0; i < NUM_ITEMS; ++ i )
                quantityOrderedItem[i] = 0;
        }
        void takeInOrder()
        {
            for( int i = 0; i < NUM_ITEMS; ++ i )
            {
                // firstly form the 'takeInInt' prompt ...
                ostringstream oss;
                oss << left << setw(23) << ITEMS[i].name + ":  ";

                quantityOrderedItem[i] = takeInInt( oss.str() );
                total += quantityOrderedItem[i] * ITEMS[i].price;
            }
            total *= TAX_RATE; // update total price to include tax
        }

        // so can easily output a receipt showing what was ordered ...
        friend ostream& operator << ( ostream& os, const Receipt& r )
        {
            for( int i = 0; i < NUM_ITEMS; ++ i )
                os << left << setw(14) << ITEMS[i].name + ":" << right << setw(10)
                   << r.quantityOrderedItem[i] << endl;
            return os;
        }
    } ;


    // to hold register id number & recNUM;
    int  regNUM, recNUM;

    // array to hold receipts ...
    Receipt aryRec[MAX_NUM_RECEIPTS];

public:
    // Register Constructor
    Register( int defReg = 0 );

    // 'special' overloaded operator = defined here ...
    // to be used in conjuction here with overloaded op + below //
    Register& operator = ( const Register& r )
    {
        if( this != &r )
        {
            for( int i = 0; i < MAX_NUM_RECEIPTS; ++ i ) aryRec[i] = r.aryRec[i];
        }
        return *this;
    }

    // overloaded operator +
    Register operator + ( const Register& right)  const;

    void takeInReceipt( int );
    void recRETURN();
} ;
///////////////////////////////////////////////////////////



// Ctor...  note has default register number of regNUM = 0
Register::Register( int defReg ) : regNUM(defReg), recNUM(0)  {}

void Register::takeInReceipt( int orderNUM )
{
    // Displays register number and order number ...
    cout << "\nCASH REGISTER " << regNUM << ", ORDER " << orderNUM;

    if( orderNUM < MAX_NUM_RECEIPTS && orderNUM >= 0 )
    {
        cout << "\nEnter the quantity ordered for each item.\n";
        aryRec[orderNUM-1].takeInOrder();
    }
    else if( orderNUM >= 0 )
        cout << "\nALL EXHAUSTED ... PLEASE CHANGE TAPE!\n";
    else
        cout << "\nERROR!  VALID input here is a number in range 0.."
             << (MAX_NUM_RECEIPTS-1) << "\n\n";
}

// Function to Return Receipts
void Register::recRETURN()
{
    cout << fixed << setprecision(2) ;

    //Displaying receipts for both Register 1 and 2
    if( regNUM <= 2 )
    {
        //Displaying the items purchased for each order
        for( int j = 0; j < 2; ++j )
        {
            //Displaying receipts
            cout << "\nThe total tools ordered on receipt no. "
                 << j + 1 << " in cash register " << regNUM << ":\n";

            cout << aryRec[j] << endl;

            cout << "Total with tax: $" << setw(7) << aryRec[j].total << endl;
        }

        //Displaying the totals on the registers
        cout << "\nThe total for register " << regNUM << ":\n";

        cout << aryRec[0] << endl;;

        cout << "Total with tax: $" << setw(7)
             << aryRec[0].total + aryRec[1].total << endl;

    }
    //Display register ***3*** info.
    else // was register 3 ... //
    {
        //Displaying the total for Register 3
        cout << "\nThe two registers combined into register " << regNUM << ":\n";

        cout << aryRec[0] << endl;

        cout << "Total with tax: $" << setw(7) << aryRec[0].total << endl;
    }
}

// def'n of overloaded operator +
Register Register::operator + ( const Register& right ) const
{
    Register tmp;
    for( int j = 0; j < MAX_NUM_RECEIPTS; ++j )
    {
        for( int i = 0; i < NUM_ITEMS; ++i )
        {
            /********************************************
             Adds reg 1,2 for receipt 'j' for each order
             they are then stored in receipt 0 of tmp
             ********************************************/
            tmp.aryRec[0].quantityOrderedItem[i] += aryRec[j].quantityOrderedItem[i] +
                                                  right.aryRec[j].quantityOrderedItem[i];
        }
        //Register 1 and 2s totals are added to the tmp total
        tmp.aryRec[0].total += aryRec[j].total + right.aryRec[j].total;
    }
    return tmp;
}



int main()
{
    cout << "Welcome to Hal's Hardware!\n";
    cout << "I see we have a number of customers.\n";
    cout << "There are two cash registers available.\n\n";

   // creating the 2 (customer) register instances
    Register reg1(1), reg2(2);

    // get first two orders for reg1 ...
    reg1.takeInReceipt(1);
    reg1.takeInReceipt(2);

    // get ... two orders for reg2 ...
    reg2.takeInReceipt(1);
    reg2.takeInReceipt(2);

    // show results for customer reg1 and reg2 ... for day's orders ...
    reg1.recRETURN();
    reg2.recRETURN();

    // store totals for reg1 and reg2  into   (store) reg3
    Register reg3(3);
    reg3 = reg1 + reg2;

    // show results for store for day ...
    reg3.recRETURN();
}

Edited by David W

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.