Hey Everyone! I have two - hopefully simple - problems that I need help with. I wrote an atm program in c++ that works except for two things...
(1) the part where I calculate the balance based on the interest rates for the checkings and savings accounts need to be put into an overloaded function and I dont know how to do that. I understand with an overloaded function both functions will have the same name but different types or number of arguments. But don't know exactly how to apply that And
(2) When the user inputs "e" or "E" for Exit I dont what the program to ask "Make another transaction?" I just want it to go to "Thank you for using MY Bank ATM!"

Here is my code:

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

using namespace std;

//void calculate_balance(
//void calculate_balance(
void exit();

int main()
  {
  
  double savings = 1000;
  double checking = 1000;
  double withdraw_checking = 0;
  double withdraw_savings = 0;
  double deposit_checking = 0;
  double deposit_savings = 0;
  double check_amount;
  string choice, yesno, depos_check, depos_savings;
 

  cout << "\n     *** MY Bank ATM ***";
  
  do
    {
       cout << "\n\nChoose C)hecking S)avings B)alance Ca)sh A Check E)xit: ";
       cin >> choice;
    
    
    // If choice is for the balance ...display totals of accounts.
    if (choice == "b" || choice == "B")
        {
        cout << "Checking Balance: $" << setw(8) << setprecision(2) << fixed 
               << checking << '\n';
        cout << "Savings Balance:  $" << setw(8) << setprecision(2) << fixed 
               << savings << endl;
        }
        
    // If choice is for Checking account, display option to withdraw/deposit    
    if (choice == "c" || choice == "C")
        {
        cout << "\nW)ithdrawal or D)eposit? :";
        cin >> depos_check;
        
        // If user asked to withdraw cash
        if (depos_check == "w" || depos_check == "W")
            {
            cout << "Enter amount to withdraw from checking account:";
            cin >> withdraw_checking;
            
            // If withdraw leaves money in account....can do action.
            if (checking - withdraw_checking > 0)
               {
               checking = - withdraw_checking + checking + (checking * .05);
               }
               
            // if withdrawal is makes account under 0, then says sorry cannot do.   
            else 
               {
               cout << "Cannot withdraw $" << withdraw_checking << ".00 from account( $" 
                      << checking << ".00 )";
               cout << "\nPlease deposit more funds or try a smaller withdrawal.";
               }
            }
            
        // If user asked to deposit funds. Asks how much then adds deposited funds to  
            account (checking)   
            if (depos_check == "d" || depos_check == "D")
            {
            cout << "Enter amount to deposit:";
            cin >> deposit_checking;
            checking = deposit_checking + checking + (checking * .05);
            }      
        }
        
       if (choice == "s" || choice == "S")
        {
        cout << "\nW)ithdrawal or D)eposit? :";
        cin >> depos_savings;
        
        // If user asked to withdraw cash
        if (depos_savings == "w" || depos_savings == "W")
            {
            cout << "Enter amount to withdraw from savings account:";
            cin >> withdraw_savings;
            
            // If withdraw leaves money in account....can do action.
            if (savings - withdraw_savings > 0)
               {
               savings =  - withdraw_savings + savings + (savings * .025);
               }
               
            // if withdrawal makes account under 0, then says sorry cannot do.   
            else 
               {
               cout << "Cannot withdraw $" << withdraw_savings << ".00 from account( $" << 
                      savings << ".00 )";
               cout << "\nPlease deposit more funds or try a smaller withdrawal.";
               }
            }
            
        // If user asked to deposit funds. Asks how much then adds deposited funds to 
            account (savings)   
        if (depos_savings == "d" || depos_savings == "D")
            {
            cout << "Enter amount to deposit:";
            cin >> deposit_savings;
            savings = deposit_savings + savings + (savings * .025);
            }      
        
          // If user asked to cash a check...input amount of check and display .
	if (choice == "ca" || choice == "Ca")
	{
	cout << "Enter amount of Check: " << setw(8) << setprecision(2) << fixed << 
                       check_amount << '\n';
	cout << "Here is your cash: " << setw(8) << setprecision(2) << fixed << 
                       savings  << endl;
	} 

	if (choice == "e" || choice == "E")
	{
  	 exit();
	}
	   }

    // Asks user if they wish to perform another task        
    cout << "\n\nMake another transaction? (Y/N):";
    cin >> yesno;
    
    }
  while (yesno == "y" || yesno == "Y");

	  
  cout << "Thank you for using MY Bank ATM!"<<endl;

  return 0;
  
}

