我的目标是显示一个菜单,用户可在其中选择5个功能(1-5)。函数运行后输出其数据。最后,用户再次选择功能(1-5)。
我的(不正确的)半码:
int main(){
int option;
int *pOption = &option;
bool choice = true;
cout << "Main menu:
cout << "To use function 1 enter 1. \n";
cout << "To use function 2 enter 2. \n";
cout << "To use function 3 enter 3. \n";
cout << "To exit please enter 0. \n";
cin >> option;
while (choice == true) {
if (option == 1) {
cout << "You picked Function one: ";
functionOne(Variable);
}
if (option == 2) {
cout << "You picked Function 2: ";
functionOne(Variable);
}
if (option == 3) {
cout << "You picked Function 3: ";
functionOne(Variable);
}
}//end of while loop
return 0;
}
例如,假设用户点击选项1并转到下面的此功能。在我们输出后,如何利用指针&#39; pOption&#39;我创建了选择要使用的新功能?还是我完全错过了球?
void functionOne(Variable){
cout<< "This is function 1";
cin >>pOption;
return;
}
答案 0 :(得分:1)
如果我有足够的时间在电池电量耗尽之前,我会坚持使用似乎是主要问题的内容并找到其他内容。
不要在调用函数内部请求下一个选项,而是在main函数中执行此操作:
cin >> option;
while (0 != option) <-exit loop if option is zero
{
if (option == 1) {
cout << "You picked Function one: ";
functionOne(Variable);
}
if (option == 2) {
cout << "You picked Function 2: ";
functionOne(Variable);
}
if (option == 3) {
cout << "You picked Function 3: ";
functionOne(Variable);
}
cin >> option; <- ask for next option here
}//end of while loop
接下来,您可以通过阅读else if
和switch
cin >> option;
while (0 != option)
{
switch (option) {
case 1:
cout << "You picked Function one: ";
functionOne(Variable);
break;
case 2:
cout << "You picked Function 2: ";
functionOne(Variable);
break;
case 3:
cout << "You picked Function 3: ";
functionOne(Variable);
break;
default:
cout << "You picked an unsupported option: " << option;
}
cin >> option;
}//end of while loop
接下来,如果“我不是一个数字,傻瓜”选项中的一些傻瓜类型怎么办?阅读输入验证。谷歌是你的朋友,而不是我。我的电池电量为3%,我正在关机。再见。