View Single Post
Join Date: Sep 2004
Posts: 7,652
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: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #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