void exit()
{
	cout << "Thank you for using MY Bank ATM!" <<endl;
}

An overloaded function could be like this. You could have two functions with the same name but which take different parameters. For example, you could have a function called "calculate_balance" that takes no parameters and a function called "calculate_balance" that takes an integer, for example. Something like this:

void calculate_balance ();   // takes noparameters
void calculate_balance (int number); // takes an integer

The program knows which function to call by the parameters it is passed. So something like this:

#include <iostream>
using namespace std;

void calculate_balance ();   // takes no parameters
void calculate_balance (int number); // takes an integer

int main ()
{
     calculate_balance ();    // will call the first function
     calculate_balance (5);  // will call the second function
     return 0;
}


void calculate_balance ()
{
     cout << "I am in calculate_balance.  I was passed no parameters" << endl;
}


void calculate_balance (int number)
{
     cout << "I am in calculate_balance. I was passed a " << number << endl;
}

If everything is declared the same - as double - how do I make the parameters different? I calculate the balance four different times and they all have the same format:

checking = - withdraw_checking + checking + (checking * .05);
checking = deposit_checking + checking + (checking * .05);
savings = - withdraw_savings + savings + (savings * .025);
savings = deposit_savings + savings + (savings * .025);

So what would I have to do to declare my overloaded function?
In my code in the first post I had those calculations inside the main but I have to move them into the overloaded function and call the function in the main.

For a simple mathematical equation (in other words, something that can be done in one or two lines), why do you need a function?

To overload a function you have to have at least one of the following

  • a different number of parameters
  • at least one corresponding parameter of different types

(The result type doesn't make a difference.)

Hope this helps.

The only way I can think of overloading a function that takes two doubles as parameters like that and overload them so they are different is something like this:

double calculate_balance (double, double);
double calculate_balance (double*, double);
double calculate_balance (double, double*);
double calculate_balance (double*, double*);

That's four different functions, all taking two doubles and returning a double. They are slightly different in that some of the parameters are double and some are pointers to doubles. Either that or something kind of silly like this:

double calculate_balance (double, double);
double calculate_balance (double, double, bool);
double calculate_balance (double, double, bool, bool);
double calculate_balance (double, double, int);

There you are adding meaningless parameters to the end that you don't need or use, but are just passing them in order to overload the function.

That would work, but it is not recommended.

The purpose of overloading a function is to allow the same operation to be done on different data.

For example, if you want to print a string you might have a couple of overloaded functions: void print( const char *s ); void print( const std::string s ); This allows you to account for the different ways in which you may think of a "string".

However, no matter how the string is represented the underlying operation is the same: the string is printed. That is to say that the purpose of overloading a function is not to give functions that do different things the same name. The functions may have to accomplish their goal differently, because the data given them are different, but the end result is the same: something gets printed.


In your case, then, I don't think you ought to overload anything. You are doing four distinct things: 1) depositing to checking, 2) withdrawing from checking, 3) depositing to savings, 4) withdrawing from savings. Since you are doing four things, make four functions.

Given that the only variance between depositing and withdrawing is the sign of the amount of money added, you could combine the checking functions into one, and the saving functions into another: double calculate_checking_balance( double capital, double principal ); double calculate_savings_balance( double capital, double principal ); So to deposit $12.00 into savings, you would say: savings = calculate_savings_balance( savings, 12.00 ); And to withdraw $21.99 from checking, you would say: checking = calculate_checking_balance( checking, -21.99 ); Hope this helps.

Thanks for all your responses. Duoas the reason I have to use overloaded function is because that's what my instructor required. Didn't make any sense to me but that's what they want.

Show your professor my post, ask him what we have misunderstood about your assignment, and post back with the response. :yawn:

:) Okay, i'll see what he says!

