RSS Forums RSS

help with c++ coursework - code so far included.

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 1
Reputation: alexdm50 is an unknown quantity at this point 
Solved Threads: 0
alexdm50 alexdm50 is offline Offline
Newbie Poster

help with c++ coursework - code so far included.

  #1  
Nov 21st, 2008
hi im new to daniweb im currently working on a piece of work. the idea is to create a bank account with deposit and withdrawal functions. create student business and current accounts (this is all roughly done and a case of copying and pasting for each account type with a different set of questions) . what it requires me to do is allow 10 accounts to be opened and then allow the user to search for each one and deposit and withdraw from each one. would anyone have any suggestions on how i could do this. below is my code so far and thankyou for taking the time to look. also it requires me to take an object orientated approach and im rather confused what this entails, any suggestions again would be appreciated.

1st - BANK.CPP

#include <iostream>
#include <string>  

using namespace std;

int main()
{
// Declare Variables
	int withdraw;
	int sqone;
	int sqtwo;
	int bone;
	string colname;
	string name;
	string sname;
	int housenumber;
	string postcode;
	string dob;
	string telno;
	double acctbalance, deposit;
	char choices;
	acctbalance = 0;

//inputs, get the name and initial balance

          
	do
	{
cout<<"\n\n-------------------------------------------------\n";  
cout << "What do you wish to do?" << endl;  
cout << "O open new account" << endl;  
cout << "C open new Business account" << endl;  
cout << "D Deposit Money" << endl;  
cout << "W Withdraw Money" << endl;  
cout << "B Display Balance" << endl;  
cout << "P Bank Account Details" << endl;  
cout << "Q Quit" << endl<<endl;cout<<"Enter selection: ";  
cin >> choices;  
switch (choices)  
{  
//add account type options here under letter O  
case 'O':  
case 'o':  
{  
    {  
        cout<<"\n\n-------------------------------------------------\n";  
        cout << "What Account Do You Wish To Open?" << endl;  
        cout << "S Student Account" << endl;  
        cout << "C Current Account" << endl;  
        cout << "B Business Account" << endl;  
        cout<<"Enter selection: ";  
        cin >> choices;  
        break;// Need a break statement here  
    }  
}  
				 
				 
				 
	//STUDENT ACCOUNT			 
				 case 'S':
                 case 's':
                          {
                          cout<<"Please Answer The Following Questions / 1 = Yes, 2 = No "<<endl;
                          cout<<"---------------------------------------------------------"<<endl;
						  cout<<"Are You A Full Time Student?"<<endl;
                          cin>>sqone;
                          if (sqone == 0)
                          {
                                       cout << "sorry that is not a valid option"<< endl;
                                       break;
                          }
						  if (sqone == 2)
                          {
                                       cout<<"Sorry You Are Not Valid For This Account"<<endl;
                                       break;
                          }
                          else if (sqone == 1)
                          {
                                       cout<<"Thankyou"<<endl;
									   cout<<"Have You Got Documentation To Support Your Course And Accomadation?"<<endl;
										cin>>sqtwo;
						  if (sqtwo == 0)
							  {
                                       cout << "sorry that is not a valid option"<< endl;
                                       break;
                          }
						  if (sqtwo == 1)
                          {
										cout<<"Thankyou you are elegible for this account"<<endl;
										cout<<"------------------------------------------"<<endl;
									   	cout << "Colleague Name: ";
										cin >> colname;
										cout << "Enter First Name: ";
										cin >> name;
										cout << "Enter Surname: ";
										cin >> sname;
										cout << "House Number: ";
										cin >> housenumber;
										cout << "Post Code: ";
										cin >> postcode;
										cout << "Date Of Birth: ";
										cin >> dob;
										cout << "Telephone Number: ";
										cin >> telno;
										cout << "Please enter initial balance: ";
										cin >> acctbalance;
										break;

                       
                          }
							else if (sqtwo == 2)
                          {
                                       cout<<"Sorry You Are Not Valid For This Account"<<endl;
                                       break;
						  }
						
                 

	  //BUSINESS ACCOUNT
						case 'c':
                 case 'C':
                          {
                          cout<<"Please Answer The Following Questions / 1 = Yes, 2 = No "<<endl;
                          cout<<"---------------------------------------------------------"<<endl;
						  cout<<"Have you got documentation showing your business details?"<<endl;
                          cin>>bone;
                          if (bone == 0)
                          {
                                       cout << "sorry that is not a valid option"<< endl;
                                       break;
                          }
						  if (bone == 2)
                          {
                                       cout<<"Sorry You Are Not Valid For This Account"<<endl;
                                       break;
                          }
                          if (bone == 1)
                          {
                                       
									   cout<<"We Will Now Proceed To Open This Account For You?"<<endl;
										cout<<"------------------------------------------"<<endl;
									   	cout << "Colleague Name: ";
										cin >> colname;
										cout << "Enter First Name: ";
										cin >> name;
										cout << "Enter Surname: ";
										cin >> sname;
										cout << "House Number: ";
										cin >> housenumber;
										cout << "Post Code: ";
										cin >> postcode;
										cout << "Date Of Birth: ";
										cin >> dob;
										cout << "Telephone Number: ";
										cin >> telno;
										cout << "Please enter initial balance: ";
										cin >> acctbalance;
										break;

                       
                          }
							else if (sqtwo == 2)
                          {
                                       cout<<"Sorry You Are Not Valid For This Account"<<endl;
                                       break;
						  }
						  }
						  }
						  }
		 
				 
				 
				 case 'D':
                 case 'd':
                          {
                          cout << "Enter amount to be deposited: "<< endl;
                          cin >>deposit;
                          if (deposit > 0)
                                      {
                                      acctbalance = acctbalance + deposit;
                                      if (deposit >= 10000)
                                      cout <<"Big Money!"<<endl;
                                      break;
                                      }
                          if (deposit == 0 || deposit < 0)
                                      {
                                      cout <<"Amount must be greater than 0";
                                      break ;
                                      }
                          }
                 case 'B':
                 case 'b':
                          {
                          cout <<"Your current balance is $ "<<acctbalance<<'.'<<endl;
                          if (acctbalance ==0)
                                          cout <<"Big Money!"<<endl;
                          break;
                          }
                 case 'W':
                 case 'w':
                          {
                          cout<<"Please enter amount to withdraw "<<endl;
                          cout<<"Withdrawal must be multiple of 10"<<endl;
                          cin>>withdraw;
                          if (withdraw == 00)
                          {
                                       cout << "Can't withdraw 0 dollars"<< endl;
                                       break;
                          }
                          if (withdraw < 0)
                          {
                                       cout<<"Must be a positive number"<<endl;
                                       break;
                          }
                          if (withdraw > acctbalance)
                          {
                                       cout<<"Can't withdraw more than balance"<<endl;
                                       break;
                          }
                          if ((withdraw % 10) !=0)
                          {
                                       cout<<"amount must be a multiple of 20"<<endl;
                                       break;
                          }
                          else
                          {
                                       acctbalance=acctbalance - withdraw;
                                       cout<<"Here is your money. Enjoy!"<< endl;
                                       break;
                          }
						  case 'P':
                 case 'p':
                          {
							  cout << "\n\n"<<endl;
							  cout <<"Personal Details"<<endl;
							  cout <<"First Name : "<<name<<endl;
							  cout <<"Surname : " <<sname<<endl;
							  cout <<"house number : " <<housenumber<<endl;
							  cout <<"post code : " <<postcode<<endl;
							  cout <<"telephone number : " <<telno<<endl;

                          cout <<"Your current balance is £ "<<acctbalance<<'.'<<endl;
                          if (acctbalance ==0)
                                          cout <<"Big Money!"<<endl;
                          break;
                          }
                          }

                 case 'Q':
                 case 'q':
                          {
                                       cout<<"Thank for using our services have a nice day!"<<endl;
                                       break;
                          }
        }
        }while (choices != 'q' && choices != 'Q');
        return 0;
}

