Classes and Drivers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Classes and Drivers

 
0
  #1
Apr 13th, 2007
Hello! I have written written my own class, which includes a header file and a .cpp file. I have also written the driver for this program. Everything looks fine to me, but I am receiving about 20 random errors. I will post my code for all 3 files but I believe it is a problem that can be diagnosed using just the driver.

The program is a bank account program that adds new accounts, withdraws, deposits, updates, displays, etc. Any help would be greatly appreciated! Thanks.


HEADER


  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. class BankAccount
  6. {
  7. public:
  8. BankAccount();
  9. void deposit( double amt );
  10. void withdraw( double amt );
  11. void update();
  12. int getID();
  13. void setID( int anID );
  14. double getBalance();
  15. void setBalance( double amt );
  16. double getInterestRate();
  17. void setInterestRate( double amt );
  18. string getFirstName();
  19. void setFirstName( string theName );
  20. string getLastName();
  21. void setLastName( string theName );
  22. string getFullName();
  23. void display();
  24. private:
  25. string firstName, lastName;
  26. double interestRate;
  27. int ID;
  28. double balance;
  29. };


CLASS DEFINITION


  1.  
  2. #include "BankAccount.h"
  3. /*
  4.  Default Constructor
  5. */
  6. BankAccount::BankAccount()
  7. {
  8. ID = 0;
  9. balance = 0.0;
  10. interestRate = 0.0;
  11. }
  12. /*
  13.  Adds deposit amount to current balance
  14. */
  15. void BankAccount::deposit( double amt )
  16. {
  17. balance = balance + amt;
  18. }
  19. /*
  20.  Withdraws amount from current balance
  21.  if there are sufficient funds.
  22.  Fails quietly for insufficient funds.
  23. */
  24. void BankAccount::withdraw( double amt )
  25. {
  26. if( balance < amt ) return;
  27. balance = balance - amt;
  28. }
  29. /*
  30.  Getter (accessor) for balance
  31. */
  32. double BankAccount::getBalance()
  33. {
  34. return balance;
  35. }
  36. /*
  37.  Setter (mutator) for balance
  38. */
  39. void BankAccount::setBalance( double amt )
  40. {
  41. balance = amt;
  42. }
  43. /*
  44.  Adds interest to account using current interest rate
  45. */
  46. void BankAccount::update()
  47. {
  48. balance = balance + balance * interestRate;
  49. }
  50. /*
  51.  Getter for ID
  52. */
  53. int BankAccount::getID()
  54. {
  55. return ID;
  56. }
  57. /*
  58.  Setter for ID
  59. */
  60. void BankAccount::setID( int anID )
  61. {
  62. ID = anID;
  63. }
  64. /*
  65.  Getter for balance
  66. */
  67. double BankAccount::getInterestRate()
  68. {
  69. return interestRate;
  70. }
  71. /*
  72.  Setter for interest rate
  73. */
  74. void BankAccount::setInterestRate( double amt )
  75. {
  76. interestRate = amt;
  77. }
  78. /*
  79.  Getter for first name
  80. */
  81. string BankAccount::getFirstName()
  82. {
  83. return firstName;
  84. }
  85. /*
  86.  Setter for first name
  87. */
  88. void BankAccount::setFirstName( string theName )
  89. {
  90. firstName = theName;
  91. }
  92. /*
  93.  Getter for last name
  94. */
  95. string BankAccount::getLastName()
  96. {
  97. return lastName;
  98. }
  99. /*
  100.  Setter for last name
  101. */
  102. void BankAccount::setLastName( string theName )
  103. {
  104. lastName = theName;
  105. }
  106. /*
  107.  Getter for full name
  108. */
  109. string BankAccount::getFullName()
  110. {
  111. return lastName + ", " + firstName;
  112. }
  113. /*
  114.  Displays info about about to console.
  115. */
  116. void BankAccount::display()
  117. {
  118. cout << "ID : " << ID << endl;
  119. cout << "Name : " << getFullName() << endl;
  120. cout << "Balance : $" << balance << endl;
  121. cout << "Interest Rate : " << interestRate << " %" << endl;
  122. }


