I'm trying to let the user choose between three functions for the program to run, but when I compile I get the error" variable-sized object `addnumber' may not be initialized" on line 47. My code:
The length of an array must be known at compile-time. In other words, the length must be constant. Therefore, a value only known at run-time cannot be used to determine the array length.
If it's possible, you can use dynamic memory allocation[1]. This will allow you to create an array during run-time:
1 2 3 4 5 6 7 8 9 10 11
int input(0);
// Get the length of the array:
std::cin >> input;
// Create an array dynamically:
double *dynamic_array(newdouble[input]);
// Delete the array (MUST DO THIS!):
delete [] dynamic_array;
dynamic_array = 0x0;
You could just remove that line, since "addnumber" is not used anywhere else in the program.
However, the explanation is that "c" is an integer with a value not known at compile time (since it is input by the user).
Hence this double addnumber[c] is attempting to define an array with a variable length. This is not usually valid in C++ (though some compilers may accept it).
If you did need to use addnumber[], the simplest solution would be to set it to a size at least as large as required, such as double addnumber[10], and make sure the program never tries to use any element beyond addnumber[9].
Note also the initialisation part = {d,e,f,g,h,i,j}; does not make sense in any case.