Reputation: 1543
I have this working code:
/* 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()
send b
to fnMainMenu()
. When I am at fnUpdateSalary()
I want to go to fnMainMenu()
again. My question is, is there anyway from fnUpdateSalary()
or whatever function available to call fnMainMenu()
without declaring variable data
again? I was hoping I could just use fnMainMenu();
instead of
char data = 'r'; fnMainMenu(data);
I get this error error C2660: 'fnMainMenu' : function does not take 0 arguments
if I just called fnMainMenu();
I hope my question make sense. Thanks in advance.
Upvotes: 0
Views: 66
Reputation: 545686
The parameter doesn’t seem to serve a real purpose anyway. Passing b
into the main menu function certainly achieves nothing. If you just want to distinguish between admin and non-admin access, change the type and usage of the argument:
void fnMainMenu(bool is_admin) {
if (is_admin)
cout << "Admin\n";
else
cout << "Not admin\n";
}
And call it like this:
fnMainMenu(true);
// Or, alternatively:
fnMainMenu(false);
That’s it. Incidentally, you don’t need (and shouldn’t!) declare a variable to pass as the argument here. Just pass the value directly, like I’ve done above.
Also, why are your function names prefixed by fn
? Don’t do this, it’s not good practice. Just use proper names that explain well what the functions do.
Upvotes: 1
Reputation: 20258
Since you're writing C++ code and data
seems to be relatively long-lived with respect to the application logic, it would perhaps make sense to regroup these functions into a class.
data
could be a data member of this class, and fnUpdateSalary
, fnLog
, fnMainMenu
methods.
class Application {
public:
Application ()
: data ('b') // default value
{ }
void fnLog() {
data = 'b';
fnMainMenu ();
}
void fnMainMenu() {
if (data == 'a')
cout << "a = admin";
else
cout << "\n\nb not admin";
// ...
fnUpdateSalary ();
// ...
}
void fnUpdateSalary() {
// ...
fnMainMenu ();
// ...
}
private:
char data;
};
int main () {
Application app;
app.fnLog ();
}
Upvotes: 0
Reputation: 1671
If I fully understand what you are doing, you need a combination of two things. Firstly, a static variable in fnMainMenu and secondly a default parameter:
fnMainMenu(char s = '\0')
{
static char c;
if(s != '\0') c = s;
...
...
}
The "static" keyword means that the character will be preserved across function calls. The default parameter means that s will be assigned a null termination character unless you explicitly pass another value.
Upvotes: 0