DRIVER


  1.  
  2.  
  3. #include "BankAccount.h"
  4. int getChoice();
  5. void addBankAccount();
  6. void withdrawAccount();
  7. void depositAccount();
  8. void updateAccounts();
  9. void displayAccount();
  10. void displayBalance();
  11. void displayInfo();
  12. int find(count, accts, id);
  13.  
  14. double tempRate;
  15. double balance;
  16. int count[10];
  17. BankAccount accts[10];
  18. int nextID = 100;
  19.  
  20. void main()
  21. {
  22. int choice;
  23. do{
  24. choice = getChoice();
  25. if( choice == 1 ) addBankAccount();
  26. else if( choice == 2 ) withdrawAccount();
  27. else if( choice == 3 ) depositAccount();
  28. else if( choice == 4 ) updateAccounts();
  29. else if( choice == 5 ) displayAccount();
  30. else if( choice == 6 ) displayBalance();
  31. else if( choice == 7 ) displayInfo();
  32. if( choice > 8 || choice < 1 ) cout << "ERROR: Please enter valid choice (1-8)." << endl;
  33. } while( choice != 8 );
  34.  
  35.  
  36. }
  37.  
  38. //This function displays the calculation menu.
  39. int getChoice()
  40. {
  41. int c;
  42. cout << "----------------------\n";
  43. cout << "1 - Add a new bank account.\n";
  44. cout << "2 - Withdraw from a specific bank account.\n";
  45. cout << "3 - Deposit to a specific account.\n";
  46. cout << "4 - Update all accounts which will add a fixed interest rate to each account.\n";
  47. cout << "5 - Display one specific bank accout.\n";
  48. cout << "6 - Display the average balance for all of the accounts.\n";
  49. cout << "7 - Display the current information for all accounts.\n";
  50. cout << "8 - Terminate the program.\n";
  51. cout << "----------------------\n";
  52. cout << "Enter number of choice --> ";
  53. cin >> c;
  54. return c;
  55. }
  56. //This function adds a bank account for a new member.
  57. void addBankAccount()
  58. {
  59. int count = 0;
  60. string temp;
  61.  
  62. cout << "Please enter first name.";
  63. cin >> temp;
  64. accts[count].setFirstName(temp);
  65. cout << "Please enter last name.";
  66. cin >> temp;
  67. accts[count].setLastName(temp);
  68. cout << "Please enter balance.";
  69. cin >> balance;
  70. accts[count].setBalance(balance);
  71. cout << "Please enter interest rate (decimal form).";
  72. cin >> tempRate;
  73. accts[count].setInterestRate(tempRate);
  74. accts[count].setID(nextID);
  75. nextID = nextID + 1;
  76. count++;
  77.  
  78.  
  79. }
  80.  
  81. // This function withdraws an amount from a specific account.
  82. void withdrawAccount()
  83. {
  84. int slot;
  85. double amt;
  86. int id;
  87. cout << "Enter ID number.";
  88. cin >> id;
  89. int slot = find(count, accts, id)
  90. cout << "Enter amount to withdraw.";
  91. cin >> amt;
  92. accts[slot].withdraw(amt);
  93.  
  94.  
  95. }
  96.  
  97. //This function deposits an amount into a specific account.
  98. void depositAccount()
  99. {
  100. int slot;
  101. double amt;
  102. int id;
  103. cout << "Enter ID number.";
  104. cin >> id;
  105. int slot = find(count, accts, id)
  106. cout << "Enter amount to deposit.";
  107. cin >> amt;
  108. accts[slot].deposit(amt);
  109. }
  110.  
  111. //This function updates all accounts using a fixed interest rate.
  112. void updateAccounts()
  113. {
  114. for (int i=0; i<count; i++)
  115. accts[i].update();
  116. }
  117.  
  118. //This function displays one bank account.
  119. void displayAccount()
  120. {
  121. int target;
  122. cout << "Enter account number.";
  123. cin >> target;
  124. accts[target].display();
  125. }
  126.  
  127. //This function displays the average balance of all accounts.
  128. void displayBalance()
  129. {
  130. }
  131. //This function displays the current information for all accounts.
  132. void displayInfo()
  133. {
  134. for (int i=0; i<count; i++)
  135. accts[i].display();
  136. }
  137.  
  138. int find(count, accts, id)
  139. {
  140. int index = 0;
  141. bool found = false;
  142. while ((!found) && (index < count))
  143. if (id == accts[index])
  144. found = true;
  145. else
  146. index++;
  147. if (found)
  148. return index;
  149. else
  150. return -1;
  151. }
