Using a function that's already been used...

Hi,

(Bear with me here, total C++ noob)

I'm currently making a simple ATM program for our Programming Subject. I got around to making the core components of the ATM program like the balance inquiry, withdraw and deposit. However, I'm having trouble with the PIN Change function.

Basically, after making the new PIN code, I want to re-direct to a new PIN entry page that will re-direct to the Main Menu once the inputted PIN code is the same as the new PIN code made in the PIN Change function.

The problem is, the Main Menu is already called when I first start the program. If I call it again (as we all know), the "function redefinition" error comes up.

So my question is, is there any way I can re-call the Main Menu function if its already used?

Concept Map of what I'm talking about can be found here (I can't post my codes right now, so this should at least clarify what I'm referring to.)

http://i.imgur.com/DLJj2Q7.png
Last edited on
If I call it again (as we all know), the "function redefinition" error comes up
.

Function definition and function calling are two different things :+)

Function definition happens once. Function calling can happen as many times as one likes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

void MyFunction(); // function forward declaration tells compiler that this function exists

int main() {
  const int limit = 10;
  for (int a = 0; a < limit; ++a) {
    std::cout << "Calling the function for the " << a+1 << " time " <<  "\n";
    MyFunction();
    std::cout << "\n";
  }


} // end of main

// Function definition:
void MyFunction() {
   std::cout << "Inside MyFunction" ;
}
Last edited on
I forgot one thing: all of our functions are on different .cpp files

So here's my Main Menu function:
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
81
82
83
84
#include <iostream>
#include <conio.h>

#include "GlobalResources.h"
#include "Balance.cpp"
#include "Landing_2.cpp"
#include "Deposit.cpp"
#include "Withdraw.cpp"
#include "Change PIN.cpp"
#include "PIN_2.cpp"
using namespace std;

int mainmenu()
{
	char A, B, C, D, E, OpCode, RetCode, Y, N;
	
	ReMain:
	system ("cls");
	cout << "Welcome to the Bank of Hearts's ATM Service!" << endl;
	cout << "How may we help you?" << endl;
	cout << "A = Balance Inquiry" << endl;
	cout << "B = Deposit" << endl;
	cout << "C = Withdraw" << endl;
	cout << "D = Change my PIN Code" << endl;
	cout << "E = Exit" << endl;
	
	OpCode = toupper (getche());
	
	if (OpCode == 'A')
		{
			Balance();
			goto ReturnCode;
		}
	
	else if (OpCode == 'B')
		{
			Deposit();
			goto ReturnCode;
		}
		
	else if (OpCode == 'C')
		{
			Withdraw();
			goto ReturnCode;
		}
		
	else if (OpCode == 'D')
		{
			ChangePIN();
			pin2();
		}
		
	else if (OpCode == 'E')
		{
			endlandpage2();
			goto ender;
		}
			
	else 
		{
			cout << "INVALID INPUT!" << endl;
			goto ReMain;
		}
		
	ReturnCode:
		cout << "Would you like to do another transaction? (Y/N)" << endl;
		RetCode = toupper (getch());
		
		if (RetCode == 'Y')
			goto ReMain;
			
		
		else if (RetCode == 'N')
			endlandpage();
			
		else
			{
				system ("cls");
				cout << "Invalid Return Response!" << endl;
				goto ReturnCode;
			}

ender:;
}


and here's my Change PIN function:

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <conio.h>
#include <string.h>
#include "GlobalResources.h"
#include "delay_7.cpp"
#include "DeadEnd_2.cpp"
#include "PIN_2.cpp"

using namespace std;

main()
{
		int x, y, z, count = 1, NewCount = 1;
		char Decision, Y, N;
		char TempPIN1[5], TempPIN2[5];
		ReChange:
		system ("cls");
		cout << "WARNING! This process is irreversible! Do you still want to continue? (Y/N) ";
		Decision = toupper (getch());
		
		if (Decision == 'Y')
		{
				ReTry:
				system ("cls");
				cout << "Enter your PIN Code: " << endl;
				for (x = 0; x <= 4; x++)
				{
					TempPIN1[x] = getch();
					putch ('*');
				}
		
				PIN[5] = '\0';
				
				NewPass:
				if (strcmp (TempPIN1, "12345") == 0)
				{
					cout << endl << "Enter your new password: ";
					for (y = 0; y <= 4; y++)
					{
						NewPIN[y] = getch();
						putch ('*');
					}
					
					NewPIN[5] = '\0';
					
					cout << endl << "Enter the new password again: ";
					for (z = 0; z <= 4; z++)
					{
						TempPIN2[z] = getch();
						putch ('*');
					}
					
					TempPIN2[5] = '\0';
					
					if (strcmp (NewPIN, TempPIN2) == 0)
					{
						strcpy (NewPIN, PIN);
						cout << endl << "PIN Code has been changed. " << endl;
						delaychangepin();
						pin2();
					}
					
					else
					{
						cout << endl << "New password does not match!" << endl;
						goto NewPass;
					}
								
				}
				
				else
				{
					count = count + 1;
			
					if (count <= 3)
				{
					system ("cls");
					cout << "INVALID PIN CODE! PLEASE TRY AGAIN!" << endl;
					delaychangepin();
					goto ReTry;
				}
			
				else
				{
				DeadEnd2();
				}
		}
		}
		
		else if (Decision == 'N')
		{
			
		}
		
		else
		{
			
			cout <<  endl << "Invalid Decision Response!" << endl;
			delaychangepin();
			goto ReChange;
		}
		
}


and here's the new PIN login function:
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
#include <iostream>
#include <conio.h>
#include <string.h>

#include "GlobalResources.h"
#include "DeadEnd_3.cpp"
#include "delay_8.cpp"
#include "Main Menu.cpp"

int pin2()
{
	int x, count = 1;
	ReTry:
		system ("cls");
		cout << "Enter your PIN Code: " << endl;
		for (x = 0; x <= 4; x++)
		{
			PIN[x] = getch();
			putch ('*');
		}
		
		PIN[5] = '\0';
		
		if (strcmp (PIN, NewPIN) == 1)
		{
			system ("cls");
			cout << "PIN CODE CORRECT!" << endl;
			delayrepin();
			//AFTER HERE, I SHOULD BE GOING INTO THE MAIN MENU, BUT DON'T KNOW HOW...
		}
		
		else
		{
			count = count + 1;
			
			if (count <= 3)
			{
				system ("cls");
				cout << "INVALID PIN CODE! PLEASE TRY AGAIN!" << endl;
				delayrepin();
				goto ReTry;
			}
			
			else
			{
				DeadEnd3();
				return 0;
			}
		}
}


PS: I know, using goto is a sin.
PPS: I think the reason why I'm getting the function redefinition errors is I don't declare the functions at the top.

Still, If the functions are scattered on different files, how am I gonna use for loop to recall the Main Menu function on the new PIN input function?
Last edited on
You are getting function redefinition errors because you are including source files (*.cpp)
Don't include *.cpp files, add them to your project.

You'll need to create headers with the prototype of the functions. Include those headers.
PS: I know, using goto is a sin.


Well, don't do it then :+) This is going to sound harsh: If I was a teacher, I would deduct 40% of the marks if I saw so much goto.

There are the keywords: break and continue which are used with loops. Also, consider switch statements with a default: case. One can put a switch inside a while loop, and control it with a bool value:

1
2
3
4
5
6
7
8
bool Quit = false;

while (!Quit) {
   //switch
   // each case calls a function
   // quit case sets Quit to true
   // default: case catches bad input
}


PPS: I think the reason why I'm getting the function redefinition errors is I don't declare the functions at the top.


Never include .cpp files, only header files.

Still, If the functions are scattered on different files, how am I gonna use for loop to recall the Main Menu function on the new PIN input function?


return from a function.

Also, provide meaningful names for your variables and functions. x , y , z aren't good.

Always initialise your variables to something, and delay the declaration of them until you have a useful value to do that with.

Good Luck !!
Last edited on
Topic archived. No new replies allowed.