1

I have the following code:

#include <iostream>
using namespace std;

int *growArray (int* p_values, int cur_size)
{
    int *p_new_values = new int[ cur_size * 2 ];
    for ( int i = 0; i < cur_size; ++i )
    {
        p_new_values[ i ] = p_values[ i ];
    }
    delete p_values;
    return p_new_values;
}
int main ()
{
    int next_element = 0;
    int size = 10;
    int *p_values = new int[ size ];
    int val;
    cout << "Please enter a number: ";
    cin >> val;
    while ( val > 0 )
    {
        if ( size == next_element + 1 )
        {
            cout<< "Im in If"<<endl;
            p_values = growArray( p_values, size );
        }
        p_values[ next_element ] = val;
        cout << "Please enter a number (or 0 to exit): ";
        cin >> val;
    }

}

This code is allocating memory dynamiclly . I can understand everything in this code, exept one thing. What is the purpose of this if ? No matter how many inputs I give, program will never go in that if.

10
  • 1
    next_element is never changed inside the loop? Commented Dec 9, 2016 at 15:15
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line before asking on Stack Overflow. For more help, please read How to debug small programs (by Eric Lippert). At a minimum, you should [edit] your question to include a Minimal, Complete, and Verifiable example that reproduces your problem, along with the observations you made in the debugger. Commented Dec 9, 2016 at 15:15
  • I noticed this too... This code is from a book. I dont think its wrong. But i dont know.. Commented Dec 9, 2016 at 15:16
  • 1
    This code is definitely wrong multiple places Commented Dec 9, 2016 at 15:16
  • 1
    @IisousXristos yes books and compilers have mistakes and bugs, welcome to the real world. Unless this is exercise and author put that errors explicitly for you to fix, then he should mention that. Commented Dec 9, 2016 at 15:22

1 Answer 1

1

You never increase the value of the variable next_element that is initially is equal to 0

int next_element = 0;

Thus the expression in the if statement is not evaluated to true and the same first element is being overwritten

p_values[ next_element ] = val;

The program is in whole has a wrong logic. For example the if statement should look like

 if ( size == next_element )
      ^^^^^^^^^^^^^^^^^^^^
 {
     cout<< "Im in If"<<endl;
     p_values = growArray( p_values, size );
     size *= 2;
     ^^^^^^^^^
 }

And this statement should look like

p_values[ next_element++ ] = val;
                   ^^^^^

And the while loop should look like

cout << "Please enter a number: ";
while ( cin >> val && val > 0 )
{
    //...
    cout << "Please enter a number (or 0 to exit): ";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.