数组类型' int [8]'不可分配

时间:2018-02-09 00:13:06

标签: c++ arrays

长期坚持这一点并且无法解决问题,不断获取错误消息数组类型' int [8]'不可分配。另外,它告诉我,数组条目表达必须是一个可修改的左值'我试图多次查找,似乎无法想到任何事情,任何帮助都很有用,谢谢:)     使用namespace std;

const int MAXACCOUNTS = 8;


int main()

{
cout << "Welcome" << endl;
int counter = 0;
int counter1 = 0;
int interest_counter = 0;

struct Account
{
    int AccountNumber[MAXACCOUNTS] ;
    double Balance[MAXACCOUNTS];
    int DaySinceDebited[MAXACCOUNTS];
};

Account entries[MAXACCOUNTS];

while (counter1 != MAXACCOUNTS)
{
    if (counter1 = 0)
    {
        entries[counter1].AccountNumber = 1001;
        entries[counter1].Balance = 4254.40;
        entries[counter1].DaySinceDebited = 20;
        counter += 1;
    }
    else if (counter1 = 1)
    {
        entries[counter1].AccountNumber = 7940;
        entries[counter1].Balance = 27006.25;
        entries[counter1].DaySinceDebited = 35;
        counter += 1;
    }
    else if (counter1 = 2)
    {
        entries[counter1].AccountNumber = 4382;
        entries[counter1].Balance = 123.50;
        entries[counter1].DaySinceDebited = 2;
        counter += 1;
    }
    else if (counter1 = 3)
    {
        entries[counter1].AccountNumber = 2651;
        entries[counter1].Balance = 85326.92;
        entries[counter1].DaySinceDebited = 14;
        counter += 1;
    }
    else if (counter1 = 4)
    {
        entries[counter1].AccountNumber = 3020;
        entries[counter1].Balance = 657.00;
        entries[counter1].DaySinceDebited = 5;
        counter += 1;
    }
    else if (counter1 = 5)
    {
        entries[counter1].AccountNumber = 7168;
        entries[counter1].Balance = 7423.34;
        entries[counter1].DaySinceDebited = 360;
        counter += 1;
    }
    else if (counter1 = 6)
    {
        entries[counter1].AccountNumber = 6245;
        entries[counter1].Balance = 4.99;
        entries[counter1].DaySinceDebited = 1;
        counter += 1;
    }
    else if (counter1 = 7)
    {
        entries[counter1].AccountNumber = 9342;
        entries[counter1].Balance = 107864.44;
        entries[counter1].DaySinceDebited = 45;
        counter += 1;
    }

}

cout << "Acct No   Balance   Interest Paid" << endl; 

while (counter != MAXACCOUNTS)
{
    cout << entries[counter].AccountNumber << "   " << entries[counter].Balance << "  " << entries[counter].DaySinceDebited << endl;
    counter += 1; 
}
return 0;
}

1 个答案:

答案 0 :(得分:2)

您已将AccountNumberBalanceDaySinceDebited声明为数组,但您尝试为它们分配单个值。您将它们视为不是数组,因为逻辑上它们对于数组来说没有意义。请记住,您有多个帐户(一组帐户),但每个帐户只有一个帐户编号,余额等...不要使它们成为数组。而是将它们声明为:

int AccountNumber;
double Balance;
int DaySinceDebited;

此外,在每个if中,您需要使用==进行比较。截至目前,您的陈述如下:

else if (counter1 = 1) {
                 ^^^

这会将1的值分配给counter1,然后检查它的计算结果是否为true。由于所有整数(0除外)在C ++中都计算为true,因此将始终运行。将这些更改为:

else if (counter == 1) {