I need to create a new array of size N, as specified by the user. I'm using c++.
first take the size from the user then assign this size to array , like cin >> size; int arr[size]; if it is helpful for you let me know.
It gives me the following errors: Expected constant expression. Cannot allocate an array of constant size zero. 'numbers' unknown size. Where "numbers" is the name of the array. I also used #define input cin >> at the beginning so i can just use "input size;" Dont think that would make a difference.
yes in c++ you can't assign 0 size to an array size must be greater than 0
yes, thats just from the previous error. The problem is the first error.
Should i post my code?
yes
#include <iostream> using namespace std; #define print cout << #define input cin >> int main() { int N; print "How many integers?\n"; input N; int numbers[N]; for(int i; i < N; i++) { int n; print "Number " << numbers << ": "; input n; numbers[i] = n; } } Forgot what i was doing in the for loop, oddly enough, but the problem is before that. The red underline is under the 'N' in the array declarator. It says, "expression must have a constant value".
you want to print your arrays values ???? numbers ????
i was in the middle of working on that loop when i decided to ask the community.
I dont think the for loop has anything to do with the declaration of the array.
yes
http://cboard.cprogramming.com/cplusplus-programming/82221-user-input-array-size.html
it will help you , use number=new number[size]; then tell me
I realize what it was: for(int i; i < N; i++) { int n; print "Number " << numbers[i] << ":"; input n; numbers[i] = n; }
now you are printing it correctly , tell me size issue has resolved or not?
syntax error. need to put tpe specifier?
found something in that link you sent. let me check.
ok
Thanks you very much! the answer was in the third response in the link.
there is someone in need of a wizard with c++. scroll down in the computer science category for Wiisper.
actually arrays takes the constant size , it is compiler dependent , cz when i used to declare arrays like this , it worked well , i never used new for this kind of initializing
Different compilers. im using microsoft visual studio 2010. i really need to learn pointers. Anyway, someone else needs more help.
Join our real-time social learning platform and learn together with your friends!