Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

I need to create a new array of size N, as specified by the user. I'm using c++.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

yes in c++ you can't assign 0 size to an array size must be greater than 0

OpenStudy (anonymous):

yes, thats just from the previous error. The problem is the first error.

OpenStudy (anonymous):

Should i post my code?

OpenStudy (anonymous):

yes

OpenStudy (anonymous):

#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".

OpenStudy (anonymous):

you want to print your arrays values ???? numbers ????

OpenStudy (anonymous):

i was in the middle of working on that loop when i decided to ask the community.

OpenStudy (anonymous):

I dont think the for loop has anything to do with the declaration of the array.

OpenStudy (anonymous):

yes

OpenStudy (anonymous):

it will help you , use number=new number[size]; then tell me

OpenStudy (anonymous):

I realize what it was: for(int i; i < N; i++) { int n; print "Number " << numbers[i] << ":"; input n; numbers[i] = n; }

OpenStudy (anonymous):

now you are printing it correctly , tell me size issue has resolved or not?

OpenStudy (anonymous):

syntax error. need to put tpe specifier?

OpenStudy (anonymous):

found something in that link you sent. let me check.

OpenStudy (anonymous):

ok

OpenStudy (anonymous):

Thanks you very much! the answer was in the third response in the link.

OpenStudy (anonymous):

there is someone in need of a wizard with c++. scroll down in the computer science category for Wiisper.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

Different compilers. im using microsoft visual studio 2010. i really need to learn pointers. Anyway, someone else needs more help.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!