943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 504
  • C++ RSS
Nov 21st, 2008
0

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

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. // Declare Variables
  9. int withdraw;
  10. int sqone;
  11. int sqtwo;
  12. int bone;
  13. string colname;
  14. string name;
  15. string sname;
  16. int housenumber;
  17. string postcode;
  18. string dob;
  19. string telno;
  20. double acctbalance, deposit;
  21. char choices;
  22. acctbalance = 0;
  23.  
  24. //inputs, get the name and initial balance
  25.  
  26.  
  27. do
  28. {
  29. cout<<"\n\n-------------------------------------------------\n";
  30. cout << "What do you wish to do?" << endl;
  31. cout << "O open new account" << endl;
  32. cout << "C open new Business account" << endl;
  33. cout << "D Deposit Money" << endl;
  34. cout << "W Withdraw Money" << endl;
  35. cout << "B Display Balance" << endl;
  36. cout << "P Bank Account Details" << endl;
  37. cout << "Q Quit" << endl<<endl;cout<<"Enter selection: ";
  38. cin >> choices;
  39. switch (choices)
  40. {
  41. //add account type options here under letter O
  42. case 'O':
  43. case 'o':
  44. {
  45. {
  46. cout<<"\n\n-------------------------------------------------\n";
  47. cout << "What Account Do You Wish To Open?" << endl;
  48. cout << "S Student Account" << endl;
  49. cout << "C Current Account" << endl;
  50. cout << "B Business Account" << endl;
  51. cout<<"Enter selection: ";
  52. cin >> choices;
  53. break;// Need a break statement here
  54. }
  55. }
  56.  
  57.  
  58.  
  59. //STUDENT ACCOUNT
  60. case 'S':
  61. case 's':
  62. {
  63. cout<<"Please Answer The Following Questions / 1 = Yes, 2 = No "<<endl;
  64. cout<<"---------------------------------------------------------"<<endl;
  65. cout<<"Are You A Full Time Student?"<<endl;
  66. cin>>sqone;
  67. if (sqone == 0)
  68. {
  69. cout << "sorry that is not a valid option"<< endl;
  70. break;
  71. }
  72. if (sqone == 2)
  73. {
  74. cout<<"Sorry You Are Not Valid For This Account"<<endl;
  75. break;
  76. }
  77. else if (sqone == 1)
  78. {
  79. cout<<"Thankyou"<<endl;
  80. cout<<"Have You Got Documentation To Support Your Course And Accomadation?"<<endl;
  81. cin>>sqtwo;
  82. if (sqtwo == 0)
  83. {
  84. cout << "sorry that is not a valid option"<< endl;
  85. break;
  86. }
  87. if (sqtwo == 1)
  88. {
  89. cout<<"Thankyou you are elegible for this account"<<endl;
  90. cout<<"------------------------------------------"<<endl;
  91. cout << "Colleague Name: ";
  92. cin >> colname;
  93. cout << "Enter First Name: ";
  94. cin >> name;
  95. cout << "Enter Surname: ";
  96. cin >> sname;
  97. cout << "House Number: ";
  98. cin >> housenumber;
  99. cout << "Post Code: ";
  100. cin >> postcode;
  101. cout << "Date Of Birth: ";
  102. cin >> dob;
  103. cout << "Telephone Number: ";
  104. cin >> telno;
  105. cout << "Please enter initial balance: ";
  106. cin >> acctbalance;
  107. break;
  108.  
  109.  
  110. }
  111. else if (sqtwo == 2)
  112. {
  113. cout<<"Sorry You Are Not Valid For This Account"<<endl;
  114. break;
  115. }
  116.  
  117.  
  118.  
  119. //BUSINESS ACCOUNT
  120. case 'c':
  121. case 'C':
  122. {
  123. cout<<"Please Answer The Following Questions / 1 = Yes, 2 = No "<<endl;
  124. cout<<"---------------------------------------------------------"<<endl;
  125. cout<<"Have you got documentation showing your business details?"<<endl;
  126. cin>>bone;
  127. if (bone == 0)
  128. {
  129. cout << "sorry that is not a valid option"<< endl;
  130. break;
  131. }
  132. if (bone == 2)
  133. {
  134. cout<<"Sorry You Are Not Valid For This Account"<<endl;
  135. break;
  136. }
  137. if (bone == 1)
  138. {
  139.  
  140. cout<<"We Will Now Proceed To Open This Account For You?"<<endl;
  141. cout<<"------------------------------------------"<<endl;
  142. cout << "Colleague Name: ";
  143. cin >> colname;
  144. cout << "Enter First Name: ";
  145. cin >> name;
  146. cout << "Enter Surname: ";
  147. cin >> sname;
  148. cout << "House Number: ";
  149. cin >> housenumber;
  150. cout << "Post Code: ";
  151. cin >> postcode;
  152. cout << "Date Of Birth: ";
  153. cin >> dob;
  154. cout << "Telephone Number: ";
  155. cin >> telno;
  156. cout << "Please enter initial balance: ";
  157. cin >> acctbalance;
  158. break;
  159.  
  160.  
  161. }
  162. else if (sqtwo == 2)
  163. {
  164. cout<<"Sorry You Are Not Valid For This Account"<<endl;
  165. break;
  166. }
  167. }
  168. }
  169. }
  170.  
  171.  
  172.  
  173. case 'D':
  174. case 'd':
  175. {
  176. cout << "Enter amount to be deposited: "<< endl;
  177. cin >>deposit;
  178. if (deposit > 0)
  179. {
  180. acctbalance = acctbalance + deposit;
  181. if (deposit >= 10000)
  182. cout <<"Big Money!"<<endl;
  183. break;
  184. }
  185. if (deposit == 0 || deposit < 0)
  186. {
  187. cout <<"Amount must be greater than 0";
  188. break ;
  189. }
  190. }
  191. case 'B':
  192. case 'b':
  193. {
  194. cout <<"Your current balance is $ "<<acctbalance<<'.'<<endl;
  195. if (acctbalance ==0)
  196. cout <<"Big Money!"<<endl;
  197. break;
  198. }
  199. case 'W':
  200. case 'w':
  201. {
  202. cout<<"Please enter amount to withdraw "<<endl;
  203. cout<<"Withdrawal must be multiple of 10"<<endl;
  204. cin>>withdraw;
  205. if (withdraw == 00)
  206. {
  207. cout << "Can't withdraw 0 dollars"<< endl;
  208. break;
  209. }
  210. if (withdraw < 0)
  211. {
  212. cout<<"Must be a positive number"<<endl;
  213. break;
  214. }
  215. if (withdraw > acctbalance)
  216. {
  217. cout<<"Can't withdraw more than balance"<<endl;
  218. break;
  219. }
  220. if ((withdraw % 10) !=0)
  221. {
  222. cout<<"amount must be a multiple of 20"<<endl;
  223. break;
  224. }
  225. else
  226. {
  227. acctbalance=acctbalance - withdraw;
  228. cout<<"Here is your money. Enjoy!"<< endl;
  229. break;
  230. }
  231. case 'P':
  232. case 'p':
  233. {
  234. cout << "\n\n"<<endl;
  235. cout <<"Personal Details"<<endl;
  236. cout <<"First Name : "<<name<<endl;
  237. cout <<"Surname : " <<sname<<endl;
  238. cout <<"house number : " <<housenumber<<endl;
  239. cout <<"post code : " <<postcode<<endl;
  240. cout <<"telephone number : " <<telno<<endl;
  241.  
  242. cout <<"Your current balance is £ "<<acctbalance<<'.'<<endl;
  243. if (acctbalance ==0)
  244. cout <<"Big Money!"<<endl;
  245. break;
  246. }
  247. }
  248.  
  249. case 'Q':
  250. case 'q':
  251. {
  252. cout<<"Thank for using our services have a nice day!"<<endl;
  253. break;
  254. }
  255. }
  256. }while (choices != 'q' && choices != 'Q');
  257. return 0;
  258. }

and the 2nd class - account.h
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Account
  7. {
  8. double money; // amount of money held by this account
  9. double interestRate; // a monthly or yearly interestrate, depending on how the account is to be used.
  10.  
  11.  
  12. public:
  13.  
  14. // create an account with an initial amountand a specified interest rate
  15. Account(double amount, double percent)
  16. {
  17. money = amount;
  18. interestRate = percent;
  19. }
  20.  
  21. // return the account's balance
  22. double balance()
  23. {
  24. return money;
  25. }
  26.  
  27. // add money to the account
  28. void deposit(double amount)
  29. {
  30. money += amount;
  31. }
  32.  
  33. // substract money from the account
  34. void withdraw(double amount)
  35. {
  36.  
  37. }
  38.  
  39. // add money according to the interest rate.
  40. void addInterest()
  41. {
  42. money *= (1 + interestRate/100.0);
  43.  
  44. }
  45.  
  46. };

thankyou again
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexdm50 is offline Offline
1 posts
since Apr 2008
Nov 21st, 2008
1

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

>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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 21st, 2008
0

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

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!
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Crash Windows
Next Thread in C++ Forum Timeline: linked list deleteing problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC