1

So I have this code, but it is not outputting, after adding 4th value, the right stuff, it's like it all gets deleted and it is not added until the next run

#include <iostream>

using namespace std;

const int DEFAULT_CAPACITY = 2;

void addValue(int data[], int& logSize, int& physSize, int newValue)
{
    // DATA DICTIONARY
    int *temp;

    if (logSize == physSize)
    {
        physSize *= 2;
        temp = new int[physSize];

        for (int i = 0; i <= logSize; i++)
        {
            temp[i] = data[i];
            cout<<temp[i]<<endl;
        }

        delete [] data;
        data = temp;
    }

    data[logSize] = newValue;

    for (int i = 0; i <= logSize; i++)
    {
        cout<<data[i]<<endl;
    }

    logSize++;
}

void printData(int data[], int logSize)
{
    cout<<endl;
    cout<<"ARRAY DATA:"<<endl;

    for (int i = 0; i < logSize; i++)
    {
        cout<<data[i]<<endl;
    }
}

void main()
{
    //DATA DICTIONARY
    int *data;
    int logSize;
    int physSize;
    int newValue;
    char choice;

    physSize = DEFAULT_CAPACITY;
    logSize = 0;
    data = new int[physSize];

    do
    {
        cout<<"What would you like to do?"<<endl;
        cout<<"(A)dd value to array"<<endl;
        cout<<"(D)isplay all values"<<endl;
        cout<<"(Q)uit"<<endl;

        cin>>choice;

        if (choice == 'A' || choice == 'a')
        {
            cout<<"What integer do you want to add? ";
            cin>>newValue;
            addValue(data, logSize, physSize, newValue);
        }

        if (choice == 'D' || choice == 'd')
        {
            printData(data, logSize);
        }

        cout<<endl;

    } while (choice != 'Q' && choice != 'q');
}
5
  • Use std::vector. C++ argument passing is often pass by value unless you explicitly use references (and you can't have references to arrays) Commented Jan 31, 2014 at 6:28
  • @BasileStarynkevitch - this is for an assignment, cannot use vectors on it. Commented Jan 31, 2014 at 6:28
  • What exactly is the question here? Can you be a little more specific about what's going on? Provide basic output etc... Commented Jan 31, 2014 at 6:28
  • Essentially: what is wrong with my code? When I run it, I can add the first 3 values fine, but then, it outputs gibberish as if the array is null/empty Commented Jan 31, 2014 at 6:30
  • Compile with all warnings & debug info (g++ -Wall -g). Learn to use a debugger (gdb). Commented Jan 31, 2014 at 6:30

2 Answers 2

3

The fact that you could and should use an std::vector<int> aside, data is being passed as a pointer by value here:

void addValue(int data[], int& logSize, int& physSize, int newValue)

(for int data[], read int* data). So `addValue has its own copy of the pointer, and whatever it does with it has no effect on the caller side. You can fix this particular problem by passing the pointer by reference:

void addValue(int*& data, int& logSize, int& physSize, int newValue)
Sign up to request clarification or add additional context in comments.

1 Comment

Right on! Worked perfectly, thanks. Guess I need to review this whole operation of how pointers and stuff work.
1

Pass the data by reference. You are passing it by value.

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.