I am working on a project on inheritance, where DollarAmount is a parent class of class SpendingRecord. There seems to be a problem with these two input functions because whenever I type in -1 ( the delimiter ) it does not enter the if statement in the main, so the program never breaks and it accepts -1 as an acceptable dollar amount ( it should only accept -1 if the user no longer wishes to enter dollar amounts ). getDollar, getCent, and getDELIM are functions of the DollarAmount class, so I assumed SpendingRecord inherited them. Can anyone help?
void DollarAmount::inputDollar( istream &ins )
{
char decimal;
int dollar, cent;
cout << "\n$PrintDollar " << endl;
cout << "Enter the expenditure record (e.g., 1.95, coffee, enter -1 to end): $";
ins >> dollar;
if ( dollar != getDELIM())
{
ins >> decimal >> cent;
}
//
//check to make sure that the user inputs the correct amount
//ensures that dollar is in between 0 and 9999
//ensures that the user does not put 0 dollars and 0 cents
//ensures that the cents amount is between 0 and 99
//
while ( !setCent(cent) || !setDollar(dollar) || dollar == 0 && cent == 0 && dollar != getDELIM())
{
cout << "Invalid dollar/cent amount, please reenter: $";
ins >> dollar >> decimal >> cent;
}
while( ins.fail() ){
ins.clear();
ins.ignore(1000,'\n');
cout << "Wrong input types, please reenter: $";
ins >> dollar >> decimal >> cent;
}
setDollar(dollar);
setCent(cent);
}
void SpendingRecord::inputDollar( istream & in )
{
string itemName;
DollarAmount::inputDollar(in);
if ( dollar != getDELIM() )
{
in >> itemName;
getline( in, itemName );
SpendingRecord::itemName = itemName;
}
}
//in main
SpendingRecord *ptr = NULL;
ptr = new SpendingRecord[MAX];
for ( int i = 0; i < psize; i++ )
{
cin >> ptr[i];
//
//if user has put in the last element of the array
//allocate a new array of a larger size and set it to NULL
//copy the contents of smaller array into larger array
//delete the old array
//assign address location of new array to older array
//increase the physical size of the array
//
if ( i == psize - 1 )
{
SpendingRecord *tmp = NULL;
tmp = new SpendingRecord[psize*2];
for ( int j = 0; j < psize; j++ )
tmp[i] = ptr[i];
delete [] ptr;
ptr = tmp;
psize *= 2;
}
//if the user enters -1, break and do not include the last element ( i.e. -1 )
if ( ptr[i].getDollar() == SpendingRecord::getDELIM() && ptr[i].getCent() == 0 )
{
numElements = i;
break;
}
}