Last edited by RaCheer; Apr 13th, 2007 at 12:49 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Classes and Drivers

 
0
  #2
Apr 13th, 2007
Originally Posted by RaCheer View Post
Hello! I have written written my own class, which includes a header file and a .cpp file. I have also written the driver for this program. Everything looks fine to me, but I am receiving about 20 random errors. I will post my code for all 3 files but I believe it is a problem that can be diagnosed using just the driver.

The program is a bank account program that adds new accounts, withdraws, deposits, updates, displays, etc. Any help would be greatly appreciated! Thanks.
Well, if it's OK with you, we'll just pick 20 random lines and randomly decide they must be wrong. :rolleyes:

Don't you think knowing what the errors are and where the errors are might be helpful in fixing the errors?

And please format your code. It's barely readable without proper indentation.
[edit]I see this has been mentioned at least 4 times. Are you bucking for an infraction? I really hate being ignored, and trying to read more unreadable code. Are you having a problem understanding good formatting guidelines?[/edit]
Last edited by WaltP; Apr 13th, 2007 at 1:36 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Re: Classes and Drivers

 
0
  #3
Apr 13th, 2007
Sorry...I just thought it might be more obvious to someone else. These are errors that don't make sense to me because they worked before. I have declared what the error says is not and etc. So here are the errors I receive:

  1. error C2065: 'count' : undeclared identifier
  2. error C2065: 'accts' : undeclared identifier
  3. error C2065: 'id' : undeclared identifier
  4. error C2078: too many initializers
  5. error C2040: 'count' : 'int [10]' differs in levels of indirection from ''unknown-type''
  6. error C2040: 'accts' : 'BankAccount [10]' differs in levels of indirection from ''unknown-type''
  7. error C2228: left of '.setFirstName' must have class/struct/union
  8. error C2228: left of '.setLastName' must have class/struct/union
  9. error C2228: left of '.setBalance' must have class/struct/union
  10. error C2228: left of '.setInterestRate' must have class/struct/union
  11. error C2228: left of '.setID' must have class/struct/union
  12. error C2086: 'int slot' : redefinition
  13. see declaration of 'slot'
  14. error C2146: syntax error : missing ';' before identifier 'cout'
  15. error C2228: left of '.withdraw' must have class/struct/union
  16. error C2374: 'slot' : redefinition; multiple initialization
  17. see declaration of 'slot'
  18. error C2228: left of '.deposit' must have class/struct/union
  19. error C2228: left of '.update' must have class/struct/union
  20. error C2228: left of '.display' must have class/struct/union
  21. error C2228: left of '.display' must have class/struct/union
  22. error C2448: 'find' : function-style initializer appears to be a function ==========

Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Re: Classes and Drivers

 
0
  #4
Apr 13th, 2007
HEADER


  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class BankAccount
  5. {
  6. public:
  7. BankAccount();
  8. void deposit( double amt );
  9. void withdraw( double amt );
  10. void update();
  11. int getID();
  12. void setID( int anID );
  13. double getBalance();
  14. void setBalance( double amt );
  15. double getInterestRate();
  16. void setInterestRate( double amt );
  17. string getFirstName();
  18. void setFirstName( string theName );
  19. string getLastName();
  20. void setLastName( string theName );
  21. string getFullName();
  22. void display();
  23. private:
  24. string firstName, lastName;
  25. double interestRate;
  26. int ID;
  27. double balance;
  28. };


