|
Re: help in retrieveItem using a list... I'm lost
Here is the whole code. I made some changes but most of the error I got were pointing to retrieveItem section. Please help I really don't know what those means. I just started working on c++ for few months.
Please explain what I need to do.
Thank you very much.
bankAccount.cpp
//bankAccount.cpp - Bank Account class implementation file
#include <iostream>
#include <string>
#include "bankAccount.h"
//Constructors
bankAccount::bankAccount() //initialize to 0 or just a place holder
: accountNumber(00000), accountName("John and Anna Doe"), accountType("Checking&Saving"), amountDeposit(0), amountWithdraw(0), interestRate(0), overdraft(0)
{
}
bankAccount::bankAccount(string acctName, string acctType, int acctNum, double amtDeposit, double amtWithdraw, double intRate, double overDraft)
{
//initialize to any size
accountName = acctName;
accountType = acctType;
accountNumber = acctNum;
amountDeposit = amtDeposit;
amountWithdraw = amtWithdraw;
interestRate = intRate;
overdraft = overDraft;
}
//Accessors
string bankAccount::getAcctName() const //returns the account name
{
return accountName;
}
string bankAccount::getTypeAcct() const //returns the account type
{
return accountType;
}
int bankAccount::getAcctNumber() const //returns the account number
{
return accountNumber;
}
double bankAccount::getAmtDeposit() const //returns the amount deposit
{
return amountDeposit;
}
double bankAccount::getAmtWithdraw() const //returns the amount withdraw
{
return amountWithdraw;
}
double bankAccount::getIntRate() const //returns the interest rate
{
return interestRate;
}
double bankAccount::getOvrdraft() const //returns the overdraft
{
return overdraft;
}
//Mutators
void bankAccount::setAcctName(string acctName) //change the account name
{
accountName = acctName;
}
void bankAccount::setAcctType(string acctType) //change the account type
{
accountType = acctType;
}
void bankAccount::setAcctNum(int acctNum) //change the account number
{
accountNumber = acctNum;
}
void bankAccount::setAmtDeposit(double amtDeposit) //change the account deposit
{
amountDeposit = amtDeposit;
}
void bankAccount::setAmtWithdraw(double amtWithdraw) //change the account withdraw
{
amountWithdraw = amtWithdraw;
}
void bankAccount::setNewAcctName(string acctName) //set the account name
{
accountName = acctName;
}
void bankAccount::setInterestRate(double intRate) //set the interest rate
{
interestRate = intRate;
}
void bankAccount::setOverdraft(double overDraft) //set the overdraft charges
{
overdraft = overDraft;
}
//Utilities
void bankAccount::displayCheckBal(ostream & out) const //display balance
{
out << "\nAccount Name: "<< accountName << endl;
out << "Account Number: " << accountNumber << endl;
out << "Balance: $" << amountDeposit << endl;
}
void bankAccount::displayAcctStatus(ostream & out) const //display account status
{
out << "\nAccount Name: " << accountName << endl;
out << "Account Number: " << accountNumber << endl;
out << "Account Type: " << accountType << endl;
out << "Amount Remaining: $" << amountDeposit << endl;
out << "Current Interest Rate: " << interestRate << endl;;
out << "Current Overdraft Charges: $" << overdraft << endl;
}
double bankAccount::calcInterest() //calculate and return amount deposit with interest rate
{
amountDeposit = amountDeposit + (interestRate * amountDeposit);
return amountDeposit;
}
double bankAccount::calcBalanceAmt() //calculate and return amount deposit after withrawal
{
amountDeposit = amountDeposit - amountWithdraw;
if (amountDeposit > 0)
{
return amountDeposit;
}
else
{
amountDeposit = amountDeposit - overdraft;
return amountDeposit;
}
}
bankAccount.h
//bankAccount.h - Bank Account class header file
#include <iostream>
#include <string>
using namespace std;
#pragma once
class bankAccount
{
friend class bankList;
public:
//Constructors
bankAccount();
bankAccount(string acctName, string acctType, int acctNum, double amtDeposit, double amtWithdraw, double intRate, double overDraft);
//Accessors
string getAcctName() const;
string getTypeAcct() const;
int getAcctNumber() const;
double getAmtDeposit() const;
double getAmtWithdraw() const;
double getIntRate() const;
double getOvrdraft() const;
//Mutators
void setAcctName(string acctName);
void setAcctType(string acctType);
void setAcctNum(int acctNum);
void setAmtDeposit(double amtDeposit);
void setAmtWithdraw(double amtWithdraw);
void setNewAcctName(string acctName);
void setInterestRate(double intRate);
void setOverdraft(double overDraft);
//Utilities
void displayCheckBal(ostream & out) const;
void displayAcctStatus(ostream & out) const;
double calcInterest();
double calcBalanceAmt();
private:
int accountNumber;
string accountName, accountType;
double amountDeposit, amountWithdraw, interestRate, overdraft;
};
bankList.cpp
#include <new>
#include <cassert>
using namespace std;
#include "bankList.h"
bankList::bankList(int maxAccount)
: accountSize(), accountCapacity(maxAccount)
{
myArrayPtr = new(nothrow) bankAccount[maxAccount];
assert(myArrayPtr !=0);
}
//class destructor
bankList::~bankList()
{
delete [] myArrayPtr;
}
//copy constructor
bankList::bankList(const bankList & origList)
: accountSize(origList.accountSize), accountCapacity(origList.accountCapacity)
{
myArrayPtr = new(nothrow) bankAccount[accountCapacity];
if(myArrayPtr != 0) //check if memory available
{
//copy origList's array into this new array
for (int i = 0; i < accountCapacity; i++)
{
myArrayPtr[i] = origList.myArrayPtr[i];
}
}
else
{
cerr << "*** Inadequate memory to allocate stack ***\n";
exit(1);
}
}
//asignment operator
bankList & bankList::operator=(const bankList & origList)
{
if (this != &origList) // check for bankList = bankList
{
accountSize = origList.accountSize;
accountCapacity = origList.accountCapacity;
//allocate a new array if necessary
if (accountCapacity != origList.accountCapacity)
{
delete [] myArrayPtr;
myArrayPtr = new(nothrow) bankAccount[accountCapacity];
if (myArrayPtr == 0) //check if memory available
{
cerr << "*** Inadequate memory to allocate stack ***\n";
exit(1);
}
}
//copy origList's array into this new array
for(int i = 0; i<accountCapacity; i++)
{
myArrayPtr[i] = origList.myArrayPtr[i];
}
}
return *this;
}
//definition of retreiveItem
void bankList::retrieveItem(bankAccount &key, bool &found)
{
int midPoint, first = 0, last = accountSize - 1;
bool moreToSearch = (first <= last);
found = false;
while (moreToSearch && !found)
{
midPoint = (first + last) / 2;
if (key.accountNumber < myArrayPtr[midPoint])
{
last = midPoint -1;
moreToSearch = (first <= last);
}
else
{
if (key.accountNumber > myArrayPtr[midPoint])
{
first = midPoint + 1;
moreToSearch = (first <= last);
}
else
{
found = true;
key.accountNumber = myArrayPtr[midPoint];
}
}
}
}
//definition of isFull
bool bankList::isFull() const
//full if list is at capacity
{
return (accountSize == accountCapacity);
}
//definition of empty()
bool bankList::empty() const
{
return accountSize == 0;
}
//definition of sizeIs()
int bankList::sizeIs() const
{
return accountSize;
}
//maximum Capacity;
int bankList::getCapacity()
{
return accountCapacity;
}
//Definition of insert()
void bankList::insert(bankAccount newEntry, int pos)
{
if (accountSize == accountCapacity)
{
cerr << "*** No Space for list element -- terminating execution ***\n";
exit(1);
}
if (pos < 0 || pos > accountSize)
{
cerr << "*** Illegal location to insert -- " << pos << ". List unchanged. ***\n";
return;
}
for (int i = accountSize; i > pos; i--)
{
myArrayPtr[i] = myArrayPtr[i-1];
}
myArrayPtr[pos] = newEntry;
accountSize;
}
//deleteItem
void bankList::deleteItem(int pos)
{
if (accountSize == 0)
{
cerr << "*** List is empty ***\n";
return;
}
if (pos < 0 || pos >= accountSize)
{
cerr << "Illegal location to delete -- " << pos << ". List unchanged. ***\n";
return;
}
for (int i = pos; i < accountSize; i++)
{
myArrayPtr[i] = myArrayPtr[i+1];
}
accountSize;
/*
int location = 0;
while (myArrayPtr[location] != key)
{
location ++;
}
for (int i = location +1; i < accountSize; i++)
{
myArrayPtr[i-1] = myArrayPtr[i];
}
accountSize--;*/
}
//definition of out operator
ostream & operator<< (ostream & out, const bankList & accNum)
{
accNum.display(out);
return out;
}
bankList.h
#include <iostream>
#include "bankAccount.h"
#ifndef BANK
#define BANK
typedef bankAccount bankAccount;
class bankList
{
public:
//class constructor
bankList(int maxAccount = 5);
//class destructor
~bankList();
//copy constructor
bankList(const bankList & origList);
//assignment
bankList & operator = (const bankList & origList);
// isFull operation
bool isFull () const;
//empty operation
bool empty() const;
//sizeIs operation
int sizeIs() const;
//getCapacity here
int getCapacity() const;
//retrieve item
void retrieveItem (bankAccount &key, bool &found); // find a "key"
//insertItem
void insert(bankAccount newEntry, int pos);
//delete item
void deleteItem(int pos);
//dislay
void display(ostream & out) const;
private:
int accountSize;
int accountCapacity;
bankAccount *myArrayPtr;
};
//prototype of output operator
ostream & operator<< (ostream &out, const bankList &accNum);
#endif
new errors after I made some changes:
c:\bankList.cpp(84): error C2677: binary '<' : no global operator found which takes type 'bankAccount' (or there is no acceptable conversion)
c:\bankList.cpp(84): error C2784: 'bool std: perator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const T1 *' from 'int'
c:\bankList.cpp(84): error C2784: 'bool std: perator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Ax> &' from 'int'
c:\bankList.cpp(84): error C2784: 'bool std: perator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Ax> &' from 'int'
c:\bankList.cpp(84): error C2784: 'bool std: perator <(const std: air<_Ty1,_Ty2> &,const std: air<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std: air<_Ty1,_Ty2> &' from 'int'
c:\bankList.cpp(84): error C2784: 'bool std: perator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'int'
c:\bankList.cpp(91): error C2677: binary '>' : no global operator found which takes type 'bankAccount' (or there is no acceptable conversion)
c:\bankList.cpp(91): error C2784: 'bool std: perator >(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const T1 *' from 'int'
c:\bankList.cpp(91): error C2784: 'bool std: perator >(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Ax> &' from 'int'
c:\bankList.cpp(91): error C2784: 'bool std: perator >(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Ax> &' from 'int'
c:\bankList.cpp(91): error C2784: 'bool std: perator >(const std: air<_Ty1,_Ty2> &,const std: air<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std: air<_Ty1,_Ty2> &' from 'int'
c:\bankList.cpp(91): error C2784: 'bool std: perator >(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'int'
c:\bankList.cpp(99): error C2440: '=' : cannot convert from 'bankAccount' to 'int'
c:\bankList.cpp(12 : error C2511: 'int bankList::getCapacity(void)' : overloaded member function not found in 'bankList'
|