gpt4 book ai didi

c++ - 如何解决错误? main.cpp | 21 |错误: invalid types 'float [3] Line 21

转载 作者:行者123 更新时间:2023-12-03 08:09:51 25 4
gpt4 key购买 nike

我在第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数组,数组索引(如i中的jstockArray[i][j])仍必须是整数。因此,请使用float i=0, j=0;而不是int i=0, j=0;

关于c++ - 如何解决错误? main.cpp | 21 |错误: invalid types 'float [3] Line 21,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64519267/

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