簡體   English   中英

如何修復錯誤? main.cpp|21|錯誤:無效類型 'float [3] 第 21 行

[英]How do I fix error? main.cpp|21|error: invalid types 'float [3] Line 21

我在第 21 行和第 31 行收到錯誤。當我將數組聲明為 int 並用 int 值填充數組時,該程序運行良好。 要求是用浮點值填充數組,但它不起作用

#包括

using namespace std;

int main()
{   float i=0, j=0;
    float Stock_Items=3;
    float NR_materials=3;
    float total_expenses=0;
    float leather_items=0;
    cout << "Hello user! Welcome to the furniture program \n" << endl;
    std::cout<<"This program works using a 2 D array \n \n It calculates the amount of stock in the shop and the amount of leather items"<<"\n";
    //declaration of 2 dimensional array with all items in the shop
    //This array consists of 3 rows and 3 columns- 3 furniture type and 3 materials
    float stockArray[3][3]= {{1000.75,700.00,900.60},{2500.65,2800,2000.78},{500.49,2000.90,200.99}};
    //For loop for calculating total amount of items in the shop
    for ( i = 0; i < 3; i++ )//rows
    {
        for ( j = 0; j < 3; j++ )//columns
        {
            total_expenses+=stockArray[i][j];//sum

        }
    }

//For loop for calculating total amount of leather items in the shop
    for (i=0; i<3; i++)//rows- goes through all rows
    {
        for(j=1; j<2; j++)//columns - only goes through column 1
        {
            leather_items+=stockArray[i][j];//sum
        }
    }//display results to the UI
    std::cout<<"The total amount of stock in the shop is: "<<total_expenses<<" items. \n \n";
    std::cout<<"The total amount of leather items is: "<<leather_items<<" items.\n \n";

    return 0;
}

即使您有一個float數組,數組索引(如stockArray[i][j]中的ij )仍必須是整數。 因此,不是float i=0, j=0; ,而是float i=0, j=0; , 使用int i=0, j=0; .

i 和 j 應聲明為整數而不是浮點數。 使用 for 循環聲明它們實際上可能更好。 例如:

for (int i = 0; ...) {
  for (int j = 0; ...) {...}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM