0

What i would like to do is declare an array with "dim" size :int A[dim];. Now, this works if I declare something like const int dim = 1 but doesn't with const int dim = round(x);, which is what i need to do. (Where x comes from cin >> x.)

Note: With "doesn't work" i refer to Visual Studio Code throwing red wavy line under dim in int A[dim]; and displaying the following when hovering it with my mouse: `

expression must have a constant valueC/C++(28)
main.cpp(15, 11): the value of variable "dim" (declared at line 13) cannot be used as a constant

`


This is the relevant code:

#include <iostream>
using namespace std;

int main(){
    float x;
    cin >> x;
    
    const int dim = round(x);
    int A[dim];
    int i = 0;
    }
}

Given the context i believe the error is caused by one of two reasons:

  1. Some characteristic of round() that makes the const int dim = round(x) not recognized as constant from the array later.
  2. The problem is the x and not the round() so cin >> x is the reason.

[Thanks for whoever can explain me what I'm missing or point to some documentation that does. I have done some research but I haven't found a solution to this. Also this is my first question on SO, so tell me if I should change/improve something]

EDIT: Apparently the problem isn't in the round(x) as I previously thought because simply replacing const int dim = round(x); with const int dim = x; gives the same "error".

So the problem has to do with cin >> x .

EDIT 2 Note: I'm looking for a solution that doesn't use std::vector. We haven't studied it yet in the course so I believe the algorithm(from which i took the relevant code) shouldn't comprehend it.

Final Edit I didn't realize that, as @paulmckenzie clarified, using cin made the array dynamic because the imput comes in runtime, it was a really stupid error but I apologize, I'm really a beginner. In my defense we really haven't talked about dynamic size arrays so I guess that's what threw me off. I realized from the beginning I was missing something very basic, sorry for wasting time, I'll put even more time analyzing everything before posting next time.

12
  • 3
    You might use std::vector<int> A(dim); Commented Feb 24, 2022 at 17:55
  • 1
    Variable length arrays are not standard C++. int A[dim]; requires dim to be a compile time constant, not a runtime constant. Consider std::vector<int> instead. Commented Feb 24, 2022 at 17:56
  • 1
    If you can't use a resizable container like vector you'll have to use new to allocate memory. Be sure to delete it when you're done with it. Your course may be counting on you using a compiler that supports VLAs as an extension, but it is not a standard feature of C++. Commented Feb 24, 2022 at 18:10
  • 1
    @emetsipe -- I only need to set it from an user input. -- That is changing the length, The length is coming from the input at runtime, not compile-time. When the program starts, you don't know what length is going to be inputted, thus that is changing the length from something unknown to something known. Commented Feb 24, 2022 at 18:15
  • 1
    Also, do not make the mistake of giving in and using the non-standard variable length arrays. As others pointed out, VLA's are not C++. If your teacher were to take code with VLA's, and attempt to compile it using Visual C++, there will be compilation errors galore. Then you have to spend time making all of those changes to get rid of the VLA's. Just use std::vector -- I really don't see the correlation between an algorithms class, and a C++ language class. They are two separate entities -- you could be a whiz at algorithms, and be a nobody at C++, and vice-versa. Commented Feb 24, 2022 at 18:23

1 Answer 1

3

The size of an array variable must be compile time constant in C++. User input is not compile time constant, hence it cannot be used as the size of an array variable.

In order to create an array with runtime size, you must instead create a dynamic array. The simplest way to do that is to use std::vector from the standard library.

EDIT 2 Note: I'm looking for a solution that doesn't use std::vector.

It's possible to create a dynamic array without std::vector, but that requires the use and understanding of more advanced concepts. Using new expressions directly is more difficult, error prone and is something that isn't (or shouldn't) be done in most programs in practice.

Of course, another solution is to just not use user input but rather an array with constant size.

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

Comments