Aug 24, 2015 at 8:40pm UTC
here there every time i try to compile this code:
#include <iostream>
using namespace std;
int main()
{
float sum = 0.000, product = 1;
float num{6}
;for(float counter = 0.3221 ; counter <= 5 ; counter++)
{
cout << "eneter in number ";
cin >> num[counter];
sum=sum+num[counter];
product=product+num[counter];
}
cout << "sum " << sum << endl;
cout << "product " << product << endl;
return 0;
}
this is the error i got:
error: invalid types 'float[float]' for array subscript|
how do i fix this?
i'm trying to do calculator for my first program witch i chose to use multiplication.
Aug 24, 2015 at 9:28pm UTC
num is a floating point number and you are trying to access it as though it were an array. Additionally, you are trying to access an element which is not a whole number; what exactly would the 0.32221th element of an array be?
Sep 1, 2015 at 4:44pm UTC
hmmm its float not a function for an array
Sep 1, 2015 at 5:59pm UTC
float num{6}
is the same as writing float num = 6
.
If you want num to be an array of 6 floats you should write float num[6]
.
Last edited on Sep 1, 2015 at 5:59pm UTC