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

标签 c++

我有这个工作代码:

/* 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/

相关文章:

c++ - 代码格式化会导致目标文件内容发生变化吗?

c++ - glEnableVertexAttribArray 中 "index"参数的含义和(可能)OS X OpenGL 实现中的错误

c++ - 修正贝塞尔函数的精确计算 - 在 CUDA 中使用 netlib Fortran 例程?

c++ - #include <filename> 和 #include "filename"有什么区别?

c++ - 结构中二维数组的动态内存分配

c++ - 通过new和allocator分配内存有什么区别

c++ - 如何调用其原型(prototype)在另一个函数内的函数?

c++ - 如何从 vector 中删除结构元素?

c++ - std::atomic 是否提供原子行为,而不管顺序如何?

C++11 lambdas 可以访问我的私有(private)成员。为什么?