c++ - 数组结构

标签 c++ arrays struct

我的代码有问题。我似乎无法理解如何在使用结构时将我的余额实现到数组中。有没有人可以帮助我?

#include <iostream>
#include <iomanip>

using namespace std;    

struct BankAccount
{
    int accountNum;
    double accountBal;
    double annualIntrest;
    int term;
};
int main()
{
    const int BANKACC = 5;
    const int QUIT = 1;
    const int MONTHS_IN_YEAR = 12;
    int x,found,input,month;
    double total = 0;
    double average = 0;
    BankAccount accounts[BANKACC];

    for(x = 0; x < BANKACC; x++)
    {
        do
        {
            found = 0;
            cout << "Enter in account # " << (x + 1) << endl;
            cin >> accounts[x].accountNum;
            while(accounts[x].accountNum < 1000 || accounts[x].accountNum > 9999)
            {
                cout << "Account number must be four didgets:" << endl;
                cin >> accounts[x].accountNum;
            }
            for(int check = 0; check < x; check++)
            {
                while(accounts[x].accountNum == accounts[check].accountNum)
                {
                    cout << endl << "Account Numbers cannot be the same, enter in a new account number." << endl;
                    found = 1;
                    break;
                }
            }
        } while(found); 

        cout << "Enter the accounts balance."  << endl;
        cin >> accounts[x].accountBal;
        while(accounts[x].accountBal < 0)
        {
            cout << "Account cannot have a negitive balance." << endl;
            cin >> accounts[x].accountBal;
        }
        cout << "Enter the interest rate." << endl;
        cin >> accounts[x].annualIntrest;
        while(accounts[x].annualIntrest > 0 && accounts[x].annualIntrest > 0.15)
        {
            cout << "Annual interest must be from 0 to 0.15." << endl;
            cin >> accounts[x].annualIntrest;
        }
        cout << "How many years will the term be held for? " << endl;
        cin >> accounts[x].term;

            while(accounts[x].term < 1 || accounts[x].term > 10)
            {
                cout << "The Term must be greater than 1 and should not exceed 10" << endl;
                cin >> accounts[x].term;
            }
    }
    for(int year = 1; year < accounts[x].term; year++)
    {
        for( month = 1; month < MONTHS_IN_YEAR; month++)
        { 
            accounts[x].accountBal = accounts[x].accountBal * accounts[x].annualIntrest + accounts[x].accountBal;
            total += accounts[x].accountBal;
            x++;

        }
        month = 1;
        average = total  / BANKACC;
    }
    for(x = 0; x < BANKACC; x++)
    {
        cout << "Account # " << (x + 1) << "'s number is: " << accounts[x].accountNum;
        cout << " The accounts balance is: " << accounts[x].accountBal;
        cout << " The interest on the account is: " << accounts[x].annualIntrest << endl;
    }

    cout << "Average of all the bank accounts is: " << average << endl;

    cout << "Which account do you want to access?" << endl <<
        " To stop or look at none of the account numbers type " << QUIT << endl;    
        for(x = 0; x < BANKACC; x++)
        {
            cout << accounts[x].accountNum << "    ";
        }
        cin >> input;

        while(input != QUIT)
        {
            found = 0;
            x = 0;
            while(x < BANKACC && input != accounts[x].accountNum)
            {
                x++;            
            }
                if(input == accounts[x].accountNum)
                {
                    cout << "Account:" << accounts[x].accountNum << " balance is: " <<
                    accounts[x].accountBal << " Interest rate is: " << accounts[x].annualIntrest;
                    cout << endl << "Enter the another account number or type 1 to quit.";
                    found = 1;
                    cin >> input;
                }
            if(found == 0)
            {
            cout << "Sorry that account doesn't exist. Enter another account number.";
            cin >> input;
            }
        }
    system("pause");
    return 0;
}

最佳答案

一切看起来都很好,直到这一行:

for(int year = 1; year < accounts[x].term; year++) 

此代码超出了 for 循环的范围:

for(x = 0; x < BANKACC; x++) { ... }

我认为多年来的循环应该在BANKACC循环内部。其一,当代码到达这里时,x 超出了它看起来像的数组范围。

但这不是唯一的问题。在年和月中迭代的循环总是比应有的少 1,因为它们从 1 开始,一直持续到 (< term) 或 (< months_in_year)

此外,计算平均值的方式似乎也是错误的。

关于c++ - 数组结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5084954/

相关文章:

C++ 链接 boost 库

c++ - boost.spirit qi 中序列和列表运算符的属性?

Java 平均水平及以上

c - 目标文件的多重定义

c - 没有命名成员的结构在哪里有用?

c++ - open() 的参数 : Everyone should have all access to file & should be truncated on creation

c++ - 基类成员没有期望值

arrays - 在 Julia 中,如何初始化一个二维数组(矩阵),其中每个元素都是一个列向量?

php - 在 php 中求和两个数组值

ios - 函数中缺少返回 - 模型中出现快速错误