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;








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;










share|improve this question



















  • 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

















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;










share|improve this question



















  • 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













0












0








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;










share|improve this question














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++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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












1 Answer
1






active

oldest

votes


















0















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.






share|improve this answer




















  • 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










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
);



);













draft saved

draft discarded


















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









0















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.






share|improve this answer




















  • 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















0















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.






share|improve this answer




















  • 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













0














0










0









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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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








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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?