GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Jun-2008, 16:41
herceca herceca is offline
New Member
 
Join Date: Dec 2007
Posts: 6
herceca is on a distinguished road

Help with small program


CPP / C++ / C Code:
#include <iostream>
using namespace std; //introduces namespace std
class Account
{
	private:
		int acctNumber;
		string acctName;	
		double acctBal;
	public:
		// 2 constructors
		Account ()
        {
		acctNumber=0;
		acctBal = 0;
        acctName=" ";
		}
	    Account (int a,string ac,double acc)
        {
		acctNumber=a;
		acctBal = acc;
		acctName = ac;
		}
		// sets
		int setNum()
		{
  
        }
        string setName()
        {
               
        }
        double setBal()
        {
             
        }
		// gets
		int getNum()
		{
            return acctNumber;
        }
		string getName()
		{
               return acctName;
        }
        double getBal()
        {
               return acctBal;
        }

};// end class
		
int main()
{
	const int SIZE = 15;
	Account bank[SIZE]= {Account (12345, "Smith", 1005.99),
						 Account (19822, "Lee", 20.21),
						 Account (18521, "Kim", 3061.77),
						 Account (85218, "Wall", 9855.33),
						 Account (85199, "Harry", 9077.88),
						 Account (91532, "Jess", 63112.99),
						 Account (43174, "Glee", 641.88),
						 Account (63173, "Corr", 841.55),
						 Account (23441, "Matt", 124.92),
						 Account (65185, "Card", 90.22),
						 Account (62188, "Kells", 529.77),
						 Account (32188, "Slyth", 863.22),
						 Account (90888, "Booth", 7422.88),
						 Account (52111, "Good", 321.99),
						 Account (87331, "Williams", 7317.21)};

cout <<"\tWelcome to Random Bank!";
cout <<"\n\tYou can make a deposit or withdraw ";
cout <<"\n\n\tPlease have your account number";
cout <<"\n\tor the name on the account ready\n";
		
// all of your code here
int choice;
cout << "Do you want to check account balance by name or account number?";
cout << endl;
cout << "1-Name";
cout << endl;
cout << "2-Account Number";
cout << endl;
cout << "3-Exit";
cout << endl;
cin >> choice;
string name;
int accNum;

if (choice == 1)
{
           cout << "Enter your name: ";
           cin >> name;
}

if (choice == 2)
{
           cout << "Enter your account #: ";
           cin >> accNum;
}

if (choice == 3)
{
           "Good bye.";
}
// give user a choice of searching by acct number, name or quit			
// make sure withdraw amount is not greater than balance		

for(int i=0; i<SIZE; i++)
{
    if(bank[i].getNum() == accNum && bank[i].getName() == name)
    {
       
    }
}

cout <<"Good Bye";	

	return 0;


How would I send the parts of the array to the class so I can compare name/number to check if its right so i can print account balance? then ask the person if he wants to withdraw or add(subtract if user wants to withdraw/add if the user wants to add).
  #2  
Old 01-Jun-2008, 18:38
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: help with small program


Quote:
Originally Posted by herceca
How would I send the parts of the array to the class so I can...
There are two portions to your question.
  1. One portion is asking about the classic "get/set" problem -- "How do I get & set members within a class instance?"
  2. Where should the logic for comparison, etc. go?
As for #1:
  • You could create unique member functions for "get" & "set" as you have already started.
  • You could simply overload method names as in the following:
    CPP / C++ / C Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class foo {
        string nm;
        int n;
    public:
        foo(const string&, const int);
        void name(const string &s) { nm = s; }
        string name() const { return nm; }
        void number(const int i) { n = i; }
        int number() const { return n; }
    };
    
    int
    main() {
        foo f("huey", 0);
    
        cout << f.name() << "\t" << f.number() << endl;
    
        f.name("dewey");
        f.number(1);
    
        cout << f.name() << "\t" << f.number() << endl;
    
        return 0;
    }
    
    foo::foo(const string &s, const int i) {
        nm = s;
        n = i;
    }
  • You could write a smaller number of similar functions which could accomplish the same thing through references, but this then relinquishes control of the value of members to the outside world. If you want more information on this method, ask.
As for #2, you can put such logic within yet more member functions, or you can make it external. It's your choice, but be able to justify your decision. My suggestion is to create functions for each type of transaction expected. In pseudocode:
CPP / C++ / C Code:
void debitAccount(Account &act, double amt) {
//  check to see if amount requested exceeds account balance
//  if unsuccessful, ask for another amount or return to higher level menu
//  subtract amount from account
}
This obviously makes the business logic external to the class. You may also want to consider making a hierarchy of menus. The highest level displays "Select transaction type" as debit, credit, view, etc. This may help in the overall organization.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
Pipeline freeze simulation darklightred C++ Forum 6 27-Jul-2006 20:37
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:57.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.