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.

alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
  • 1
    `next_element` is never changed inside the loop? – πάντα ῥεῖ Dec 09 '16 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)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Dec 09 '16 at 15:15
  • I noticed this too... This code is from a book. I dont think its wrong. But i dont know.. – Iisous Xristos Dec 09 '16 at 15:16
  • Consider asking the author of this code. We're not oracles. – alexeykuzmin0 Dec 09 '16 at 15:16
  • 1
    This code is definitely wrong multiple places – Slava Dec 09 '16 at 15:16
  • @IisousXristos Throw that book in the bin and get a [better one](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – πάντα ῥεῖ Dec 09 '16 at 15:17
  • Its from this book : CProgramming.Jumping.Into.C.Plus.Plus in page 150 ... – Iisous Xristos Dec 09 '16 at 15:17
  • If you are a C programmer then problems should be pretty obvious for you. If you are not then it is a wrong book to learn C++ from beginning. – Slava Dec 09 '16 at 15:19
  • I am experienced with programming. I could see the problem with the next_element variable , but It is copy pasted from a book .. How can it be wrong ? – Iisous Xristos Dec 09 '16 at 15:21
  • 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. – Slava Dec 09 '16 at 15:22

1 Answers1

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): ";
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335