对我书中的练习感到困惑

时间:2011-03-14 04:30:18

标签: c++ visual-studio-2008 class function

我需要能够使数组中的对象有效或无效/具有值或没有值。

因此,如果用户只输入10个帐户中的5个帐户,那么它会丢弃最后5个没有任何价值的帐户,以便不要求对不存在的帐户计算利息

我该怎么做?

#include <iostream>
#include <iomanip>

using namespace std;

class BankAccount
{
private:
int accountNum;
double accountBal;
static const double annualIntRate;
public:
void enterAccountData(int, double);
void computeInterest();
void displayAccount();
};
//implementation section:
const double BankAccount::annualIntRate = 0.03;
void BankAccount::enterAccountData(int number, double balance)
{
cout << setprecision(2) << fixed;



cout << "Enter the account number " << endl;
cin >> number;
accountNum = number;
while(number < 0 || number < 1000)
{
    cout << "Account numbers cannot be negative or less than 1000 " <<
        "Enter a new account number: " << endl;
    cin >> number;
}

cout << "Enter the account balance " << endl;
cin >> balance;
accountBal = balance;
while(balance < 0)
{
    cout << "Account balances cannot be negative. " <<
        "Enter a new account balance: " << endl;
    cin >> balance;
}
return;
}
void BankAccount::computeInterest()
{
const int MAX_ACCOUNTS = 10;
const int MONTHS_IN_YEAR = 12;
int months;
double rate = 0; 
int counter = 0;

cout << endl << "How many months will the account be held for? " << endl;
cin >> months;
counter = 0;
do
{
 accountBal = accountBal * annualIntRate + accountBal;
 counter++;
}while(months > counter);

cout << endl << "Account # " << accountNum << " 's balance is:$" << accountBal << endl;
}

int main()
{
const int QUIT = 0;
const int MAX_ACCOUNTS = 10;
int counter;
int input;
int number = 0;
double balance = 0;

BankAccount accounts[MAX_ACCOUNTS];
//BankAccount display;

counter = 0;

do
{
    accounts[counter].enterAccountData(number, balance);
    cout << " Enter " << QUIT << " to stop, or press 1 to proceed.";
    cin >> input;
    counter++;

}while(input != QUIT && counter != 10);

for(counter = 0; counter < MAX_ACCOUNTS; counter++)
{
    accounts[counter].computeInterest();
}
system("pause");
return 0;
}

3 个答案:

答案 0 :(得分:1)

看来你已经跳过了关于阵列的章节。重新阅读!

PS。肯定是0&lt; 1000 while(number < 0 || number < 1000)

答案 1 :(得分:1)

你应该尝试将问题简化为你真正想要的问题,这看起来有点令人困惑。

在第一个循环之后,让它“记住”它设法达到的计数器的值,然后在第二个循环中只迭代到那个,而不是最多到MAX_ACCOUNTS。

从几个月到几年的转换只是除以12没有?

答案 2 :(得分:1)

void BankAccount::enterAccountData(int number, double balance)。这两个论点有什么需要?直接获取成员变量的输入。评论了不必要的操作。这也可以完成这项工作 -

void BankAccount::enterAccountData()
{
    cout << setprecision(2) << fixed;

    cout << "Enter the account number " << endl;
    cin >> accountNum; // Take input directly to accountNum
    //  accountNum = number;  // No need of this assignment
   while(accountNum < 0 || accountNum < 1000)
   {
       cout << "Account numbers cannot be negative or less than 1000 " <<
    "Enter a new account number: " << endl;
       cin >> accountNum;
   }

   cout << "Enter the account balance " << endl;
   cin >> accountBal;
   //  accountBal = balance; // Unnecessary assignment
   while(accountBal < 0)
   {
       cout << "Account balances cannot be negative. " <<
    "Enter a new account balance: " << endl;
       cin >> accountBal;
   }
   //  return;  // What is the need of this return statement? Method signature says
                // it doesn't return anything.
}

当您考虑声明变量时,请考虑它们是否必要。不必要地声明和分配变量会使问题看起来很复杂,并且是混淆的根源。想想简单:)