CLASS DEFINITION


  1.  
  2. #include "BankAccount.h"
  3. /*
  4.   Default Constructor
  5. */
  6. BankAccount::BankAccount()
  7. {
  8. ID = 0;
  9. balance = 0.0;
  10. interestRate = 0.0;
  11. }
  12. /*
  13.   Adds deposit amount to current balance
  14. */
  15. void BankAccount::deposit( double amt )
  16. {
  17. balance = balance + amt;
  18. }
  19. /*
  20.   Withdraws amount from current balance
  21.   if there are sufficient funds.
  22.   Fails quietly for insufficient funds.
  23. */
  24. void BankAccount::withdraw( double amt )
  25. {
  26. if( balance < amt ) return;
  27. balance = balance - amt;
  28. }
  29. /*
  30.   Getter (accessor) for balance
  31. */
  32. double BankAccount::getBalance()
  33. {
  34. return balance;
  35. }
  36. /*
  37.   Setter (mutator) for balance
  38. */
  39. void BankAccount::setBalance( double amt )
  40. {
  41. balance = amt;
  42. }
  43. /*
  44.   Adds interest to account using current interest rate
  45. */
  46. void BankAccount::update()
  47. {
  48. balance = balance + balance * interestRate;
  49. }
  50. /*
  51.   Getter for ID
  52. */
  53. int BankAccount::getID()
  54. {
  55. return ID;
  56. }
  57. /*
  58.   Setter for ID
  59. */
  60. void BankAccount::setID( int anID )
  61. {
  62. ID = anID;
  63. }
  64. /*
  65.   Getter for balance
  66. */
  67. double BankAccount::getInterestRate()
  68. {
  69. return interestRate;
  70. }
  71. /*
  72.   Setter for interest rate
  73. */
  74. void BankAccount::setInterestRate( double amt )
  75. {
  76. interestRate = amt;
  77. }
  78. /*
  79.   Getter for first name
  80. */
  81. string BankAccount::getFirstName()
  82. {
  83. return firstName;
  84. }
  85. /*
  86.   Setter for first name
  87. */
  88. void BankAccount::setFirstName( string theName )
  89. {
  90. firstName = theName;
  91. }
  92. /*
  93.   Getter for last name
  94. */
  95. string BankAccount::getLastName()
  96. {
  97. return firstName;
  98. }
  99. /*
  100.   Setter for last name
  101. */
  102. void BankAccount::setLastName( string theName )
  103. {
  104. lastName = theName;
  105. }
  106. /*
  107.   Getter for full name
  108. */
  109. string BankAccount::getFullName()
  110. {
  111. return lastName + ", " + firstName;
  112. }
  113. /*
  114.   Displays info about about to console.
  115. */
  116. void BankAccount::display()
  117. {
  118. cout << "ID : " << ID << endl;
  119. cout << "Name : " << getFullName() << endl;
  120. cout << "Balance : $" << balance << endl;
  121. cout << "Interest Rate : " << interestRate << " %" << endl;
  122. }