Since adding an interest rate to SOMETHING is a similar task for both checking and savings, it could be presented as an overloaded function. Especially if there were conditions placed on the interest vs. balance in the account, would make sense to do it that way.

The simplest invocation might be :

double total(savings, rate)
{
total = savings + (savings * rate);
return total;
}

double total(checking, rate)
{
total=checking + (checking * rate);
return total;
}

(I just did this to illustrate a point, not as "plug-in "code)

Yes, I know that. But that doesn't overload the function, it just adds another parameter.

double total( double captial, double adjust, double rate ) {
  return capital +adjust +(capital *rate);
  }

> If everything is declared the same - as double - how do I make the parameters different?
don't declare everything the same; create different types
your code will also be more typesafe.

#include <iostream>

struct temperature
{
  double value ;
  inline temperature( double t ) : value(t) {}
  inline operator double() const { return value ; }
};

struct pressure
{
  double value ;
  inline pressure( double t ) : value(t) {}
  inline operator double() const { return value ; }
};

void process_value_changed( temperature old_value, temperature new_value )
{
  std::cout << "temperature changed from " << old_value
            << " to " << new_value << '\n' ;
}

void process_value_changed( pressure old_value, pressure new_value )
{
  std::cout << "pressure changed from " << old_value
            << " to " << new_value << '\n' ;
}

int main()
{
  temperature t1= 26.7, t2 = 20.45 ;
  pressure p1 = 756.72, p2 = 761.04 ;
  process_value_changed( t1, t2 ) ;
  process_value_changed( p1, p2 ) ;

  // t1 = p2 ; // error; can't assign pressure to temperature
  t1 = double(p2) ; // ok; via explicit cast
}

Yes, I know that. But that doesn't overload the function, it just adds another parameter.

double total( double captial, double adjust, double rate ) {
  return capital +adjust +(capital *rate);
  }

I think you are over-complicating the definition.
As I demonstrated, it is simply using the the same function name with different varaibles.
see:
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr312.htm

OK, JRM, you've completely lost me. The link you gave gives the exact definition of an overloaded function that I've been giving throughout this whole thread.

Calling the same function with different variables is not overloading.

There must be a difference in type or number of static arguments to overload more than one function to have the same name.

Have I misunderstood what you are trying to say?

OK, JRM, you've completely lost me. The link you gave gives the exact definition of an overloaded function that I've been giving throughout this whole thread.

Calling the same function with different variables is not overloading.

There must be a difference in type or number of static arguments to overload more than one function to have the same name.

Have I misunderstood what you are trying to say?

So, you are saying since both checking and savings are doubles passed by value, the function is not overloaded? Then what would be the definition of double total(savings, rate)?
( not a rhetorical question)
Now that I think about this a bit more, I understand that the compiler could not distinguish between the two variables, as they are both doubles.
OK, so make one a reference and the other a value-DONE.

Exactly. However, making one a reference and one a value doesn't help. If I were to say:

double x, y;
x = foo( y );

How would the complier choose between double foo( double ); and double foo( double & ); ?

More infuriatingly, even if I were to say:

double x;
x = foo( 12.0 );

the compiler would give me a hard time about choosing one. (The literal could be promoted to a temporary!)

This is because resolving between overloaded functions is not academic. The C++ standard is notoriously wishy-washy about this stuff (though, that can't really be helped due to implicit conversions and type promotions). The basic way it is done is to choose based on the least-bad worst-choice.

If you want more reading, here're a couple short, easy-to-read articles I just googled that touch on this very problem, including trying to differentiate based on value or reference.
C++ Week 20: Function Overload Resolution
ACCU: Overload Resolution
(I learned some cool stuff reading them myself.)

Hope this helps.

Thanks.
The number of little C++ "gotchas" just boggles the mind!
Reading that article brought to mind a poker game where one hand beats another...
;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.