0

I'm creating a program in which the user input data in a vector and then do different things by inputting certain characters. Now, I'm trying to create this program using functions to learn about creating functions:

#include <iostream>
#include <vector>
using namespace std;
void calculateMean(vector<int> []); //Display mean of numbers, M
void addNumber(vector<int> [], int); //Add a number, A
void smallestNumber(vector<int> [], int); //Display smallest number, S
void largestNumber(vector<int> [], int); //Display largest number, L
void printNumbers(vector<int> []); //Print numbers, P

void printNumbers(vector<int> list ) {
    if (list.size() == 0) {
        cout << "[] - the list is empty.";
    }
    else {
        for (auto val: list) {
            cout << val << " ";
        }
    }
    cout << endl << "------------" << endl;
}
void addNumber(vector<int> list ) {
    int num {};
    cout << "Please enter an integer:";
    cin >> num;
    list.push_back(num);
}

int main() {
    vector<int> data {};
    char selection {};
    do {
    cout << "P. print numbers" << endl;
    cout << "A. Add a number" << endl;
    cout << "M. display mean of numbers" << endl;
    cout << "S. display smallest number" << endl;
    cout << "L. display largest number" << endl;
    cout << "Q. quit program." << endl;
    cin >> selection;

    if (selection == 'P') {
        printNumbers(data);
    }

    else if (selection == 'A') {
        addNumber(data);
    }




    } while (selection != 'Q');

    return 0;
}

However, when I tried adding a number, the vector doesn't add the number into the vector data:

P. print numbers
A. Add a number
M. display mean of numbers
S. display smallest number
L. display largest number
Q. quit program.
P
[] - the list is empty.
------------
P. print numbers
A. Add a number
M. display mean of numbers
S. display smallest number
L. display largest number
Q. quit program.
A
Please enter an integer:123
P. print numbers
A. Add a number
M. display mean of numbers
S. display smallest number
L. display largest number
Q. quit program.
P
[] - the list is empty.
------------
P. print numbers
A. Add a number
M. display mean of numbers
S. display smallest number
L. display largest number
Q. quit program.

Is it because the function addNumbers() just runs a copy of the vector itself? Should I use pass by reference to debug it?

  • Are you familiar with the difference between passing by value and passing by reference? You are passing the vector by value, and thus operating on a copy not the original. `void addNumber(vector &list)` will pass by reference and allow you to manipulate the original vector rather than the copy. – user4581301 Jul 10 '22 at 06:38
  • Some reading on the topic: https://stackoverflow.com/questions/21215409/does-c-pass-objects-by-value-or-reference – user4581301 Jul 10 '22 at 06:42

2 Answers2

1

In addNumber function you need to pass vector with reference operator otherwise it will not be able to update your vector list. it is just creating copy of your vector and only modifing that

like this

void addNumber(vector<int> &list ) {
    int num;
    cout << "Please enter an integer:";
    cin >> num;
    list.push_back(num);
}
0

The reason for this is, Vectors are passed by value and not by reference. Arrays are passed by refernce! All the STL containers are passed by value. So, the copy of vector is created in the function, and when the function call is over, no changes are reflected back in the original vector. Try passing it by reference. Hope this helps you!