0

How do I write an INLINE iterative function called AddValueToVector () which prompts the user to enter ten (10) integer values, which will be used throughout the program. The values should be stored in a vector and the function should be used when the user presses A from the menu options. The vector should be passed to the function by reference.`

This is what I have so far.

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

inline void AddValueToVector(char Option)
{
    int i = 0;
    vector <int> store;

    for (i = 0; i<11; i++){
        cout<<"Enter Integer"<<endl;
        cin>>i;

    }

}

void menu ()
{
    char Option;
    cout << "              Vector Manipulations" <<endl;
    cout <<"            ***********************"<<endl;
    cout <<"                   MENU OPTIONS"<<endl;
    cout <<"             *********************"<<endl;
    cout <<"Press [A] - To ADD values to Vector"<<endl;
    cout <<"Press [S] - To SEARCH Vector Contents"<<endl;
    cout <<"Press [P] - To PRINT Vector Contents"<<endl;
    cout <<"Press [M] - To find the MINIMUM Value in the Vector"<<endl;
    cout <<"Press [L] - To find the MAXIMUM value in the Vector"<<endl;
    cout <<"Press [X] - To EXIT program"<<endl;
    cout <<"Please Select an Option From The Menu: "<<endl;
    cin >>Option;

    switch(Option)
    {
        case 'A':
            cout<<"You Have Selected: To Add Values to Vector: "<<endl;
            AddValueToVector('A');
        break;
    }
}
int main ()
{
    menu();

 return 0;
}
  • What problem are you facing with this code? – The Coding Fox Feb 27 '22 at 16:19
  • 1
    What do you think `inline` does? Because it doesn’t seem to be relevant to anything you’re doing there. – Sneftel Feb 27 '22 at 16:19
  • I did some research and apparently, its suppose to make the code run faster or something like that... but what I really want to do is store the user input in a vector and be able to call that option using the menu but I'm not sure if my code is correct. – Learn Language Feb 27 '22 at 16:34
  • @LearnLanguage It doesn't make the code faster, at least not by the mere fact that the function is `inline`. If you are an absolute beginner, just forget for now that `inline` exists. – user17732522 Feb 27 '22 at 16:45

1 Answers1

1

Your code has a lot of things that are unnecessary and can be improved:

Firstly, there is no need for a separate function menu(). you can just implement its code in the main() function.

Secondly, to enter 10 integer values, i < 10 is correct, not i < 11.

Thirdly, using namespace std is considered a bad practice. For more info, look up to why is "using namespace std" considered as a bad practice.

Fourthly, instead of this:

std::cout << "Hello " << std::endl;
std::cout << "World!" << std::endl;

..you can write this:

std::cout 
    << "Hello " << std::endl
    << "World!" << std::endl;

So after all of these improvements, and fulfilling your need, here is the code:

#include <iostream>
#include <vector>

inline void AddValueToVector(std::vector<int>& vec)
{
    std::cout << "You Have Selected: To Add Values to Vector: " << std::endl;

    for (int i = 0; i < 10; i++) 
    {
        std::cout << "Enter Integer: ";
        vec.push_back(0);
        std::cin >> vec[vec.size() - 1];
    }
}

int main()
{
    std::vector<int> store;

    while (true)
    {
        char Option;

        std::cout 
            << "              Vector Manipulations" << std::endl
            << "            ***********************" << std::endl
            << "                   MENU OPTIONS" << std::endl
            << "             *********************" << std::endl
            << "Press [A] - To ADD values to Vector" << std::endl
            << "Press [S] - To SEARCH Vector Contents" << std::endl
            << "Press [P] - To PRINT Vector Contents" << std::endl
            << "Press [M] - To find the MINIMUM Value in the Vector" << std::endl
            << "Press [L] - To find the MAXIMUM value in the Vector" << std::endl
            << "Press [X] - To EXIT program" << std::endl;

        std::cout << "Please Select an Option From The Menu: ";
        std::cin >> Option;

        if (std::tolower(Option) == 'x') break;

        switch (std::tolower(Option))
        {
        case 'a':

            AddValueToVector(store);
            break;
        }
    }

    for (auto& i : store)
    {
        std::cout << i << std::endl;
    }

    return 0;
}

As you can see in the above code, I've made a std::vector in main() and then passed it to the function AddValueToVector() by reference (& means reference).

The Coding Fox
  • 1,488
  • 1
  • 4
  • 18