and the 2nd class - account.h
#include <iostream>
#include <string>

using namespace std;

class Account
{
  double money;         // amount of money held by this account
  double interestRate;  // a monthly or yearly interestrate, depending on how the account is to be used.


public:

     // create an account with an initial amountand a specified interest rate
  Account(double amount, double percent)
  {
    money = amount;
    interestRate = percent;
  }

     // return the account's balance
  double balance()
  {
    return money;
  }

     // add money to the account
  void deposit(double amount)
  {
    money += amount;
  }

     // substract money from the account
  void withdraw(double amount)
  {
    
  }

     // add money according to the interest rate.
  void addInterest()
  {
    money *= (1 + interestRate/100.0);
         
  }

};

thankyou again
AddThis Social Bookmark Button
Reply With Quote  
Posts: 7,460
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 676
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: help with c++ coursework - code so far included.

  #2  
Nov 21st, 2008
>allow 10 accounts to be opened and then allow the user to search
>for each one and deposit and withdraw from each one
Well, start by allowing one account and work up from there. As a first attempt, I would suggest an array of account class objects. You need to write the account class, of course. Overall the problem is more tedious than difficult. Here's a basic example:
  1. #include <ios>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <limits>
  5. #include <stdexcept>
  6. #include <string>
  7.  
  8. class Account {
  9. std::string _name;
  10.  
  11. // Represented by the number of pennies, so on a
  12. // 32-bit system these accounts are limited to
  13. // $42,949,672.96. Beware arithmetic overflow
  14. unsigned long _balance;
  15. public:
  16. Account(): _balance ( 0 ) {}
  17.  
  18. Account ( std::string name, unsigned long initial_balance )
  19. : _name ( name ), _balance ( initial_balance )
  20. {}
  21.  
  22. std::string Name() const
  23. {
  24. return _name;
  25. }
  26.  
  27. unsigned long Balance() const
  28. {
  29. return _balance;
  30. }
  31.  
  32. unsigned long Credit ( unsigned long amount )
  33. {
  34. return _balance += amount;
  35. }
  36.  
  37. unsigned long Debit ( unsigned long amount )
  38. {
  39. if ( amount > _balance ) {
  40. throw std::runtime_error (
  41. "Cannot debit more than the account contains" );
  42. }
  43.  
  44. return _balance -= amount;
  45. }
  46.  
  47. friend std::ostream& operator<< ( std::ostream& out, const Account& acct )
  48. {
  49. return out<< acct._name <<": "<< acct._balance / 100 <<"."
  50. << std::setfill ( '0' ) << std::setw ( 2 ) << acct._balance % 100;
  51. }
  52. };
  53.  
  54. int main()
  55. {
  56. Account accts[10];
  57. int n = 0;
  58.  
  59. while ( true ) {
  60. std::cout<<"> ";
  61.  
  62. std::string command;
  63.  
  64. if ( !getline ( std::cin, command ) || command == "q" )
  65. break;
  66.  
  67. if ( command == "new" ) {
  68. std::string name;
  69. unsigned long balance;
  70.  
  71. std::cout<<"Name: ";
  72. getline ( std::cin, name );
  73.  
  74. std::cout<<"Balance: ";
  75. std::cin>> balance;
  76. std::cin.ignore();
  77.  
  78. accts[n++] = Account ( name, balance );
  79. }
  80. else if ( command == "credit" ) {
  81. std::string name;
  82. unsigned long amount;
  83.  
  84. std::cout<<"Account name: ";
  85. getline ( std::cin, name );
  86.  
  87. std::cout<<"Amount to debit: ";
  88. std::cin>> amount;
  89. std::cin.ignore();
  90.  
  91. for ( int i = 0; i < n; i++ ) {
  92. if ( accts[i].Name() == name ) {
  93. try {
  94. accts[i].Debit ( amount );
  95. } catch ( std::exception& ex ) {
  96. std::cout<< ex.what() <<'\n';
  97. }
  98. }
  99. }
  100. }
  101. else if ( command == "debit" ) {
  102. std::string name;
  103. unsigned long amount;
  104.  
  105. std::cout<<"Account name: ";
  106. getline ( std::cin, name );
  107.  
  108. std::cout<<"Amount to credit: ";
  109. std::cin>> amount;
  110. std::cin.ignore();
  111.  
  112. for ( int i = 0; i < n; i++ ) {
  113. if ( accts[i].Name() == name )
  114. accts[i].Credit ( amount );
  115. }
  116. }
  117. else if ( command == "report" ) {
  118. for ( int i = 0; i < n; i++ )
  119. std::cout<< accts[i] <<'\n';
  120. }
  121. else
  122. std::cout<<"Unrecognized command\n";
  123. }
  124. }
I'm here to prove you wrong.
Reply With Quote  
Posts: 1,572
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 239
Lerner Lerner is offline Offline
Posting Virtuoso

Re: help with c++ coursework - code so far included.

  #3  
Nov 21st, 2008
Just as an aside, in this economy a short might be a more accurate variable type than an unsigned long for the balance. Hopefully we don't see the day where char types have enough memory to successfully handle the balances!
Klatu Barada Nikto
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C++ Forum
Views: 289 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:56 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC