如何为用户输入编写内联函数,将其存储在向量中并使用菜单选项调用它?
如何编写名为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码有很多东西,这些东西不必要并且可以改进:
首先,不需要单独的函数
menu()
。您只需在main()
函数中实现其代码即可。其次,输入10个整数值,
i < 10
是正确的,而不是i
11.
.第三,
使用命名空间 std
被认为是一种不好的做法。有关更多信息,请查找 为什么“using namespace std”被视为不好的做法。第四,而不是这样:
..你可以这样写:
所以在完成所有这些改进并满足你的需求之后,这里是代码:
正如你在上面的代码中看到的,我做了
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 themain()
function.Secondly, to enter 10 integer values,
i < 10
is correct, noti < 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:
..you can write this:
So after all of these improvements, and fulfilling your need, here is the code:
As you can see in the above code, I've made a
std::vector
inmain()
and then passed it to the functionAddValueToVector()
by reference (&
meansreference
).