1

I am quite an experienced python programmer, attempting to learn C++. I am having an issue with initialising an integer array of a fixed size.

I have read this but creating my integer as a constant has not fixed my issue. What am i missing here?

BTW i am using VS2019 Community, any help would greatly be appreciated!

#include <iostream> 
#include <sstream> 

int numericStringLength(int input) {

    int length = 1;

    if (input > 0) {
        // we count how many times it can be divided by 10:
        // (how many times we can cut off the last digit until we end up with 0)
        for (length = 0; input > 0; length++) {
            input = input / 10;
        }
    }

    return length;

}

int convertNumericStringtoInt(std::string numericString) {

    std::stringstream data(numericString);
    int convertedData = 0;
    data >> convertedData;

    return convertedData;

}


int main() {

    std::string numericString;

    std::cout << "Enter the string: ";
    std::cin >> numericString;

    const int length = numericStringLength(convertNumericStringtoInt(numericString));

    std::cout << "Length of Numeric string: " << length << "\n";

    int storage[length];
}
RandomHash
  • 669
  • 6
  • 20
  • 1
    You do it with `std::array my_array`. – nada Sep 09 '19 at 11:55
  • 4
    Use a `std::vector` unless you have a good reason not to. – Bathsheba Sep 09 '19 at 11:55
  • @Bathsheba That is always correct, except when OP wants it to be fixed size. – nada Sep 09 '19 at 11:56
  • C++ doesn't support [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) as mentioned already. – Some programmer dude Sep 09 '19 at 11:57
  • @nada: I brushed over that, there's no good reason presented here as to why it needs to be a compile-time decided fixed size. – Bathsheba Sep 09 '19 at 11:57
  • 2
    @nada The problem is that the size isn't known at compile-time, which is required for `std::array`. – Some programmer dude Sep 09 '19 at 11:57
  • C++ doesn't have variable length arrays like C (and even when compiling C, MSVC doesn't support them). Use `std::vector` when you don't know an array size until run time. – Shawn Sep 09 '19 at 11:58
  • Considering that you use signed integers, that allows the user to enter negative numbers, which you don't handle. Either add support for it, or use unsigned integers. – Some programmer dude Sep 09 '19 at 12:02
  • Also, if you only allow non-negative numbers, and input as strings, the number of digits is equal to the length of the string. No need for the `numericStringLength` function. – Some programmer dude Sep 09 '19 at 12:04
  • @Someprogrammerdude: Although you need to be *very* careful with radix, decimal separators, thousand separators, scientific notation, leading positive sign. Far better just to count the digits as the OP does: the bottleneck will be in the IO. – Bathsheba Sep 09 '19 at 12:07
  • 2
    @Shawn Use the answer section when you have an answer – Lightness Races in Orbit Sep 09 '19 at 12:11
  • 2
  • I think there is some terminology clashing here. In C++, unlike Python, arrays do not change size after they are created. So in the context of C++, "fixed size array" (usually) does not mean an array whose size will not change after it is created -- that would be overly redundant. Rather, it (usually) means an array whose size is fixed at compile time (c.f. [tag:variable-length-array]). – JaMiT Sep 09 '19 at 13:04

4 Answers4

4

but creating my integer as a constant has not fixed my issue

It is not sufficient for the array length to be const. It must be compile time constant. const merely means that the object does not change throughout its lifetime - i.e. it implies runtime constness. Since length is not a compile time constant, the program is ill-formed. Examples of values that are compile time constant:

  • Literals such as 42
  • Template arguments
  • Enumerations
  • constexpr variables
  • const variables with compile time constant initialiser (this may have some limitations, I'm not sure)

It should be quite clear from the program that the length is calculated from user input, which would be impossible to do when the program is compiled. So since you cannot make it a compile time constant, you cannot use an array variable. You need to allocate the array dynamically. Simplest solution is to use a vector:

std::vector<int> storage(length);
eerorika
  • 232,697
  • 12
  • 197
  • 326
2

Making the variable const is not enough.

  • const just means "I won't change this after it's initialised".

It has to be a compile-time constant, as the machinery for a basic C array is baked into the computer instructions that form your executable.

  • You're calculating it at runtime, so there's no way that is going to work.

You are going to have to use a vector or some other such dynamically-resizable array type.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

If you want a fixed size array, you can use std::array, example:

std::array<int, 3> arr { 1,2,3 };
//              ^
//          fixed size needs to be known at compile time

If you don't know the size at compile time, use std::vector

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
1

The problem you have is that length is a run-time constant, whose value is calculated when the program is running. This is opposed to a compile-time constant, whose value is known by the compiler when you build your program.

Arrays needs compile-time constants for their size.

If the size is not known at compile-time but only at run-time, then you should use std::vector.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621