•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,850 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,305 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1416 | Replies: 6 | Solved
![]() |
i know i learned it somewhere, but its been a while since i used it... how do you make an user given array size... i mean...
i know this is not hte way of doing it... but just so you get the idea of what i need to do...
int b; cout<<"Input the array size: "; cin>>b; int array[b];
i know this is not hte way of doing it... but just so you get the idea of what i need to do...
-->sometimes i wanna take my toaster in a bath<-- int *array = new int[b];Or if you can, use a
std::vector If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
•
•
i know i learned it somewhere, but its been a while since i used it... how do you make an user given array size... i mean...int b; cout<<"Input the array size: "; cin>>b; int array[b];
i know this is not hte way of doing it... but just so you get the idea of what i need to do...
arrays have to be constant
maybe:
const int size = b;
int array[size];
Of coarse, once it's done, you can't re-size it.
why not use a vector?
•
•
•
•
arrays have to be constant
maybe:
const int size = b;
int array[size];
Of coarse, once it's done, you can't re-size it.
why not use a vector?
did you try it? 'cuz when i did that, it sent me the error
Error 13: Constant expression requiredi don't use a vector because it is not what i need...
-->sometimes i wanna take my toaster in a bath<-- •
•
•
•
did you try it? 'cuz when i did that, it sent me the errorError 13: Constant expression required
i don't use a vector because it is not what i need...
I just looked it up. Arrays cannot be intialized with a non-const value or a value unknown at compile time. That's why it failed.
You will have to initailize it with the largest possible size required.
Perhaps use a pointer with a limlit set in a loop by a user determined variable might be the way to go?
I don't know WHY you need to do thsi, so it's hard to guess what other methods might be appropriate.
Good luck!
•
•
•
•
I don't know WHY you need to do thsi, so it's hard to guess what other methods might be appropriate.
Allowing such behaviour inevitably invites beginners to try such things as the following, which, if it weren't illegal, would be undefined behaviour
CPP Syntax (Toggle Plain Text)
int main() { int size(5); int arr[size] = { 1,2,3,4,5 }; size = 8; arr[5] = 6; }
There may be other reasons the ISO standards committee decided to disallow it, but this seems reason enough to me.
Last edited by Bench : Jul 7th, 2007 at 1:24 pm.
¿umop apisdn upside down? ![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- To arrange Array Members in ascending order ! (Java)
- im wonderin whats wrong in my work (C++)
- User define 2D array (C++)
- string array size (C++)
- works for static need help to make it dynamic (C++)
Other Threads in the C++ Forum
- Previous Thread: isPrime()
- Next Thread: Strange linking error at run-time



Linear Mode