简体   繁体   English

我需要课堂帮助。 (C ++)

[英]I need help with classes. (C++)

Well I am trying to do an exercise in my programming book and it's difficult for me to grasp exactly what it wants. 好吧,我正在尝试在我的编程书中做一个练习,我很难准确地了解它想要什么。

My "enterAccountData()" function is suppose to ask the user for an account number and a balance neither of which can be negative and the account number cannot be less than 1000 我的“ enterAccountData()”函数假设是询问用户一个帐号和一个余额,两者均不能为负,并且帐号不能小于1000

The second one is the one I am stuck on "computeInterest()" THis function is suppose to accept an integer argument that represents the number of years the account will earn interest. 第二个是我被困在“ computeInterest()”上的那个函数。此函数假定接受一个整数参数,该参数表示帐户将获得利息的年数。 The function then displays the account number from the previous function and displays its ending balance at the end of each year based on the interest rate attached to the BankAccount class. 然后,该函数显示上一个函数的帐号,并根据BankAccount类的利率在每年年底显示其期末余额。 (The interest rate on "BankAccount" has to be a constant static field which is set at 3 percent(0.03)). (“ BankAccount”上的利率必须是一个恒定的静态字段,该字段设置为3%(0.03))。

So My question is this: How do I set up "computeInterest()" too allow it to calculate the interest using the constant static field when my debugger will not allow me to actually keep the field as a constant? 所以我的问题是这样的:当调试器不允许我将字段实际保持为常量时,如何设置“ computeInterest()”以使其也可以使用常量静态字段来计算利息? I am not trying to stop any random errors from happening for now, just trying to get the jist of what the book is exactly asking for. 我不是想暂时阻止任何随机错误,而只是想弄清楚这本书到底是什么。 Here is my code. 这是我的代码。

#include <iostream>
#include <iomanip>

using namespace std;

class BankAccount
{
  private:
    int accountNum;
    double accountBal;
    static double annualIntRate;
  public:
    void enterAccountData(int, double);
    void computeInterest();
    void displayAccount();
};

//implementation section:
double BankAccount::annualIntRate = 0.03;
void BankAccount::enterAccountData(int number, double balance)
{
    cout << setprecision(2) << fixed;
    accountNum = number;
    accountBal = balance;

    cout << "Enter the account number " << endl;
    cin >> number;

    while(number < 0 || number < 999)
    {
        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;

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

    cout << "How many months will the account be held for? ";
    cin >> months;
    counter = 0;
    do
    {
        balance = accountBal * rate + accountBal;
        counter++;
    }while(months < counter);
    cout << "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);

    accounts[counter].computeInterest();

    system("pause");
    return 0;
}

The constant field is easy enough: 常量字段很容易:

class BankAccount
{
  ...
  const static double annualIntRate = 0.03;
  ...
}

(Does your debugger complain about that? I'm using gcc 4.2.1) But there are other troubling things in your code, like the way computeInterest tries to set rate to zero, and the while loop... needs work. (您的调试器是否对此表示抱怨?我使用的是gcc 4.2.1)但是您的代码中还有其他麻烦的事情,例如computeInterest尝试将rate设置为零的方式,而while循环...需要工作。

EDIT: 编辑:
One good principle is worth a hundred specific corrections. 一个好的原则值得进行一百次具体的更正。 When you develop a complicated function, don't try to do it all at once; 开发复杂的功能时,请勿尝试一次完成所有操作。 start with a simple piece, get that working perfectly, then build up. 从一个简单的部分开始,使其完美运行,然后进行构建。 F'rinstance, computeInterest . F'rinstance, computeInterest You have several independent parts to get working: going through the while loop the correct number of times, calculating interest increments, keeping track of the balance-- right now computeInterest does none of these correctly. 您需要几个独立的部分来进行工作:在while循环中进行正确的次数,计算利息增量,跟踪computeInterest现在, computeInterest不能正确执行这些操作。 Tackle them one at a time, or in parallel if you want, but never combine parts that don't work. 一次解决一个问题,或者根据需要并行解决,但不要合并不起作用的部分。

boy it's been a LONG time since I've worked in C++, but I think all you have to do is this: 小男孩,自从我从事C ++工作以来已经很长时间了,但是我认为您要做的就是:

static double annualIntRate =.03;

in the "private" section of your code. 在代码的“私有”部分中。

and then you can use annualIntRate as though it was a global (to each instance of the class) variable. 然后就可以使用annualIntRate ,就好像它是一个全局变量(对于该类的每个实例)一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM