My assignment is to ask the user what size array they want. The compiler keeps saying, constant expression required in function main().
code is:
cout<<"Enter size of array:"<<endl;
cin>>x;
int array1[x];
My assignment is to ask the user what size array they want. The compiler keeps saying, constant expression required in function main().
code is:
cout<<"Enter size of array:"<<endl;
cin>>x;
int array1[x];
I think this is not possible. You must define "char string[100];" and then you can enter how many arrays will you use.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
the only way for doing this is with dynamic memory allocation( I think).
A better way of doing it is with "new" and "delete"Code:char *string; cout<<"Enter size of array:"<<endl; cin>>x; string= (char *) malloc(sizeof(char)*x); ... ... ... free(string);//do not forget this, you have to free the memory
but I can't give you an example because I only use them 1 or 2.
look them up and use them instead of malloc
You can use realloc. That way, you just realloc after every input.
1978 Silver Anniversary Corvette
your compiler might register memory frees as an atexit command... so check...
hasafraggin shizigishin oppashigger...
new and delete are C++ operators. This is the C board. also in C you do not need to cast malloc(), in fact it is better if you don't.
Furthermore, if you have GCC or a C99 compatible compiler you can use you original code.
Arrays must know how long they are at compile-time. If you want to allocate a certain amount of memory based on run-time information, you'll have to use dynamic memory allocation...I am pretty sure this can be done with C++ arrays however, check their forum.Code:#include <stdlib.h> int main () { int N; int * a; printf("Enter size of array: "); scanf ("%d", &N); a = malloc (sizeof(int) * size); // Now a is an array of N ints. return 0; }
Callou collei we'll code the way
Of prime numbers and pings!