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.

emetsipe
  • 162
  • 1
  • 9
  • 3
    You might use `std::vector A(dim);` – Jarod42 Feb 24 '22 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` instead. – Retired Ninja Feb 24 '22 at 17:56
  • Indeed, unrelated to `round`, `x` is a runtime value, and so `dim`. – Jarod42 Feb 24 '22 at 17:57
  • @Jarod42 and @ retired-ninja The algorithm comes from my university course where we haven't talked about std::vector at all. That was the first fix i found online but I believe there's a way without using that. – emetsipe Feb 24 '22 at 18:05
  • @emetsipe *The algorithm comes from my university course where we haven't talked about std::vector at all* -- Algorithms are not computer languages. C++ does not have variable length arrays. C++ has `std::vector` as the "variable length array". – PaulMcKenzie Feb 24 '22 at 18:10
  • 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++. – Retired Ninja Feb 24 '22 at 18:10
  • @RetiredNinja ok I that's what I need to fix. Is there any other way to define a compile-time costant besides '#define', 'enum', and 'constexpr'? We haven't studied the last two, and I think '#define' is useless since I'm using 'cin', right? – emetsipe Feb 24 '22 at 18:12
  • @emetsipe: If you feel brave, just use ``. Using VLA's is not C++, and using C arrays is possible but not idiomatic, and if your university insists on it, they are not teaching you C++. IF you need to fall back to dynamic allocation, use smart pointers (``), not "naked" new / delete, as they cover the "make sure" part RetiredNinja was talking about. – DevSolar Feb 24 '22 at 18:13
  • @emetsipe -- You should tell us what you can or can't use. If someone were to post an answer, they may have wasted their time if the answer contains things you say you cannot use. – PaulMcKenzie Feb 24 '22 at 18:14
  • @PaulMcKenzie I don't need the lenght to change throughout the program, I only need to set it from an user input. – emetsipe Feb 24 '22 at 18:14
  • 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. – PaulMcKenzie Feb 24 '22 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. – PaulMcKenzie Feb 24 '22 at 18:23

1 Answers1

4

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326