Dynamic array storing valuesCould not deduce template argument for T[]Why is processing a sorted array faster than processing an unsorted array?Dynamic Array C++ (Deletion)Initializing std::array inside a functionFirst item in an array is getting… replaced?A simple vector, how can I reassign the address for a new array correctlyInitialize a constexpr std::array with the size of an n-dimensional std::arrayReinitialising an array on if statementVarious errors out of nowhere, f.e. 'string' does not name a type even with <string> and std declaredC++ I need to be able to resize my dynamic array
How many lines of code does the original TeX contain?
Can $! cause race conditions when used in scripts running in parallel?
Does Yeshayahu 43:10b / 43:13a imply HaShem was created?
Can you board the plane when your passport is valid less than 3 months?
Why is strlen so complex in C?
Who was the most successful German spy against Great Britain in WWII, from the contemporary German perspective?
Joining lists with same elements
Why is the UK so keen to remove the "backstop" when their leadership seems to think that no border will be needed in Northern Ireland?
Is it legal for source code containing undefined behavior to crash the compiler?
Is one hour layover sufficient at Singapore Changi Airport (Indian citizen travelling from US to India)?
Redacting URLs as an email-phishing preventative?
Movie where people enter a church but find they can't leave, not in English
LINQ for generating all possible permutations
Why did my folder names end up like this, and how can I fix this using a script?
Is first Ubuntu user root?
Cooking Scrambled Eggs
Does EU 261/2004 compensation apply if delayed by the border check?
Expanding powers of expressions of the form ax+b
What to look for in a spotting scope?
Boot Windows from SAN
How does encoder decoder network works?
Why is "-ber" the suffix of the last four months of the year?
How do we tell which part of kinetic energy gives rise to temperature?
If the Shillelagh cantrip is applied to a club with non-standard damage dice, what is the resulting damage dice?
Dynamic array storing values
Could not deduce template argument for T[]Why is processing a sorted array faster than processing an unsorted array?Dynamic Array C++ (Deletion)Initializing std::array inside a functionFirst item in an array is getting… replaced?A simple vector, how can I reassign the address for a new array correctlyInitialize a constexpr std::array with the size of an n-dimensional std::arrayReinitialising an array on if statementVarious errors out of nowhere, f.e. 'string' does not name a type even with <string> and std declaredC++ I need to be able to resize my dynamic array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I resized my array with a function, however it seems like it does not store any array value I entered.
basically I put -1, the loop stops and then supposed to show the elements in array. but it does not show anything.
The output does not show anything after cout.
#include <iostream>
using namespace std;
void resize(int *&arr, int &size)
int tempsize=size;
size=size+10;
int *temp= new int [size];
for(int i=0; i<tempsize;i++)
temp[i]=arr[i];
delete [] arr;
arr=temp;
int main()
int size=0;
int capacity =10;
int *p=new int[capacity];
int check=0;
int input;
cout<<"Please enter the number in array and input -1 to end it.";
while(check!=-1)
cin>>input;
if(input==-1)
check=-1;
else
if(size==capacity)
resize(p,capacity);
p[size]=input;
size++;
cout<<"Show me the numbers in array: ";
for(int i=0; i<size;i++)
cout<<p[i]<<" ";
cout<<endl;
delete [] p;
return 0;
c++
add a comment |
I resized my array with a function, however it seems like it does not store any array value I entered.
basically I put -1, the loop stops and then supposed to show the elements in array. but it does not show anything.
The output does not show anything after cout.
#include <iostream>
using namespace std;
void resize(int *&arr, int &size)
int tempsize=size;
size=size+10;
int *temp= new int [size];
for(int i=0; i<tempsize;i++)
temp[i]=arr[i];
delete [] arr;
arr=temp;
int main()
int size=0;
int capacity =10;
int *p=new int[capacity];
int check=0;
int input;
cout<<"Please enter the number in array and input -1 to end it.";
while(check!=-1)
cin>>input;
if(input==-1)
check=-1;
else
if(size==capacity)
resize(p,capacity);
p[size]=input;
size++;
cout<<"Show me the numbers in array: ";
for(int i=0; i<size;i++)
cout<<p[i]<<" ";
cout<<endl;
delete [] p;
return 0;
c++
1
Do you really have to work with raw arrays? This would be much easier to achieve with STL.
– Sceptical Jule
Mar 27 at 19:49
@ScepticalJule telling raw beginners to work with the stl is hardly helpful. if he doesn't understand the basics of arrays, he's been learning for about 10 minutes. This is clearly a learning exercise, and no way is he ready to learn the stl.
– Joseph Larson
Mar 27 at 19:59
add a comment |
I resized my array with a function, however it seems like it does not store any array value I entered.
basically I put -1, the loop stops and then supposed to show the elements in array. but it does not show anything.
The output does not show anything after cout.
#include <iostream>
using namespace std;
void resize(int *&arr, int &size)
int tempsize=size;
size=size+10;
int *temp= new int [size];
for(int i=0; i<tempsize;i++)
temp[i]=arr[i];
delete [] arr;
arr=temp;
int main()
int size=0;
int capacity =10;
int *p=new int[capacity];
int check=0;
int input;
cout<<"Please enter the number in array and input -1 to end it.";
while(check!=-1)
cin>>input;
if(input==-1)
check=-1;
else
if(size==capacity)
resize(p,capacity);
p[size]=input;
size++;
cout<<"Show me the numbers in array: ";
for(int i=0; i<size;i++)
cout<<p[i]<<" ";
cout<<endl;
delete [] p;
return 0;
c++
I resized my array with a function, however it seems like it does not store any array value I entered.
basically I put -1, the loop stops and then supposed to show the elements in array. but it does not show anything.
The output does not show anything after cout.
#include <iostream>
using namespace std;
void resize(int *&arr, int &size)
int tempsize=size;
size=size+10;
int *temp= new int [size];
for(int i=0; i<tempsize;i++)
temp[i]=arr[i];
delete [] arr;
arr=temp;
int main()
int size=0;
int capacity =10;
int *p=new int[capacity];
int check=0;
int input;
cout<<"Please enter the number in array and input -1 to end it.";
while(check!=-1)
cin>>input;
if(input==-1)
check=-1;
else
if(size==capacity)
resize(p,capacity);
p[size]=input;
size++;
cout<<"Show me the numbers in array: ";
for(int i=0; i<size;i++)
cout<<p[i]<<" ";
cout<<endl;
delete [] p;
return 0;
c++
c++
asked Mar 27 at 19:41
gLeegLee
203 bronze badges
203 bronze badges
1
Do you really have to work with raw arrays? This would be much easier to achieve with STL.
– Sceptical Jule
Mar 27 at 19:49
@ScepticalJule telling raw beginners to work with the stl is hardly helpful. if he doesn't understand the basics of arrays, he's been learning for about 10 minutes. This is clearly a learning exercise, and no way is he ready to learn the stl.
– Joseph Larson
Mar 27 at 19:59
add a comment |
1
Do you really have to work with raw arrays? This would be much easier to achieve with STL.
– Sceptical Jule
Mar 27 at 19:49
@ScepticalJule telling raw beginners to work with the stl is hardly helpful. if he doesn't understand the basics of arrays, he's been learning for about 10 minutes. This is clearly a learning exercise, and no way is he ready to learn the stl.
– Joseph Larson
Mar 27 at 19:59
1
1
Do you really have to work with raw arrays? This would be much easier to achieve with STL.
– Sceptical Jule
Mar 27 at 19:49
Do you really have to work with raw arrays? This would be much easier to achieve with STL.
– Sceptical Jule
Mar 27 at 19:49
@ScepticalJule telling raw beginners to work with the stl is hardly helpful. if he doesn't understand the basics of arrays, he's been learning for about 10 minutes. This is clearly a learning exercise, and no way is he ready to learn the stl.
– Joseph Larson
Mar 27 at 19:59
@ScepticalJule telling raw beginners to work with the stl is hardly helpful. if he doesn't understand the basics of arrays, he's been learning for about 10 minutes. This is clearly a learning exercise, and no way is he ready to learn the stl.
– Joseph Larson
Mar 27 at 19:59
add a comment |
1 Answer
1
active
oldest
votes
Your if statement whether you need to resize the array is too encompassing. The should contain just the resize, then outside the is when you should store into the array.
Instead, size is 0, which != capacity, so you don't do anything.
1
This answer it too cryptographic to me
– Amadeus
Mar 27 at 19:52
@Amadeus cryptographic? Perhaps you mean confusing? Look at his if(size == capacity) code block. He only adds to the array if a resize is required.
– Joseph Larson
Mar 27 at 19:58
I'm sorry, this was a kind of sarcasm to say that your answer is too dificult to undersand. It would be easier if you shows in code what do you really means or want to say in words
– Amadeus
Mar 27 at 20:08
It worked out Joseph, i put input and size++ right below cin>>input;
– gLee
Mar 27 at 20:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385281%2fdynamic-array-storing-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your if statement whether you need to resize the array is too encompassing. The should contain just the resize, then outside the is when you should store into the array.
Instead, size is 0, which != capacity, so you don't do anything.
1
This answer it too cryptographic to me
– Amadeus
Mar 27 at 19:52
@Amadeus cryptographic? Perhaps you mean confusing? Look at his if(size == capacity) code block. He only adds to the array if a resize is required.
– Joseph Larson
Mar 27 at 19:58
I'm sorry, this was a kind of sarcasm to say that your answer is too dificult to undersand. It would be easier if you shows in code what do you really means or want to say in words
– Amadeus
Mar 27 at 20:08
It worked out Joseph, i put input and size++ right below cin>>input;
– gLee
Mar 27 at 20:15
add a comment |
Your if statement whether you need to resize the array is too encompassing. The should contain just the resize, then outside the is when you should store into the array.
Instead, size is 0, which != capacity, so you don't do anything.
1
This answer it too cryptographic to me
– Amadeus
Mar 27 at 19:52
@Amadeus cryptographic? Perhaps you mean confusing? Look at his if(size == capacity) code block. He only adds to the array if a resize is required.
– Joseph Larson
Mar 27 at 19:58
I'm sorry, this was a kind of sarcasm to say that your answer is too dificult to undersand. It would be easier if you shows in code what do you really means or want to say in words
– Amadeus
Mar 27 at 20:08
It worked out Joseph, i put input and size++ right below cin>>input;
– gLee
Mar 27 at 20:15
add a comment |
Your if statement whether you need to resize the array is too encompassing. The should contain just the resize, then outside the is when you should store into the array.
Instead, size is 0, which != capacity, so you don't do anything.
Your if statement whether you need to resize the array is too encompassing. The should contain just the resize, then outside the is when you should store into the array.
Instead, size is 0, which != capacity, so you don't do anything.
answered Mar 27 at 19:49
Joseph LarsonJoseph Larson
1,32710 silver badges13 bronze badges
1,32710 silver badges13 bronze badges
1
This answer it too cryptographic to me
– Amadeus
Mar 27 at 19:52
@Amadeus cryptographic? Perhaps you mean confusing? Look at his if(size == capacity) code block. He only adds to the array if a resize is required.
– Joseph Larson
Mar 27 at 19:58
I'm sorry, this was a kind of sarcasm to say that your answer is too dificult to undersand. It would be easier if you shows in code what do you really means or want to say in words
– Amadeus
Mar 27 at 20:08
It worked out Joseph, i put input and size++ right below cin>>input;
– gLee
Mar 27 at 20:15
add a comment |
1
This answer it too cryptographic to me
– Amadeus
Mar 27 at 19:52
@Amadeus cryptographic? Perhaps you mean confusing? Look at his if(size == capacity) code block. He only adds to the array if a resize is required.
– Joseph Larson
Mar 27 at 19:58
I'm sorry, this was a kind of sarcasm to say that your answer is too dificult to undersand. It would be easier if you shows in code what do you really means or want to say in words
– Amadeus
Mar 27 at 20:08
It worked out Joseph, i put input and size++ right below cin>>input;
– gLee
Mar 27 at 20:15
1
1
This answer it too cryptographic to me
– Amadeus
Mar 27 at 19:52
This answer it too cryptographic to me
– Amadeus
Mar 27 at 19:52
@Amadeus cryptographic? Perhaps you mean confusing? Look at his if(size == capacity) code block. He only adds to the array if a resize is required.
– Joseph Larson
Mar 27 at 19:58
@Amadeus cryptographic? Perhaps you mean confusing? Look at his if(size == capacity) code block. He only adds to the array if a resize is required.
– Joseph Larson
Mar 27 at 19:58
I'm sorry, this was a kind of sarcasm to say that your answer is too dificult to undersand. It would be easier if you shows in code what do you really means or want to say in words
– Amadeus
Mar 27 at 20:08
I'm sorry, this was a kind of sarcasm to say that your answer is too dificult to undersand. It would be easier if you shows in code what do you really means or want to say in words
– Amadeus
Mar 27 at 20:08
It worked out Joseph, i put input and size++ right below cin>>input;
– gLee
Mar 27 at 20:15
It worked out Joseph, i put input and size++ right below cin>>input;
– gLee
Mar 27 at 20:15
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385281%2fdynamic-array-storing-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Do you really have to work with raw arrays? This would be much easier to achieve with STL.
– Sceptical Jule
Mar 27 at 19:49
@ScepticalJule telling raw beginners to work with the stl is hardly helpful. if he doesn't understand the basics of arrays, he's been learning for about 10 minutes. This is clearly a learning exercise, and no way is he ready to learn the stl.
– Joseph Larson
Mar 27 at 19:59