DRIVER


  1.  
  2. #include "BankAccount.h"
  3. int getChoice();
  4. void addBankAccount();
  5. void withdrawAccount();
  6. void depositAccount();
  7. void updateAccounts();
  8. void displayAccount();
  9. void displayBalance();
  10. void displayInfo();
  11. int find(count, accts, id);
  12.  
  13. double tempRate;
  14. double balance;
  15. int count[10];
  16. BankAccount accts[10];
  17. int nextID = 100;
  18.  
  19. void main()
  20. {
  21. int choice;
  22. do{
  23. choice = getChoice();
  24. if( choice == 1 ) addBankAccount();
  25. else if( choice == 2 ) withdrawAccount();
  26. else if( choice == 3 ) depositAccount();
  27. else if( choice == 4 ) updateAccounts();
  28. else if( choice == 5 ) displayAccount();
  29. else if( choice == 6 ) displayBalance();
  30. else if( choice == 7 ) displayInfo();
  31. if( choice > 8 || choice < 1 ) cout << "ERROR: Please enter valid choice (1-8)." << endl;
  32. } while( choice != 8 );
  33.  
  34.  
  35. }
  36.  
  37. //This function displays the calculation menu.
  38. int getChoice()
  39. {
  40. int c;
  41. cout << "----------------------\n";
  42. cout << "1 - Add a new bank account.\n";
  43. cout << "2 - Withdraw from a specific bank account.\n";
  44. cout << "3 - Deposit to a specific account.\n";
  45. cout << "4 - Update all accounts which will add a fixed interest rate to each account.\n";
  46. cout << "5 - Display one specific bank accout.\n";
  47. cout << "6 - Display the average balance for all of the accounts.\n";
  48. cout << "7 - Display the current information for all accounts.\n";
  49. cout << "8 - Terminate the program.\n";
  50. cout << "----------------------\n";
  51. cout << "Enter number of choice --> ";
  52. cin >> c;
  53. return c;
  54. }
  55. //This function adds a bank account for a new member.
  56. void addBankAccount()
  57. {
  58. int count = 0;
  59. string temp;
  60.  
  61. cout << "Please enter first name.";
  62. cin >> temp;
  63. accts[count].setFirstName(temp);
  64. cout << "Please enter last name.";
  65. cin >> temp;
  66. accts[count].setLastName(temp);
  67. cout << "Please enter balance.";
  68. cin >> balance;
  69. accts[count].setBalance(balance);
  70. cout << "Please enter interest rate (decimal form).";
  71. cin >> tempRate;
  72. accts[count].setInterestRate(tempRate);
  73. accts[count].setID(nextID);
  74. nextID = nextID + 1;
  75. count++;
  76.  
  77.  
  78. }
  79.  
  80. // This function withdraws an amount from a specific account.
  81. void withdrawAccount()
  82. {
  83. int slot;
  84. double amt;
  85. int id;
  86. cout << "Enter ID number.";
  87. cin >> id;
  88. int slot = find(count, accts, id)
  89. cout << "Enter amount to withdraw.";
  90. cin >> amt;
  91. accts[slot].withdraw(amt);
  92.  
  93.  
  94. }
  95.  
  96. //This function deposits an amount into a specific account.
  97. void depositAccount()
  98. {
  99. int slot;
  100. double amt;
  101. int id;
  102. cout << "Enter ID number.";
  103. cin >> id;
  104. int slot = find(count, accts, id)
  105. cout << "Enter amount to deposit.";
  106. cin >> amt;
  107. accts[slot].deposit(amt);
  108. }
  109.  
  110. //This function updates all accounts using a fixed interest rate.
  111. void updateAccounts()
  112. {
  113. for (int i=0; i<count; i++)
  114. accts[i].update();
  115. }
  116.  
  117. //This function displays one bank account.
  118. void displayAccount()
  119. {
  120. int target;
  121. cout << "Enter account number.";
  122. cin >> target;
  123. accts[target].display();
  124. }
  125.  
  126. //This function displays the average balance of all accounts.
  127. void displayBalance()
  128. {
  129.  
  130. }
  131.  
  132. //This function displays the current information for all accounts.
  133. void displayInfo()
  134. {
  135. for (int i=0; i<count; i++)
  136. accts[i].display();
  137. }
  138.  
  139. int find(count, accts, id)
  140. {
  141. int index = 0;
  142. bool found = false;
  143. while ((!found) && (index < count))
  144. if (id == accts[index])
  145. found = true;
  146. else
  147. index++;
  148. if (found)
  149. return index;
  150. else
  151. return -1;
  152. }
Last edited by RaCheer; Apr 13th, 2007 at 2:06 am. Reason: Indentation
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: Classes and Drivers

 
0
  #5
Apr 13th, 2007
Well, for starters in your function prototype for your function find(), you don't have any type values...haven't taken the time to decipher your code to figure out the other problems, but this will solve part of it...
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Re: Classes and Drivers

 
0
  #6
