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
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.
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.
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.
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)
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.
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.
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.
#include <iostream>
#include <vector>
usingnamespace 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.