variable sized object may not be initialized

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    //char b;
    cout << "Choose to find the average, square or add" << endl;
    string input;
    cin >> input;
    
    if (input== "Average")
    {
    
    double x;
    double y;
    double w; 
    double z;
	cout << "Enter numbers to be added" << endl;
	cin >> x; 
	cin >> y; 
	cin >> w;
	cin >> z;
	cout << "The average is: " << endl;
	cout << (x + y + w + z) / 4 << endl;
}
    else if (input== "Square")
    {
	double a;

	cout << "Enter number to be squared" << endl;
	cin >> a;
	cout << "Square is: " << a * (a) << endl;
}
    else if (input== "Add")
    {
         int c;
         double d;
         double e;
         double f;
         double g;
         double h;
         double i;
         double j;
         cout << "Enter how many numbers you want to add" << endl;
         cin >> c;
         double addnumber[c] = {d,e,f,g,h,i,j};
         cout << "Enter numbers to be added" << endl;
         
         if (c = 2)
         {
               cin >> d;
               cin >> e;
               cout << "Sum is: " << d + e << endl;
               }
         else if (c = 3)
         {
              cin >> d;
              cin >> e;
              cin >> f;
              cout << "Sum is: " << d + e + f << endl;
              }      
         else if (c = 4)
         {
              cin >> d;
              cin >> e;
              cin >> f;
              cin >> g;
              cout << "Sum is: " << d + e + f + g << endl;
              }     
         else if (c = 5)
         {
              cin >> d;
              cin >> e;
              cin >> f;
              cin >> g;
              cin >> h;
              cout << "Sum is: " << d + e + f + g + h << endl;
              } 
         else if (c = 6)
         {
              cin >> d;
              cin >> e;
              cin >> f;
              cin >> g;
              cin >> h;
              cin >> i;
              cout << "Sum is: " << d + e + f + g + h + i << endl;
              }    
         else if (c > 6)
         {
              cout << "Yo number is too big bro" << endl;
              }
         else 
         {
              cout << "Yo number is invalid dawg" << endl;
              }
         }
	
	
	system("PAUSE");
	return 0;
}
closed account (zb0S216C)
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(new double[input]);

// Delete the array (MUST DO THIS!):
delete [] dynamic_array;
dynamic_array = 0x0;

References:
[1] http://www.cplusplus.com/doc/tutorial/dynamic/


Wazzak
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.
I should think the simplest way would be not to use array storage at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    std::cout << "Enter the amount of numbers to sum: " ;

    unsigned numbers ;
    std::cin >> numbers ;

    std::cout << "Enter the numbers\n" ;

    double total = 0.0 ;
    for ( unsigned i=0;  i<numbers; ++i )
    {
        double input ;
        std::cin >> input ;
        total += input ;
    }

    std::cout << "The total is " << total ;
    std::cout << "\nThe average is " << total / numbers << '\n' ;
}
Topic archived. No new replies allowed.