Apr 13th, 2007
I totally overlooked that. Thank you. I have got it down to one error now. It says that count has an undeclared identifier. I declared it in the addBankAccount function. I tried declaring it before main and passing it as a parameter but I got even more errors. Here is my new code for the driver:

  1.  
  2. #include "BankAccount.h"
  3. int getChoice();
  4. void addBankAccount();
  5. void withdrawAccount();
  6. void depositAccount();
  7. void updateAccounts();
  8. void displayAccount();
  9. void displayBalance();
  10. void displayInfo();
  11. int find(int count, int accts[], int id);
  12.  
  13. double tempRate;
  14. double balance;
  15. BankAccount accts[10];
  16. int nextID = 100;
  17.  
  18. void main()
  19. {
  20. int choice;
  21. do{
  22. choice = getChoice();
  23. if( choice == 1 ) addBankAccount();
  24. else if( choice == 2 ) withdrawAccount();
  25. else if( choice == 3 ) depositAccount();
  26. else if( choice == 4 ) updateAccounts();
  27. else if( choice == 5 ) displayAccount();
  28. else if( choice == 6 ) displayBalance();
  29. else if( choice == 7 ) displayInfo();
  30. if( choice > 8 || choice < 1 ) cout << "ERROR: Please enter valid choice (1-8)." << endl;
  31. } while( choice != 8 );
  32.  
  33.  
  34. }
  35.  
  36. //This function displays the calculation menu.
  37.  
  38. int getChoice()
  39. {
  40. int c;
  41. cout << "----------------------\n";
  42. cout << "1 - Add a new bank account.\n";
  43. cout << "2 - Withdraw from a specific bank account.\n";
  44. cout << "3 - Deposit to a specific account.\n";
  45. cout << "4 - Update all accounts which will add a fixed interest rate to each account.\n";
  46. cout << "5 - Display one specific bank accout.\n";
  47. cout << "6 - Display the average balance for all of the accounts.\n";
  48. cout << "7 - Display the current information for all accounts.\n";
  49. cout << "8 - Terminate the program.\n";
  50. cout << "----------------------\n";
  51. cout << "Enter number of choice --> ";
  52. cin >> c;
  53. return c;
  54. }
  55.  
  56. //This function adds a bank account for a new member.
  57.  
  58. void addBankAccount()
  59. {
  60. int count = 0;
  61. string temp;
  62.  
  63. cout << "Please enter first name.";
  64. cin >> temp;
  65. accts[count].setFirstName(temp);
  66. cout << "Please enter last name.";
  67. cin >> temp;
  68. accts[count].setLastName(temp);
  69. cout << "Please enter balance.";
  70. cin >> balance;
  71. accts[count].setBalance(balance);
  72. cout << "Please enter interest rate (decimal form).";
  73. cin >> tempRate;
  74. accts[count].setInterestRate(tempRate);
  75. accts[count].setID(nextID);
  76. nextID = nextID + 1;
  77. count++;
  78.  
  79.  
  80. }
  81.  
  82. // This function withdraws an amount from a specific account.
  83.  
  84. void withdrawAccount()
  85. {
  86. double amt;
  87. int id;
  88. cout << "Enter ID number.";
  89. cin >> id;
  90. int slot = find(count, accts, id);
  91. cout << "Enter amount to withdraw.";
  92. cin >> amt;
  93. accts[slot].withdraw(amt);
  94.  
  95.  
  96. }
  97.  
  98. //This function deposits an amount into a specific account.
  99.  
  100. void depositAccount()
  101. {
  102. double amt;
  103. int id;
  104. cout << "Enter ID number.";
  105. cin >> id;
  106. int slot = find(count, accts, id);
  107. cout << "Enter amount to deposit.";
  108. cin >> amt;
  109. accts[slot].deposit(amt);
  110. }
  111.  
  112. //This function updates all accounts using a fixed interest rate.
  113.  
  114. void updateAccounts()
  115. {
  116. for (int i=0; i<count; i++)
  117. accts[i].update();
  118. }
  119.  
  120. //This function displays one bank account.
  121. void displayAccount()
  122. {
  123. int target;
  124. cout << "Enter account number.";
  125. cin >> target;
  126. accts[target].display();
  127. }
  128.  
  129. //This function displays the average balance of all accounts.
  130.  
  131. void displayBalance()
  132. {
  133.  
  134. }
  135.  
  136. //This function displays the current information for all accounts.
  137.  
  138. void displayInfo()
  139. {
  140. for (int i=0; i<count; i++)
  141. accts[i].display();
  142. }
  143.  
  144. int find(int count, int accts[], int id)
  145. {
  146. int index = 0;
  147. bool found = false;
  148. while ((!found) && (index < count))
  149. if (id == accts[index])
  150. found = true;
  151. else
  152. index++;
  153. if (found)
  154. return index;
  155. else
  156. return -1;
  157. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: Classes and Drivers

 
0
  #7
Apr 13th, 2007
That's because count is only a local variable in your addBankAccount function...As soon as the function ends count goes away...try declaring it at the beginning of main then pass it to your addBankAccount function...you will also have to pass count to your other functions that make use of the count variable...
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Re: Classes and Drivers

 
0
  #8
Apr 13th, 2007
Ok...I have made those changes and now I have the two errors:

  1. error C2664: 'find' : cannot convert parameter 2 from 'BankAccount [10]' to 'int []'
  2. Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  3.  
  4. error C2664: 'find' : cannot convert parameter 2 from 'BankAccount [10]' to 'int []'
  5. Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Here is my new code for the driver:

 
#include"BankAccount.h"
int getChoice();
void addBankAccount(int);
void withdrawAccount(int);
void depositAccount(int);
void updateAccounts(int);
void displayAccount();
void displayBalance();
void displayInfo(int);
int find(int count, int accts[], int id);
 
 
double tempRate;
double balance;
BankAccount accts[10];
int nextID = 100;
 
void main()
{
      int count = 0;
      int choice;
      do{
          choice = getChoice();
          if( choice == 1 ) addBankAccount(count);
          else if( choice == 2 ) withdrawAccount(count);
          else if( choice == 3 ) depositAccount(count);
          else if( choice == 4 ) updateAccounts(count);
          else if( choice == 5 ) displayAccount();
          else if( choice == 6 ) displayBalance();
          else if( choice == 7 ) displayInfo(count);
          if( choice > 8 || choice < 1 ) cout << "ERROR: Please enter valid choice (1-8)." << endl;
      } while( choice != 8 );


}
 
//This function displays the calculation menu.
int getChoice()
{
     int c;
     cout << "----------------------\n";
     cout << "1 - Add a new bank account.\n";
     cout << "2 - Withdraw from a specific bank account.\n";
     cout << "3 - Deposit to a specific account.\n";
     cout << "4 - Update all accounts which will add a fixed   interest rate to each account.\n";
     cout << "5 - Display one specific bank accout.\n";
     cout << "6 - Display the average balance for all of the accounts.\n";
     cout << "7 - Display the current information for all accounts.\n";
     cout << "8 - Terminate the program.\n";
     cout << "----------------------\n";
     cout << "Enter number of choice --> ";
     cin >> c;
     return c;
}
//This function adds a bank account for a new member.
void addBankAccount(int count)
{

      string temp;

      cout << "Please enter first name.";
      cin >> temp;
      accts[count].setFirstName(temp);
      cout << "Please enter last name.";
      cin >> temp;
      accts[count].setLastName(temp);
      cout << "Please enter balance.";
      cin >> balance;
      accts[count].setBalance(balance);
      cout << "Please enter interest rate (decimal form).";
      cin >> tempRate;
      accts[count].setInterestRate(tempRate);
      accts[count].setID(nextID);
      nextID = nextID + 1;
      count++;

 
}
 
// This function withdraws an amount from a specific account.
void withdrawAccount(int count)
{
      double amt;
      int id;
      cout << "Enter ID number.";
      cin >> id;
      int slot = find(count, accts, id);
      cout << "Enter amount to withdraw.";
      cin >> amt;
      accts[slot].withdraw(amt);
 

}
 
//This function deposits an amount into a specific account.
void depositAccount(int count)
{
      double amt;
      int id;
      cout << "Enter ID number.";
      cin >> id;
      int slot = find(count, accts, id);
      cout << "Enter amount to deposit.";
      cin >> amt;
      accts[slot].deposit(amt);
}
 
//This function updates all accounts using a fixed interest rate.
void updateAccounts(int count)
{
      for (int i=0; i<count; i++)
           accts[i].update();
}
 
//This function displays one bank account.
void displayAccount()
{
       int target;
       cout << "Enter account number.";
       cin >> target;
       accts[target].display();
}
 
//This function displays the average balance of all accounts.
 
void displayBalance()
{
 
}
//This function displays the current information for all accounts.
 
void displayInfo(int count)
{
       for (int i=0; i<count; i++)
             accts[i].display();
}
 
int find(int count, int accts[], int id)
{
     int index = 0;
     bool found = false;
     while ((!found) && (index < count))
          if (id == accts[index])
             found = true;
         else
             index++;
     if (found)
         return index;
     else
         return -1;
}

Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: Classes and Drivers

 
0
  #9
Apr 13th, 2007
That is because you have the wrong type in the prototype...you are passing acct which is of type BankAccount...therefore you need to change your prototype and header to something like this:
  1. int find(int count, BankAccount accts[], int id);
hope this helps...
Last edited by ft3ssgeek; Apr 13th, 2007 at 9:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Re: Classes and Drivers

 
0
  #10
Apr 14th, 2007
Geez...I have no idea why it's still not working! I made those corrections and now I have 8 errors which all seem to relate to the find function. Is it not correct?:

  1. error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const
  2.  
  3. error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'int'
  4.  
  5. error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'int'
  6.  
  7. error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'int'
  8.  
  9. error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'int'
  10.  
  11. error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'int'
  12.  
  13. error C2784: 'bool std::operator ==(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'int'
  14.  
  15. error C2677: binary '==' : no global operator found which takes type 'BankAccount' (or there is no acceptable conversion)
  16.  


DRIVER CODE:

#include"BankAccount.h"
int getChoice();
void addBankAccount(int);
void withdrawAccount(int);
void depositAccount(int);
void updateAccounts(int);
void displayAccount();
void displayBalance();
void displayInfo(int);
int find(int count, BankAccount accts[], int id);
 
 
double tempRate;
double balance;
BankAccount accts[10];
int nextID = 100;
 
void main()
{
      int count = 0;
      int choice;
      do{
          choice = getChoice();
          if( choice == 1 ) addBankAccount(count);
          else if( choice == 2 ) withdrawAccount(count);
else if( choice == 3 ) depositAccount(count);
          else if( choice == 4 ) updateAccounts(count);
          else if( choice == 5 ) displayAccount();
          else if( choice == 6 ) displayBalance();
          else if( choice == 7 ) displayInfo(count);
          if( choice > 8 || choice < 1 ) cout << "ERROR: Please enter valid choice (1-8)." << endl;
      } while( choice != 8 );


}
 
//This function displays the calculation menu.
int getChoice()
{
     int c;
     cout << "----------------------\n";
     cout << "1 - Add a new bank account.\n";
     cout << "2 - Withdraw from a specific bank account.\n";
     cout << "3 - Deposit to a specific account.\n";
     cout << "4 - Update all accounts which will add a fixed   interest rate to each account.\n";
     cout << "5 - Display one specific bank accout.\n";
     cout << "6 - Display the average balance for all of the accounts.\n";
     cout << "7 - Display the current information for all accounts.\n";
     cout << "8 - Terminate the program.\n";
     cout << "----------------------\n";
     cout << "Enter number of choice --> ";
     cin >> c;
     return c;
}
//This function adds a bank account for a new member.
void addBankAccount(int count)
{

      string temp;

      cout << "Please enter first name.";
      cin >> temp;
      accts[count].setFirstName(temp);
      cout << "Please enter last name.";
      cin >> temp;
      accts[count].setLastName(temp);
      cout << "Please enter balance.";
      cin >> balance;
      accts[count].setBalance(balance);
      cout << "Please enter interest rate (decimal form).";
      cin >> tempRate;
      accts[count].setInterestRate(tempRate);
      accts[count].setID(nextID);
      nextID = nextID + 1;
      count++;

 
}
 
// This function withdraws an amount from a specific account.
void withdrawAccount(int count)
{
      double amt;
      int id;
      cout << "Enter ID number.";
      cin >> id;
      int slot = find(count, accts, id);
      cout << "Enter amount to withdraw.";
      cin >> amt;
      accts[slot].withdraw(amt);
 

}
 
//This function deposits an amount into a specific account.
void depositAccount(int count)
{
      double amt;
      int id;
      cout << "Enter ID number.";
      cin >> id;
      int slot = find(count, accts, id);
      cout << "Enter amount to deposit.";
      cin >> amt;
      accts[slot].deposit(amt);
}
 
//This function updates all accounts using a fixed interest rate.
void updateAccounts(int count)
{
      for (int i=0; i<count; i++)
           accts[i].update();
}
 
//This function displays one bank account.
void displayAccount()
{
       int target;
       cout << "Enter account number.";
       cin >> target;
       accts[target].display();
}
 
//This function displays the average balance of all accounts.
 
void displayBalance()
{
 
}
//This function displays the current information for all accounts.
 
void displayInfo(int count)
{
       for (int i=0; i<count; i++)
             accts[i].display();
}
 
int find(int count, BankAccount accts[], int id)
{
     int index = 0;
     bool found = false;
     while ((!found) && (index < count))
          if (id == accts[index])
             found = true;
         else
             index++;
     if (found)
         return index;
     else
         return -1;
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC