Why can't my functions see each other?

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?
Here's the three functions if anyone is interested

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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.
Try putting forward declarations for each function that is called above its definition. For example, you could probably just list them at the top:
1
2
3
4
void login();
void main_menu(customer& c);
void login_page();
void new_acct(); // ? 


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.
Last edited on
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.
Topic archived. No new replies allowed.