944,033 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3344
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 13th, 2007
0

Classes and Drivers

Expand 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.


HEADER


C++ Syntax (Toggle Plain Text)
  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


C++ Syntax (Toggle Plain Text)
  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


C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007
Apr 13th, 2007
0

Re: Classes and Drivers

Click to Expand / Collapse  Quote originally posted by RaCheer ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Apr 13th, 2007
0

Re: Classes and Drivers

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:

C++ Syntax (Toggle Plain Text)
  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 ==========

Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007
Apr 13th, 2007
0

Re: Classes and Drivers

HEADER


C++ Syntax (Toggle Plain Text)
  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


C++ Syntax (Toggle Plain Text)
  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


C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007
Apr 13th, 2007
0

Re: Classes and Drivers

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...
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Apr 13th, 2007
0

Re: Classes and Drivers

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:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007
Apr 13th, 2007
0

Re: Classes and Drivers

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...
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Apr 13th, 2007
0

Re: Classes and Drivers

Ok...I have made those changes and now I have the two errors:

C++ Syntax (Toggle Plain Text)
  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;
}

Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007
Apr 13th, 2007
0

Re: Classes and Drivers

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:
C++ Syntax (Toggle Plain Text)
  1. int find(int count, BankAccount accts[], int id);
hope this helps...
Last edited by ft3ssgeek; Apr 13th, 2007 at 9:06 pm.
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Apr 14th, 2007
0

Re: Classes and Drivers

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?:

C++ Syntax (Toggle Plain Text)
  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;
}
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007

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: Print the parallel array index with the largest value?
Next Thread in C++ Forum Timeline: dev-c++ and libtiff





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


Follow us on Twitter


© 2011 DaniWeb® LLC