Could anyone please tell me how I can store any number of values into an array?
thanks
Could anyone please tell me how I can store any number of values into an array?
thanks
An array, when created, has a fixed size.
Try using a linked list, or a vector by #include-ing <vector>.
Yes, but for this assignment the user must be able to enter any number of values into an array, and we haven't covered linked lists or vectors yet.
Would my teacher really mean to store 128 elements in the array?
int myarray[128];
myarray[0] = 1;
If you're having trouble, post your code.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Or post the exact problem requirements. For better or worse, programming involves a precise understanding of the problem. For example, the problem may be to allow the user to enter as many integer numbers as they want up to a total of 128 integers or it may be to allow the user to enter as many integer numbers as they want without an upper limit. You can write code to handle either problem, but the code you use will be heavily dependent on which problem it is.
if you want to store an unknown number of values in an array you could dynamically allocate the array.
or just make an array big enough to hold a whole lot of values, so that the user will be unlikely to fill it all the way up.Code:cout << "How big will your array be?\n"; int x; cin >> x; int * array = new int[x]; //... delete [] array;
I came up with a cool phrase to put down here, but i forgot it...
^ some compilers complain becaue it's not a constant...
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
of course its not a constant... thats the whole point behind dynamic memory...
any compiler that complains about dynamically allocated arrays is not a compiler worth using
I came up with a cool phrase to put down here, but i forgot it...
bloodshed dev c++ V 4.8.9.0 complains... and it's worth using...
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
thats curious... because im looking at a fully compiled program written in Dev C++ that involves nothing but a single dynamic array...
is it possible that youve never used dynamic memory before and you simply dont know that it works and uses standard C++ keywords that work on any compiler?
I came up with a cool phrase to put down here, but i forgot it...
maybe it's just letting you know that you can't use 'x' and be certain it still contains the size of the array you allocated?
Your warning level might be at the highest setting? Seems like a weird warning to give, like ... said.
Thor's self help tip:
Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.
OS: Windows XP
Compiler: MSVC
nvm... I just tried it... I must be getting it confused with something else that I needed a const for... I cant' remember what it was now though...
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Probably you tried this:
int array[x];
That is what will choke your compiler, most likely
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.