14 Replies - 706 Views - Last Post: 18 March 2010 - 07:40 AM Rate Topic: -----

#1 stshrive  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 10

Help With Friend Functions And Printing

Posted 17 March 2010 - 07:42 PM

Hello Everyone,

I have a working program that has a bank class / object and stores account information. But I have to alter the program by creating a friend function that will print all account information (account number, name, and amount in account). I am really kind've clueless here...I know that we have to put the statement friend before the function (called totalprint). Any help you could provide to add this function in would be helpful!

Header File (bankaccount.h)
#ifndef bankaccount_today
#define bankaccount_today

class BankAccount 
{
private:
    char name[40];
    double balance;
public:
    BankAccount();
	~BankAccount();
	friend void totalprint(void) const;
	void print(void) const;
    void deposit(void);
	void withdraw(void);
	void add(void);
	char acctnum[25];
};

#endif



Main (main.cpp)
#include<iostream>
#include "bankaccount.h"
using namespace std;

int selectmenu(void);
int counter=0;
int record;
BankAccount *iptr = new BankAccount[num];
int main()
{
	cout<<endl<<"How Many Records Would You Like To Enter: ";
    int x;
	int num;
    cin>>num;

    int choice = selectmenu();

	while (choice != 6)
	{
		switch(choice){
		case 1:
			if (counter<num)
			{
				iptr[counter].add();
				counter++;
			}
			else
	            cout<<endl<<"Max number of accounts ("<<num<<") already created!"<<endl;
			break;

		case 2:
			cout<<endl<<"Enter The Record Number For Your Deposit: ";
			cin>>record;
			while (record > counter && record > 0)
			{
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
				cin>>record;
			}
				
			iptr[record-1].deposit();
			break;

		case 3:
			cout<<endl<<"Enter The Record Number For Your Withdrawl: ";
            cin>>record;
            while (record > num && record > 0)
            {
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
                cin>>record;
			}
            
			iptr[record-1].withdraw();      
            break;

		case 4:
			cout.setf(ios_base::fixed, ios_base::floatfield);
			cout<<endl<<"Enter The Record Number You Would Like To Print: ";
			cin>>record;
			while (record > counter && record > 0)
            {
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
                cin>>record;
			}
			
			iptr[record-1].print();

			break;
		}
		case 5:
			cout.setf(ios_base::fixed, ios_base::floatfield);
			iptr[record-1].totalprint
			
		choice=selectmenu();
	}
    return 0;
}

int selectmenu(void)
{
	int selection;

	cout<<endl<<"Please Make A Seclection From Our Menu";
	cout<<endl<<"1 - Add";
	cout<<endl<<"2 - Deposit";
	cout<<endl<<"3 - Withdrawl";
	cout<<endl<<"4 - Print";
	cout<<endl<<"5 - Print All Records";
	cout<<endl<<"6 - Quit";
	
	cin>>selection;
	
	return selection;
}



Function Cpp (methods.cpp)
#include<iostream>
#include<cstring>
#include<iomanip>
#include "bankaccount.h"
using namespace std;

BankAccount::BankAccount()
{
	strcpy(name,"No Name");
	strcpy(acctnum,"0");
	balance=0.0;
}
BankAccount::~BankAccount()
{
}

void BankAccount::add(void)
{

	cout<<endl<<"Enter the account #: ";
    cin>>acctnum;
	cin.ignore();

	cout<<endl<<"Enter The First And Last Name: ";
    cin.getline(name,50);

	cout<<endl<<"Enter The Starting Balance: ";
    cin>>balance;

    return;
}
void BankAccount::print(void) const
{
	cout << "Here Is Your Bank Account Information" << endl;
    cout << "Name: " << name << endl;
    cout << "Account Number: " << acctnum << endl;
    cout << "Balance: " << setprecision(2)<< balance << endl;
}
void BankAccount::printall(void) const
{

void BankAccount::deposit(void)
{
      double cash;
      cout<<"Enter The Amount You Wish To Deposit: ";
      cin>>cash;
      balance += cash;
      cout<<endl<<"Your new balance is: "<< balance <<endl;
}

void BankAccount::withdraw(void)
{
    double cash;
    cout<<"Enter The Amount You Wish To Withdrawl: ";
    cin>>cash;

    if (cash < 0)
      cout << "Illegal transaction attempted";
    else 
      balance -=cash;
      cout<<endl<<"Your new balance is: "<< balance<<endl;
 
}



As you can see I started putting in some things...

~Storm
P.S. - Please see attached picture for what my output should look like!

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Help With Friend Functions And Printing

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 07:42 PM

Can you use a friend operator overload as the printer or does it have to be a separate function?
Was This Post Helpful? 0
  • +
  • -

#3 stshrive  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 10

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 07:45 PM

I am pretty sure it has to be a separate function!!
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 07:52 PM

Okie dokie. Here's a small example:

class Test{
private:
	int data;
public:
	Test(int n): data(n) {};
	~Test() {};
	int getData()		{return data;};
	friend void print(Test&);
};

void print(Test& test){
	cout << "Data: " << test.data << endl;
	return;
}

int main(){
	Test example(5);
	print(example);
	return 0;
}





For further reading
Was This Post Helpful? 0
  • +
  • -

#5 stshrive  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 10

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 07:55 PM

Alright. I kind've get it. But I need to print out all content that is stored in the array. I am pretty sure I have to have some sort of loop but can't figure out how to code it..
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 07:58 PM

Something like this I imagine, unless you;'re referring to another array that the BankAccount object is holding.

void print(BankAccount& acc){
    for(int i = 0; i < 25; i++){
         cout << "Account #: " << i + 1 << " " << acc.acctNum[i] << endl;
    }
}


Was This Post Helpful? 0
  • +
  • -

#7 stshrive  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 10

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 08:02 PM

Ok. Why is there a '&' in the prototype?
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 08:09 PM

That means a reference to the object is passed. In fact, it should be a const reference, since we do not want the program ,under any circumstances, to modify our object.


As for references in general: Read this and this
Was This Post Helpful? 0
  • +
  • -

#9 stshrive  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 10

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 08:28 PM

Cool. So I coded the information in, but I came up with 9 or so errors. Here is the updated code.

bankaccount.h
class BankAccount 
{
private:
    char name[40];
    double balance;
public:
    BankAccount();
	~BankAccount();
	friend void totalprint(void);
	void print(void) const;
    void deposit(void);
	void withdraw(void);
	void add(void);
	char acctnum[25];
};

#endif



main.cpp
#include<iostream>
#include "bankaccount.h"
using namespace std;

int selectmenu(void);
int counter=0;
int record;
BankAccount *iptr = new BankAccount[num];
int main()
{
	cout<<endl<<"How Many Records Would You Like To Enter: ";
    int x;
	int num;
    cin>>num;

    int choice = selectmenu();

	while (choice != 6)
	{
		switch(choice){
		case 1:
			if (counter<num)
			{
				iptr[counter].add();
				counter++;
			}
			else
	            cout<<endl<<"Max number of accounts ("<<num<<") already created!"<<endl;
			break;

		case 2:
			cout<<endl<<"Enter The Record Number For Your Deposit: ";
			cin>>record;
			while (record > counter && record > 0)
			{
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
				cin>>record;
			}
				
			iptr[record-1].deposit();
			break;

		case 3:
			cout<<endl<<"Enter The Record Number For Your Withdrawl: ";
            cin>>record;
            while (record > num && record > 0)
            {
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
                cin>>record;
			}
            
			iptr[record-1].withdraw();      
            break;

		case 4:
			cout.setf(ios_base::fixed, ios_base::floatfield);
			cout<<endl<<"Enter The Record Number You Would Like To Print: ";
			cin>>record;
			while (record > counter && record > 0)
            {
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
                cin>>record;
			}
			
			iptr[record-1].print();

			break;
		}
		case 5:
			cout.setf(ios_base::fixed, ios_base::floatfield);
			iptr[record-1].totalprint();
			break;
		choice=selectmenu();
	}
    return 0;
}

int selectmenu(void)
{
	int selection;

	cout<<endl<<"Please Make A Seclection From Our Menu";
	cout<<endl<<"1 - Add";
	cout<<endl<<"2 - Deposit";
	cout<<endl<<"3 - Withdrawl";
	cout<<endl<<"4 - Print";
	cout<<endl<<"5 - Print All Records";
	cout<<endl<<"6 - Quit";
	
	cin>>selection;
	
	return selection;
}



methods.cpp
#include<iostream>
#include<cstring>
#include<iomanip>
#include "bankaccount.h"
using namespace std;

BankAccount::BankAccount()
{
	strcpy(name,"No Name");
	strcpy(acctnum,"0");
	balance=0.0;
}
BankAccount::~BankAccount()
{
}

void BankAccount::add(void)
{

	cout<<endl<<"Enter the account #: ";
    cin>>acctnum;
	cin.ignore();

	cout<<endl<<"Enter The First And Last Name: ";
    cin.getline(name,50);

	cout<<endl<<"Enter The Starting Balance: ";
    cin>>balance;

    return;
}
void BankAccount::print(void) const
{
	cout << "Here Is Your Bank Account Information" << endl;
    cout << "Name: " << name << endl;
    cout << "Account Number: " << acctnum << endl;
    cout << "Balance: " << setprecision(2)<< balance << endl;
}
void BankAccount::printall(const BankAccount& print) const
{
	for(int i=0; i < counter; i++)
	{
		cout << "Account #: " << i+1 << " " << print.acctnum[i] << endl;
		cout << "Name: " << i + 1 << " " << print.name[i] << endl;
		cout << "Ending Balance: " << i + 1 << " " << print.balance[i] << endl;
	}
}
void BankAccount::deposit(void)
{
      double cash;
      cout<<"Enter The Amount You Wish To Deposit: ";
      cin>>cash;
      balance += cash;
      cout<<endl<<"Your new balance is: "<< balance <<endl;
}

void BankAccount::withdraw(void)
{
    double cash;
    cout<<"Enter The Amount You Wish To Withdrawl: ";
    cin>>cash;

    if (cash < 0)
      cout << "Illegal transaction attempted";
    else 
      balance -=cash;
      cout<<endl<<"Your new balance is: "<< balance<<endl;
 
}



The errors I am getting are the following
1>------ Build started: Project: Chapter11-Exercise1, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\main.cpp(8) : error C2065: 'num' : undeclared identifier
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\main.cpp(69) : error C2046: illegal case
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\main.cpp(71) : error C2039: 'totalprint' : is not a member of 'BankAccount'
1>        c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\bankaccount.h(5) : see declaration of 'BankAccount'
1>methods.cpp
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\methods.cpp(9) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        c:\program files\microsoft visual studio 9.0\vc\include\string.h(74) : see declaration of 'strcpy'
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\methods.cpp(10) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        c:\program files\microsoft visual studio 9.0\vc\include\string.h(74) : see declaration of 'strcpy'
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\methods.cpp(39) : error C2039: 'printall' : is not a member of 'BankAccount'
1>        c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\bankaccount.h(5) : see declaration of 'BankAccount'
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\methods.cpp(40) : error C2270: 'printall' : modifiers not allowed on nonmember functions
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\methods.cpp(41) : error C2065: 'counter' : undeclared identifier
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\methods.cpp(44) : error C2248: 'BankAccount::name' : cannot access private member declared in class 'BankAccount'
1>        c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\bankaccount.h(7) : see declaration of 'BankAccount::name'
1>        c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\bankaccount.h(5) : see declaration of 'BankAccount'
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\methods.cpp(45) : error C2248: 'BankAccount::balance' : cannot access private member declared in class 'BankAccount'
1>        c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\bankaccount.h(8) : see declaration of 'BankAccount::balance'
1>        c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\bankaccount.h(5) : see declaration of 'BankAccount'
1>c:\users\storm shriver\documents\visual studio 2008\winter semester '10\projects\chapter11-exercise1\chapter11-exercise1\methods.cpp(45) : error C2109: subscript requires array or pointer type
1>Generating Code...
1>Build log was saved at "file://c:\Users\Storm Shriver\Documents\Visual Studio 2008\Winter Semester '10\Projects\Chapter11-Exercise1\Chapter11-Exercise1\Debug\BuildLog.htm"
1>Chapter11-Exercise1 - 9 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 08:58 PM

You have TONS of little errors all over, im not going to post every single one. I think you know how to debug but heres a problem with your print function
void BankAccount::printall(const BankAccount& print) const
{
        for(int i=0; i < counter; i++)
        {
                cout << "Account #: " << i+1 << " " << print.acctnum[i] << endl;
                cout << "Name: " << i + 1 << " " << print.name[i] << endl;
                cout << "Ending Balance: " << i + 1 << " " << print.balance[i] << endl;
        }
}



balance, name, and acctnum are not arrays here just single variables. You need to pass a pointer to the array of BankAccount that your previously allocated.

it would then look like this
void BankAccount::printall(const BankAccount *print) const
{
	for(int i=0; i < counter; i++)
	{
		cout << "Account #: " << i+1 << " " << print[i].acctnum << endl;
		cout << "Name: " << i + 1 << " " << print[i].name << endl;
		cout << "Ending Balance: " << i + 1 << " " << print[i].balance << endl;
	}
}



[code]
Was This Post Helpful? 0
  • +
  • -

#11 stshrive  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 10

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 10:55 PM

Thanks for everyones help. I have almost completed the program, but with the below code in main I ran into one error.

I put where the error is in the code!

#include <iostream>
#include <iomanip>
#include "bankaccount.h"
using namespace std;

int selectmenu(void);
int counter=0;
int record;
int const MAX = 100;

BankAccount printout[MAX];

void printall(void);

int main()
{
	cout<<endl<<"How Many Records Would You Like To Enter: ";
	int num;
    cin>>num;
	BankAccount *iptr = new BankAccount[num];
    int choice = selectmenu();

	while (choice != 6)
	{
		switch(choice){
		case 1:
			if (counter<num)
			{
				iptr[counter].add();
				counter++;
			}
			else
	            cout<<endl<<"Max number of accounts ("<<num<<") already created!"<<endl;
			break;

		case 2:
			cout<<endl<<"Enter The Record Number For Your Deposit: ";
			cin>>record;
			while (record > counter && record > 0)
			{
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
				cin>>record;
			}
				
			iptr[record-1].deposit();
			break;

		case 3:
			cout<<endl<<"Enter The Record Number For Your Withdrawl: ";
            cin>>record;
            while (record > num && record > 0)
            {
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
                cin>>record;
			}
            
			iptr[record-1].withdraw();      
            break;

		case 4:
			cout.setf(ios_base::fixed, ios_base::floatfield);
			cout<<endl<<"Enter The Record Number You Would Like To Print: ";
			cin>>record;
			while (record > counter && record > 0)
            {
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
                cin>>record;
			}
			iptr[record-1].print();
			break;
		
		case 5:
			printall();
			break;
		}

		choice=selectmenu();
	}
    return 0;
}

int selectmenu(void)
{
	int selection;

	cout<<endl<<"Please Make A Seclection From Our Menu";
	cout<<endl<<"1 - Add";
	cout<<endl<<"2 - Deposit";
	cout<<endl<<"3 - Withdrawl";
	cout<<endl<<"4 - Print";
	cout<<endl<<"5 - Print All Records";
	cout<<endl<<"6 - Quit";
	
	cin>>selection;
	
	return selection;
}
void printall()
{
	BankAccount account;
	cout.setf(ios_base::fixed, ios_base::floatfield);
	cout<<"Here Are The Account Ending Balances";
	cout.fill('*');
	cout.width(65);
	cout<<endl<<" ";
	cout.fill(' ');
	cout<<endl<<"  "<<setiosflags(ios::left)<<setw(15)<<"Account #"<<setw(30)<<"Name"
		<<setw(10)<<"Ending Balance"<<endl;
	cout<<setw(65)<<setfill('-')<<" "<<setfill(' ')<<endl;
	for (int i=1; i < counter; i++)
	{
		account.acctnum = printout[i].acctnum; // Here is where the error is!! Compiler Error 210
		strcpy(account.name,printout[i].name); // =' : left operand must be l-value
		account.balance = printout[i].balance;
		cout.fill(' ');
		cout<<endl<<"   "<<setiosflags(ios::left)<<setw(15)<<account.acctnum<<setw(30)
		<<account.name<<resetiosflags(ios::left)<<setw(10)<<setprecision(2)<<account.balance<<endl;
		cout<<setw(65)<<setfill('-')<<" "<<setfill(' ')<<endl;
	}

	return;
}


Was This Post Helpful? 0
  • +
  • -

#12 stshrive  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 10

Re: Help With Friend Functions And Printing

Posted 17 March 2010 - 11:21 PM

Well I figured that problem out by using strcopy. I don't know if this solved the problem or not...since when I run the program, and attempt to print out all account information it just prints out the default values of 0, No Name, 0.

Here is the final main code.
#include <iostream>
#include <iomanip>
#include "bankaccount.h"
using namespace std;

int selectmenu(void);
int counter=0;
int record;
int const MAX = 100;

BankAccount printout[MAX];

void printall(void);

int main()
{
	cout<<endl<<"How Many Records Would You Like To Enter: ";
	int num;
    cin>>num;
	BankAccount *iptr = new BankAccount[num];
    int choice = selectmenu();

	while (choice != 6)
	{
		switch(choice){
		case 1:
			if (counter<num)
			{
				iptr[counter].add();
				counter++;
			}
			else
	            cout<<endl<<"Max number of accounts ("<<num<<") already created!"<<endl;
			break;

		case 2:
			cout<<endl<<"Enter The Record Number For Your Deposit: ";
			cin>>record;
			while (record > counter && record > 0)
			{
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
				cin>>record;
			}
				
			iptr[record-1].deposit();
			break;

		case 3:
			cout<<endl<<"Enter The Record Number For Your Withdrawl: ";
            cin>>record;
            while (record > num && record > 0)
            {
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
                cin>>record;
			}
            
			iptr[record-1].withdraw();      
            break;

		case 4:
			cout.setf(ios_base::fixed, ios_base::floatfield);
			cout<<endl<<"Enter The Record Number You Would Like To Print: ";
			cin>>record;
			while (record > counter && record > 0)
            {
				cout<<endl<<"The Record Number You Enteretd Was Out Of Range"<<endl<<"Please Enter A Valid Record Number"<<endl;
                cin>>record;
			}
			iptr[record-1].print();
			break;
		
		case 5:
			printall();
			break;
		}

		choice=selectmenu();
	}
    return 0;
}

int selectmenu(void)
{
	int selection;

	cout<<endl<<"Please Make A Seclection From Our Menu";
	cout<<endl<<"1 - Add";
	cout<<endl<<"2 - Deposit";
	cout<<endl<<"3 - Withdrawl";
	cout<<endl<<"4 - Print";
	cout<<endl<<"5 - Print All Records";
	cout<<endl<<"6 - Quit"<<endl;
	
	cin>>selection;
	
	return selection;
}
void printall()
{
	BankAccount account;
	cout.setf(ios_base::fixed, ios_base::floatfield);
	cout<<"Here Are The Account Ending Balances";
	cout.fill('*');
	cout.width(65);
	cout<<endl<<" ";
	cout.fill(' ');
	cout<<endl<<"  "<<setiosflags(ios::left)<<setw(15)<<"Account #"<<setw(30)<<"Name"
		<<setw(10)<<"Ending Balance"<<endl;
	cout<<setw(65)<<setfill('-')<<" "<<setfill(' ')<<endl;
	for (int i=1; i < counter; i++)
	{
		strcpy(account.acctnum,printout[i].acctnum);
		strcpy(account.name,printout[i].name);
		account.balance = printout[i].balance;
		cout.fill(' ');
		cout<<endl<<"   "<<setiosflags(ios::left)<<setw(15)<<account.acctnum<<setw(30)
		<<account.name<<resetiosflags(ios::left)<<setw(10)<<setprecision(2)<<account.balance<<endl;
		cout<<setw(65)<<setfill('-')<<" "<<setfill(' ')<<endl;
	}

	return;
}



Here is the final bankaccount.h code
#ifndef bankaccount_today
#define bankaccount_today

class BankAccount 
{
private:
    char name[40];
    double balance;
	int printout;
public:
    BankAccount();
	~BankAccount();
	friend void printall(void);
	void print(void) const;
    void deposit(void);
	void withdraw(void);
	void add(void);
	char acctnum[25];
};

#endif



Here is the final methods file
#include<iostream>
#include<cstring>
#include<iomanip>
#include "bankaccount.h"
using namespace std;

BankAccount::BankAccount()
{
	strcpy(name,"No Name");
	strcpy(acctnum,"0");
	balance=0.0;
}
BankAccount::~BankAccount()
{
}

void BankAccount::add(void)
{

	cout<<endl<<"Enter the account #: ";
    cin>>acctnum;
	cin.ignore();

	cout<<endl<<"Enter The First And Last Name: ";
    cin.getline(name,50);

	cout<<endl<<"Enter The Starting Balance: ";
    cin>>balance;

    return;
}
void BankAccount::print(void) const
{
	cout << "Here Is Your Bank Account Information" << endl;
    cout << "Name: " << name << endl;
    cout << "Account Number: " << acctnum << endl;
    cout << "Balance: " << setprecision(2)<< balance << endl;
}
void BankAccount::deposit(void)
{
      double cash;
      cout<<"Enter The Amount You Wish To Deposit: ";
      cin>>cash;
      balance += cash;
      cout<<endl<<"Your new balance is: "<< balance <<endl;
}

void BankAccount::withdraw(void)
{
    double cash;
    cout<<"Enter The Amount You Wish To Withdrawl: ";
    cin>>cash;

    if (cash < 0)
      cout << "Illegal transaction attempted";
    else 
      balance -=cash;
      cout<<endl<<"Your new balance is: "<< balance<<endl;
 
}



:) Thanks.
Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Help With Friend Functions And Printing

Posted 18 March 2010 - 06:54 AM

Why are you doing a copy, just pass in the array of structs to ur function
Was This Post Helpful? 0
  • +
  • -

#14 stshrive  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 26-January 10

Re: Help With Friend Functions And Printing

Posted 18 March 2010 - 07:18 AM

I tried to do that, but I got the error C2106
Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Help With Friend Functions And Printing

Posted 18 March 2010 - 07:40 AM

Post the way u tried to do it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1