1

I have this problem where I can't replace elements for 'numVect' through a function 'InputNumbers()' using user input. I tried to create a new vector in the function, and then define it as 'numVect' but no success.

#include <iostream>
#include <vector>
using namespace std;

vector <int> InputNumbers(vector <int> numVect) {
    vector <int> numVect2 = {};
    cout << "Input 5 numbers into a vector: " << endl;
    for (int i = 0; i < 5; i++) {
        cin >> numVect2[i];
    }
    return numVect2;
}

// void VectorRow();
// void VectorStack();
// void VectorTwoStacks();

void OutputNumbers(vector <int> numVect) {
    cout << "Vector: " << endl;
    for (auto i : numVect) {
        cout << i << " ";
    }
    cout << endl;
}

int main() {
    vector <int> numVect = {4, 5, 6, 7, 8};
    enum menu {Input = 1, Row = 2, Stack = 3, 
               Twostacks = 4, Numbers = 5};
    int choice;
    do {
        cout << "1. Input numbers\n";
        cout << "2. Row\n";
        cout << "3. Stack\n";
        cout << "4. Two stacks\n";
        cout << "5. Output numbers\n";
        cout << "6. End\n";
        cout << "--------------------------\n";
        cout << "Choice: "; cin >> choice; cout << endl;

        switch (choice) {
            case Input: InputNumbers(numVect); break;
            // case Row: VectorRow(); break;
            // case Stack: VectorStack(); break;
            // case Twostacks: VectorTwoStacks(); break;
            case Numbers: OutputNumbers(numVect); break;
        }

    } while (choice != 6);
    return 0;
}

Also, when I run the function 'InputNumbers()', it stops the terminal after user inputs the [0] element for 'numVect2'

Input 5 numbers into a vector:
1
PS C:\Users\leval\Desktop\majasdarbi\praktiskais_darbs2> 2
2
PS C:\Users\leval\Desktop\majasdarbi\praktiskais_darbs2> 
  • 1
    Change `cin >> numVect2[i];` to `cin >> numVect2.at(i);` so the vector can throw an exception if it accesses past the end of the vector. – Eljay Oct 25 '22 at 11:42
  • Does this answer your question? [Difference between value parameter and reference parameter?](https://stackoverflow.com/questions/2207179/difference-between-value-parameter-and-reference-parameter) – eike Oct 25 '22 at 12:18
  • @eike not really, but somehow figured it out myself. Changed the code so that the vector numVect doesn't need to be passed to InputNumbers(). – levaldssandis Oct 25 '22 at 12:45
  • @levaldssandis It really does, though. In C++, you have to pass parameters by reference if you want your changes to be reflected in the original object. You have to learn these basic concepts to be able to write C++ code. I recommend taking the [tour](https://isocpp.org/tour). – eike Oct 25 '22 at 12:54
  • You need to pass the vector by reference instead of copying it by value all the time. – Andrej Podzimek Oct 25 '22 at 15:18

2 Answers2

1

In InputNumbers(), you are invoking operator[] on an empty vector, which is undefined behavior. Also, you are ignoring the input vector, so you may as well remove that parameter.

You need to instead either:

  • use vector::push_back():
vector <int> InputNumbers() {
    vector <int> numVect;
    cout << "Input 5 numbers into a vector: " << endl;
    for (int i = 0; i < 5; i++) {
        int temp;
        cin >> temp;
        numVect.push_back(temp);
    }
    return numVect;
}

...

numVect = InputNumbers();
  • use the vector's constructor or vector::resize() method to pre-allocate the vector's internal array so you can then use its operator[] properly:
vector <int> InputNumbers() {
    vector <int> numVect(5);
    /* or:
    vector <int> numVect;
    numVect.resize(5);
    */
    cout << "Input 5 numbers into a vector: " << endl;
    for (int i = 0; i < 5; i++) {
        cin >> numVect[i];
    }
    return numVect;
}

...

numVect = InputNumbers();

If you really want to pass in the target vector as a parameter, you need to pass it in by reference instead of by value, eg:

void InputNumbers(vector <int> &numVect) {
    // manipulate numVect as needed...
}

...

InputNumbers(numVect);

Either way, you should be passing the vector to OutputNumbers() by const reference, not by value, eg:

void OutputNumbers(const vector <int> &numVect)
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

You're passing a vector by value to InputNumbers. So, InputNumbers has its own local copy. Any changes made to it do not affect the vector in main. If you pass the vector by reference to InputNumbers then InputNumbers can change it. Take out numVect2 and give this a try.

numVect2 is a vector with no elements and you try to write to it. This is bound to go badly.

Ian
  • 572
  • 1
  • 4
  • 17