How do i change this into a function?

This is a discussion on How do i change this into a function? within the C++ Programming forums, part of the General Programming Boards category; Ok, i'm having real issues with functions, can anyone help me or make suggestions as to how i change this ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    Exclamation How do i change this into a function?

    Ok, i'm having real issues with functions, can anyone help me or make suggestions as to how i change this to a function?

    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    float deposit(float x);
    float withdrawal(float y);
    float transfer(float z);
    bool funds;
    
    using namespace std;
    
    void main()
    
    {	string filename, account, transaction;
    	fstream file;
    	float checking, savings, amount;
    
    	cout << "Welcome to the Bank!" << endl << endl;
    	cout << "Please Enter your name: ";
    	cin >> filename;
    
    	file.open(filename.data(),ios::in);
    
    	checking=0;
    	amount=0;
    	savings=0;
    	cout << fixed << setprecision(2) << endl << endl;
    	cout << left << setw(21) << "Transaction " << left << setw(8) << "Amount " << left << setw(9) << "Savings " << left << setw(9) << "Checking " << endl;
    	cout << "-------------------- " << "------- " << "-------- " << "--------" << endl;
    	
    	while(!file.eof())
    	{
    
    		file >> account;
    		file >> transaction;
    		file >> amount;
    	
    		cout << account << " " << left << setw(12) << transaction << " ";
    	
    //CHECKING AREA
    		
    		if(account=="checking")
    		{
    			if(transaction=="deposit")
    			{
    				checking = amount + checking;
    				cout << right << setw(6) << amount;
    				cout << right << setw(9) << savings;
    				cout << " " << right << setw(8) << checking << endl;
    			}
    			else if(transaction=="withdrawal")
    			{
    				if(amount<checking)
    				{
    					funds=true;
    				}
    				else
    				{
    					funds=false;
    				}
    
    				if(funds)
    				{
    					checking = amount - checking;
    					cout << right << setw(6) << amount;
    					cout << right << setw(8) << savings;
    					cout << " " << right << setw(8) << checking << endl;
    				}
    
    				else if(!funds)
    				{
    					cout << right << setw(6) << amount << " ";
    					cout << right << setw(10) << "Insufficient Funds" << endl;
    				}
    
    			}
    			else if(transaction=="transfer")
    			{
    				checking = checking - amount;
    				savings = amount + savings;
    				cout << right << setw(6) << amount;
    				cout << right << setw(9) << savings;
    				cout << " " << right << setw(8) << checking << endl;
    			}
    		}
    
    //SAVINGS AREA
    
    		else if(account=="savings")
    		{
    			if(transaction=="deposit")
    			{
    				savings = amount + savings;
    				cout << right << setw(7) << amount;
    				cout << right << setw(9) << savings;
    				cout << " " << right << setw(8) << checking << endl;
    			}
    			else if(transaction=="withdrawal")
    			{
    				savings = savings - amount;
    				cout << right << setw(7) << amount;
    				cout << right << setw(9) << savings;
    				cout << " " << right << setw(8) << checking << endl;
    			}
    		}
    	};
    cout << endl;
    
    
    
    }
    I know that true/false thing is redundant, but our teacher wanted us to use a bool to determine if theres enough funds according to true and falses. There are supposed to be three functions, a deposit, withdrawal, and transfer. I believe the trasnfer needs two paramaters.

    I tried cutting out a deposit if statement and try to put that in a function, but it wouldnt work.

    any help much apreciated.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    here is what the out put looks like. The code works, i'm just confused as to how i would make that a function.

    Welcome to the Bank!

    Please Enter your name: c:\mark.txt


    Transaction Amount Savings Checking
    -------------------- ------- -------- --------
    checking deposit 50.00 0.00 50.00
    savings deposit 100.00 100.00 50.00
    savings withdrawal21.50 78.50 50.00
    checking transfer 42.68 121.18 7.32
    checking deposit 14.95 121.18 22.27
    checking withdrawal25.00 Insufficient Funds
    savings deposit 1.25 122.43 22.27
    checking deposit 25.00 122.43 47.27

    Press any key to continue

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    http://35.9.168.96/C++/Program11.doc

    is the url of the assignment to clairfy things.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > I know that true/false thing is redundant, but our teacher wanted us to use a bool
    Maybe so, but that's no reason for it to be global.

    > void main
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    > while(!file.eof())
    This is also in the FAQ

    > I tried cutting out a deposit if statement and try to put that in a function, but it wouldnt work.
    It would have been nicer if you had at least posted your attempt. Now we've no idea how close you are.

    Perhaps
    Code:
    float do_savings ( string transaction, float savings, float amount ) {
    	if(transaction=="deposit")
    	{
    		savings = amount + savings;
    		cout << right << setw(7) << amount;
    		cout << right << setw(9) << savings;
    		cout << " " << right << setw(8) << checking << endl;
    	}
    	else if(transaction=="withdrawal")
    	{
    		savings = savings - amount;
    		cout << right << setw(7) << amount;
    		cout << right << setw(9) << savings;
    		cout << " " << right << setw(8) << checking << endl;
    	}
    	return savings;
    }
    Then in main, you have
    Code:
    else if(account=="savings") {
      savings = do_saving( transaction, savings, amount );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21