Mar 26, 2012 at 2:14am UTC
Hello, everyone.
Below is the code for an ATM machine. I am trying to program in a destructor method that will allow the user to close the program.
Every time I run the damn thing, I get this error:
" type `double' argument given to `delete', expected pointer "
I'm not sure how to fix it. Could someone please help? I'm very sleepy and I tried reading up on pointers but couldn't understand it.
#include <iostream>
#include <cstdlib>
using namespace std;
class BANK_ACCOUNT
{
private:
int PIN_NUM;
double BALANCE;
double AMOUNT;
int TYPE;
public:
BANK_ACCOUNT (int, double);
bool check_pin (int);
double deposit (double);
double get_balance ();
double withdraw (double);
void setpin (int);
int displaytrans (int);
~BANK_ACCOUNT();
};
BANK_ACCOUNT::BANK_ACCOUNT (int pin, double deposit)
{
PIN_NUM=pin;
BALANCE=deposit;
}
bool BANK_ACCOUNT::check_pin(int pin)
{
if (PIN_NUM==pin)
{
}
else
{
cout << "That PIN# is incorrect. Terrorist." << endl;
system("PAUSE");
exit (1);
}
}
double BANK_ACCOUNT::deposit (double DEP)
{
AMOUNT=BALANCE + DEP;
}
double BANK_ACCOUNT::get_balance ()
{
return AMOUNT;
}
double BANK_ACCOUNT::withdraw (double WITH)
{
AMOUNT=BALANCE - WITH;
}
void BANK_ACCOUNT::setpin (int newpin)
{
PIN_NUM=newpin;
}
int BANK_ACCOUNT::displaytrans (int CH)
{
TYPE=CH;
return TYPE;
}
BANK_ACCOUNT::~BANK_ACCOUNT()
{
delete BALANCE;
}
int main ()
{
cout << "Hello. I'm an ATM." << endl << endl;
cout << "(Program Created By Kira Pilot)" << endl << endl;
system("PAUSE");
system("CLS");
int pin_num;
cout << "Please create your one digit PIN# by entering a number and pressing ENTER: " << endl;
cin >> pin_num;
double in_deposit;
cout << "Please enter the deposit amount you wish to open your account with and press ENTER: " << endl;
cin >> in_deposit;
BANK_ACCOUNT open(pin_num, in_deposit);
system("CLS");
bool finished = false;
while( !finished )
{
int choice;
cout << "Please enter the value corresponding to your choice and press ENTER: " << endl;
cout << "1) Deposit money" << endl;
cout << "2) Withdraw money" << endl;
cout << "3) Display current balance" << endl;
cout << "4) Change PIN#" << endl;
cout << "5) Display the most recent transaction and type" << endl;
cout << "6) Close the account" << endl;
cout << "7) Exit the program" << endl;
cin >> choice;
system("CLS");
if (choice==1)
{
cout << "Please enter your PIN# and press ENTER:" << endl;
cin >> pin_num;
open.check_pin(pin_num);
double deposit;
cout << "Please enter the amount to be deposited and press ENTER: " << endl;
cin >> deposit;
open.deposit(deposit);
cout << "Your new account balance is: " << open.get_balance() << endl;
}
if (choice==2)
{
cout << "Please enter your PIN# and press ENTER:" << endl;
cin >> pin_num;
open.check_pin(pin_num);
system("CLS");
double withdraw;
cout << "Please enter the amount to be withdrawn and press ENTER: " << endl;
cin >> withdraw;
open.withdraw(withdraw);
cout << "Your new account balance is: " << open.get_balance() << endl;
}
if (choice==3)
{
cout << "Please enter your PIN# and press ENTER:" << endl;
cin >> pin_num;
open.check_pin(pin_num);
system("CLS");
cout << "Your account balance is: " << open.get_balance() << endl;
}
if (choice==4)
{
cout << "Please enter your PIN# and press ENTER:" << endl;
cin >> pin_num;
open.check_pin(pin_num);
int new_pin;
cout << "Please enter your new, one digit PIN# and press ENTER: " << endl;
cin >> new_pin;
open.setpin(new_pin);
}
if (choice==5)
{
cout << "Please enter your PIN# and press ENTER:" << endl;
cin >> pin_num;
open.check_pin(pin_num);
cout << "Your most recent transaction type was transaction type " << open.displaytrans(choice) << " on the below menu." << endl;
}
if (choice==6)
{
cout << "Your closing balance is: " << open.get_balance() << endl;
cout << "Closing your account." << endl;
//open.~BANK_ACCOUNT(pin_num);
}
char quit_time;
cout << "Do you wish to perform another transaction? [Y/N]: " << endl << endl;
cin >> quit_time;
if( quit_time == 'N' || quit_time == 'n' )
{
finished = true;
cout << "Thanks for using this ATM service." << endl;
}
}
system("PAUSE");
return 0;
}
Mar 26, 2012 at 7:50am UTC
call delete only on objects where you previously called new. Since you don't have any dynamic memory there's no need for a delete.
Seems that you misunderstand the use of a destructor? What do you think a destructor does?