0

So i have this code, part of a larger function.

int size, j;
    cout << "Enter the size of array" << endl;
    cin >> size;
    float b, n[size];// error

and I get the already famous E0028-expression must have a constant value. Now I saw people getting around this with "new int" and while I kinda understand the concept of it, technically I have not learned this type of int, not yet,nor objects etc. Also where I'm learning c++ they tell me that this should work just fine. I use visual studio enterprise 2017 to code(maybe there is a problem on my end with the compiler). Basically what I want is an array that has the size of it decided by the user input. And yes I know it wants to have a const and not a variable value. What are any work-arounds this ? (answer like you would try to teach your dog programming please because that is where my knowledge lies). Thank you.

Edit: While I see people trying to tell me use std::vector(that again i technically did not learn but kinda understand the concept of it) the people from the place where i'm learning c++ are telling me it should work that way.I did read a bit about the error before asking the question and saw some related stuff about the c99 standard( 2 much stuff to make a wall of text here). So the follow up question is: are they teaching outdated ways of writing this stuff ? Thank you.

2

2 Answers 2

0

Variable length arrays, such as n[size], are not supported by standard C++, although some compilers allow it as an extension. (Note that C allows it, although it was made optional in C11.)

Use std::vector<float> n(size); in your case instead. That will allow you to access elements of n using []; e.g. n[0] is the first element.

As a rule of thumb, use a std::vector to model an array of a numeric type, unless you can think of a good reason not to.

Sign up to request clarification or add additional context in comments.

2 Comments

note that it's not allowed in all C standards. C99 requires it but in C11 it's optional
@LưuVĩnhPhúc: Too right - that's one for the pub quiz.
0

The error is selfeplanatory: you cannnot use non-const expression in float n[size];.

In your case you need to use new operator or use one of standard containers: std::array or std::vector if you want to change the array size later on.

4 Comments

Well thank you for the answer but what i'm actually trying to figure out is why are they telling me it should work ? On another forum someone suggested it has something to do with the c99 standard(read a bit about it). Is the place where i'm learning just using very old methods that are outdated ?
@Bubu, C allows it. Some C++ compilers allow it, but as an extension to the language.
@Bathsheba Thank you dude, so basically they are using C methods and I should first make sure I understand vectors v well before going to arrays. Or get extensions (which I feel like is kind of cheating in this example) instead of learning new things (vectors) and applying them.
@Bubu: Indeed. std::vector is the way to go.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.