Home > front end >  How to replace vector elements using a function that asks for user input (C )
How to replace vector elements using a function that asks for user input (C )

Time:10-26

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> 

CodePudding user response:

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)

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related