gpt4 book ai didi

c++ - 类循环

转载 作者:太空宇宙 更新时间:2023-11-04 12:20:40 25 4
gpt4 key购买 nike

在我的“computeInterest()”函数中,我需要一个循环来显示先前用于从“enterAccountData()”填充数组的帐号,我会使用一个嵌套循环来查看帐号吗?还是只有一个循环?

提前感谢您的帮助,这是我的代码。

#include <iostream>
#include <iomanip>

using namespace std;

class BankAccount
{
private:
int accountNum;
double accountBal;
static const double annualIntRate;
public:
double enterAccountData(int, double);
void computeInterest();
void displayAccount();
};
//implementation section:
const double BankAccount::annualIntRate = 0.03;
double 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 balance,number;
}
void BankAccount::computeInterest()
{
const int MAX_ACCOUNTS = 10;
const int MONTHS_IN_YEAR = 12;
int months;
double rate = 0;
int counter = 0;

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

我注释掉了一个 for 循环,我之前厌倦了它不起作用。

最佳答案

您不需要循环。即使使用循环,也总是打印相同的值。

为什么?

该程序有 10BankAccount 实例,这意味着每个实例都有自己的类成员拷贝。

const int MAX_ACCOUNTS = 10;
const int MONTHS_IN_YEAR = 12; // Both of these declared global

而在main()中,如果需要显示所有账户信息——

for(int i = 0; i < MAX_ACCOUNTS; ++i)
{
accounts[i].displayAccount(); // Where in displayAccount definition is to display accountNum and accountBal
}

而如果在BankAccount::ComputeInterest()中,需要显示账户信息-

void BankAccount::ComputeInterest() 
{


// ......

std::cout << "\n Acc. Num:\t " << accountNum << "\t Acc. Bal:\t" << accountBal << "\n";

}

BankAccount::ComputeInterest() 不采用任何引用其他实例的参数。因此,它具有仅调用它的对象信息。并且可以更好地组织程序。

变量有不必要的重复(即 MAX_ACCOUNTS 等)

double BankAccount::enterAccountData(int number, double balance) 返回类型为 double

double BankAccount::enterAccountData(int number, double balance)
{
// ....

return balance,number;
}

return 永远不会返回多个值。返回最后一个变量的值。因此,仅返回 number

关于c++ - 类循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5292143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com