gpt4 book ai didi

c++ - 停止向下一个函数发送值

转载 作者:行者123 更新时间:2023-11-28 07:51:31 24 4
gpt4 key购买 nike

我有这个工作代码:

/* Include files */
#include <iostream>
#include <string>
#include <limits>

using namespace std;

void fnMainMenu(char s);

/******************************************************************************
* Function: fnUpdateSalary
* Description: Update employee salary
******************************************************************************/
void fnUpdateSalary()
{
int choice = 0;
char data = 'a';
incorrect_input: // goto teleport exit :)
cout<<"\n\t=======================";
cout<<"\n\tWelcome\n";
cout<<"\t=======================\n\n";
cout<<"1. Update employee salary\n";
cout<<"2. Main menu\n";
cout<<"3. Exit system\n";
cout<<"\n >> ";

cin>>choice;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');

switch(choice){
case 1 :
//fnUpdateSalary();
break;
case 2 :
fnMainMenu(data);
break;
case 3 :
exit(1);
break;
default :
cout<<"Input not recognized. Please enter the correct input.\n";


}
}

void fnLog()
{
char data = 'b';
fnMainMenu(data); // call Main Menu and I want to pass "hello"
}





/******************************************************************************
* Function: fnMainMenu
* Description: Menu for the customer
******************************************************************************/
void fnMainMenu(char s)
{
cout << s;

if (s == 'a')
{ cout << "a = admin";
}
else
{cout << "\n\nb not admin";
}

//system("cls");
int chooice = 0;

cout<<"\n\t=======================";
cout<<"\n\tWelcome\n";
cout<<"\t=======================\n\n";
cout<<"1. Manage employee\n";
cout<<"2. Search employee\n";
cout<<"3. Employee report\n";
cout<<"4. Reset password\n";
cout<<"5. Exit system\n";
cout<<"\n >> ";
int numbers = 2;
cin>>chooice;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');

switch(chooice){
case 1 :
fnUpdateSalary();
break;
case 4 :
// fnChangePassword();
break;
default : exit(1);

}
}


/******************************************************************************
* Function: main
* Description: The main calling function
******************************************************************************/
int main()
{

fnLog();
return 0;
}

fnLog() 发送 bfnMainMenu()。当我在 fnUpdateSalary() 时,我想再次转到 fnMainMenu()。我的问题是,是否有 fnUpdateSalary() 或任何可用于调用 fnMainMenu() 的函数,而无需再次声明变量 data?我希望我可以使用 fnMainMenu(); 而不是

char data = 'r'; fnMainMenu(data);

如果我刚刚调用 fnMainMenu();,我会收到此错误 error C2660: 'fnMainMenu' : function does not take 0 arguments

我希望我的问题有道理。提前致谢。

最佳答案

无论如何,该参数似乎并没有真正的用途。将 b 传递到主菜单函数中肯定没有任何效果。如果您只想区分管理员和非管理员访问权限,请更改参数的类型和用法:

void fnMainMenu(bool is_admin) {
if (is_admin)
cout << "Admin\n";
else
cout << "Not admin\n";
}

然后这样调用它:

fnMainMenu(true);
// Or, alternatively:
fnMainMenu(false);

就是这样。顺便说一句,您不需要(也不应该!)在这里声明一个变量作为参数传递。直接传递值,就像我上面所做的那样。

此外,为什么您的函数名称以 fn 为前缀?不要这样做,这不是好的做法。只需使用能够很好地解释函数作用的专有名称即可。

关于c++ - 停止向下一个函数发送值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13672878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com