如何为用户输入编写内联函数,将其存储在向量中并使用菜单选项调用它?

发布于 2025-01-10 06:39:42 字数 1503 浏览 4 评论 0原文

如何编写名为 AddValueToVector () 的内联迭代函数,该函数提示用户输入十 (10) 个整数值,这些值将在整个程序中使用。这些值应存储在向量中,并且当用户从菜单选项中按 A 时应使用该函数。向量应该通过引用传递给函数。`

这是我到目前为止所拥有的。

#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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2025-01-17 06:39:42

您的代码很多东西,这些东西不必要并且可以改进

首先,不需要单独的函数menu()。您只需在 main() 函数中实现其代码即可。

其次,输入10个整数值,i < 10 是正确的,而不是 i 11..

第三, 使用命名空间 std 被认为是一种不好的做法。有关更多信息,请查找 为什么“using namespace std”被视为不好的做法。

第四,而不是这样:

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

..你可以这样写:

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

所以在完成所有这些改进并满足你的需求之后,这里是代码:

#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;
}

正如你在上面的代码中看到的,我做了main() 中的 std::vector ,然后通过引用将其传递给函数 AddValueToVector() (& 表示参考)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文