I have three functions all above the main function. Based on user input I want them to be able to call the other functions, but for some reason if one function isn't literally above another it won't even know it exists. Is there a way to get around this?
void login_page()
{
int choice;
cout<<"Welcome to The First National Bank of Tamu"<<endl;
cout<<"Please make a selection"<<endl;
cout<<"1. Open a new account"<<endl;
cout<<"2. Login"<<endl;
cout<<"3. Close existing account"<<endl;
cin>>choice;
switch (choice){
case 1: new_acct(); login_page();
case 2: login();
//case 3: Login, withdraw money and close account
default: login_page();
}
}
void main_menu(customer& c)
{
int choice;
//welcome();
cout<<"Please make a selection:\n"<<endl;
cout<<"1. Withdraw money (up to $500)"<<endl;
cout<<"2. Make a deposit"<<endl;
cout<<"3. Account balance"<<endl;
cout<<"4. List the last 10 transactions"<<endl;
cout<<"5. Statistical information"<<endl;
cout<<"6. Logout"<<endl;
cin>>choice;
switch (choice){
case 1: //do something
case 2: //do something
case 3: //do something
case 4: //do something
case 5: //do something
case 6: login_page();
default: main_menu(c);
}
}
void login()
{
string fn, ln;
int pin;
customer c("","",0,0,0);
cout<<"First Name:";cin>>fn;
cout<<"\nLast Name:"; cin>>ln;
cout<<"\nPin #:"; cin>>pin;
ifstream ifs("bank.txt");
if (!ifs) error("Can't open bank file");
while(true) {
customer c("","",0,0,0);
if (!(ifs>>c)) break;
if(fn!=c.get_fn() || ln!=c.get_ln() || pin!=c.get_pin()){
cout<<"Fail\n";
}
else{
cout<<"Success\n";
customer c(c.get_fn(),c.get_ln(),c.get_bal(),c.get_acct(),c.get_pin());
main_menu(c);
}
}
}
They're nested in each other quite thoroughly which isn't a good idea, to be honest.
It's probably a circular-dependency problem: login() needs main_menu(), main_menu() needs login_page() and main_menu(), login_page() needs login() and login_page(). None of them is fully defined without the others.
I'm not certain C++ has problems with this, but it's pretty bad regardless. A function should never call itself, unless it's based on recursion. If a function needs to be repeated under certain circumstances, use a while loop to control the flow.
In the case where 'the next step' depends on the conditions at execution time, use a separate flow control function that decides which step is next and then calls it.
The compiler basically (it's more complicated than this) runs from top to bottom and when it sees the call it does not know anything about the functions that were declared yet. The forward declaration tells the compiler that there will be a function with a given signature defined later (or elsewhere). The signature is all that is needed to compile the calling code. If the function is declared and called but not defined, the linker will give an unresolved symbol or function call type error. It's important to understand the difference between declarations and definitions--which have very specific meanings in C++ but may be overlooked by beginners.
Ya I figured it wasn't good form, but unfortunately I'm on a time crunch. Thanks moorecm for the tip. That definitely allows my program to run now. So far it's doing what I want, but I know that could change real fast.