I have little to no knowledge of C++ and how to use arrays. That being said, what I'm trying to do is create a simple class for chemical elements which automatically decides the number of shells allotted to that element based on just its atomic number. Here's my sample code:
class Element {
int n, i;
int s = 1;
for (int i = 2; i < n; i += 8) {s += 1;}
int shell[s + 1];
public: Element(int n) {this.n = n;}
};
That snippet of code is supposed to create an array called int shell[s + 1]
which contains s-1 shells. I made it s-1 instead of s so I wouldn't constantly confuse myself by referring to shell #1 as shell[0] and so forth. Thus, shell[0] is unused. Or I could do it the other way around and actually use shell[0], but that's irrelevant. As you can see, int s
is automatically set to 1 because all elements contain at least one shell. Then there's a for loop that adds shells based on int n
. Finally, I declared the array int shell[s + 1]
.
Ultimately, I got a multitude of errors. Most of them were nonsensical syntax errors, but apparently in C++ you're not allowed to initialize non-final instance variables. That doesn't make much sense to me, because I really need int s
to begin at 1 for the for loop. It also tells me that I can't make variable-sized arrays, either. What do?