Help with arrays and input.

I'm a c++ beginner and I need to know how can I create an array like example[x];, in which x is a number typed by the user (cin>>x;).

Is there any way I can do something like that?
The easiest way would be to use a std::vector instead of the array.

But if you must use the array you will need to dynamically allocate the memory with new/delete.
Sorry mate, I have no idea what does std::vector mean, you mind defining it?

Ty for your help though.
Yeah, all you gotta do is declare a variable(say size), accept this from the user with cin>>size;
Then validate this input and make sure its not less than or equal to 0. Immediately you may say
1
2
3
while (size<=0)
cin>>size;
array[size];
Matri X, it doesn't work like that, I keep getting the "size" after "array" underlined in red ( I use visual c++ express ) and a Error appears: expression must have a constant value.

I would have to make "size" a constant but then I wouldn't be able to input its value via cin>>size;

I DON'T KNOW WHAT TO DO.
Like jlb said: this sure isn't the best method but if you still gotta do it that way, then you may have
1
2
3
4
5
6
int size;
cin>>size;
while(size<=0)
cin>>size;
const int new_size=size;
int array[new_size];
Sorry mate, I have no idea what does std::vector mean, you mind defining it?

Did you even try to Google "std::vector"? How about "C++ dynamic memory"?

In C++ statically allocated arrays must use a const int for their size. The other options are vectors and dynamically allocated arrays. If you don't know what either of these methods are I suggest you abandon the thought of user supplied arrays sizes and just make your arrays large enough and insist that your user enters a value for their size that is equal to or less than that size.

Matri X, I obviously tried that too, doesn't work... It should, but it doesn't, it only works if I change "size" on the 5th line for a actual number, but then we're back to the original issue. Any other ideas??

You said it wasn't the best method, Is there any way I can do it without using arrays?? I need x integers to be created and I need x to be given by the user.
closed account (3qX21hU5)
The easiest way to do it would be to use vectors like jlb said, just go into the search up top on the forum and search for vectors and it will give you a tutorial on them.


EDIT: Deleted this since it was bad information

But again I would highly highly suggest you use vectors instead because the code I just posted is very sloppy and usually you will want to avoid it.

Last edited on
For example

Why then does my compiler generate the following error messages with your code?

main.cpp||In function ‘int main()’:|
main.cpp|17|error: ISO C++ forbids variable length array ‘myArray’ [-Wvla]|
||=== Build finished: 1 errors, 0 warnings ===|


In C++ array sizes must be compile time constants, your "const" is not a compile time constant.

Last edited on
closed account (3qX21hU5)
Visual Studio Compiler? Like I said
Most compilers will require you to have a constant literal or constant variable when initializing arrays but some don't.
.

The code I posted compiles and runs with CodeBlocks but not VS or most other compilers. I agree it is very very sloppy and won't compiler on most compilers that is why I put you should avoid arrays for this and use vectors instead.

I just wanted to stress that if the OP was going to use Matrix's idea and his compiler allowed it he should at least use a const variable (Worst solution) or use a vector (Best solution)
Last edited on
Tuuliof, according to the error message I keep getting(even after trying my own method), there isn't any way of treating arrays the way you want it to, therefore, you need to learn STL::Vectors asap.
Visual Studio Compiler?

No g++ on Linux with all gcc specific extensions disabled. The gcc compiler does have many hacks that, in my opinion shouldn't be used by novices.

The C++ standard specifically forbids VLA, all statically allocated arrays must use compile time constants for their sizes to be in conformance to the standard.

Thank you very much for you help Zereo.
closed account (3qX21hU5)
Please do not use the example above tuuliof because it was bad information and might get you into trouble if you didn't understand what was going on and why.

I would highly suggest vectors for your solution. Here is a example of how vectors can work. They are similiar to arrays in a lot of ways. But they also add a huge amount of functionality.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    // Vector is like array except you don't need to define a size
    vector<int> numbers;
    
    cout << "How many number do you want to input?: ";
    int inputSize;
    cin >> inputSize;
    
    // Runs however many times the user entered
    for (unsigned i = 0; i != inputSize; ++i)
    {
        unsigned userNumber;
        cout << "Enter a number: ";
        cin >> userNumber;
        
        // Puts whatever number the user entered into the vector
        numbers.push_back(userNumber);
    }
    
    // Prints all the numbers in the vector
    for (vector<int>::size_type i = 0; i != numbers.size(); ++i)
        cout << numbers[i] << endl;
    
    return 0;
}
I'm taking a look on some tutorials on vectors, Zereo, I didn't even tried to understand your code before doing it, but now I'm starting to get it, this is so very useful, thak you very much for your attention and tips.
However, I found out its still possible to implement what tuuliof wanted. I was just scrapping through some of the articles on this site and look what I found http://cplusplus.com/articles/iz3hAqkS/ under the subtopic Variable Length Arrays. You might wanna take a quick look at it.
Matri X wrote:
under the subtopic Variable Length Arrays.

You'll notice the article you referenced is about C99, not C++. VLA's are illegal in C++.
Topic archived. No new replies allowed.