why is my array "out of scope"

I am getting the error that my array was not declared in this scope. Why does it do this and how can I be sure of where the declaration goes?

Header
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
 void MainMenu();
  void CreateAccount();
  void PrintData();
  //void PrintCustomerData();
 // void FillBankArray(BankAccount[]);
  //void StoreData(string, int, long double, int);
  //int CreateAccountNumber(int);
   //Classes
   class BankAccount
   {
   	private:
   		
   		string AccountHolder;
   		int AccountNumber;
   		long double Balance;
   		long double InterestRate;
   	//	BankAccount();
   	public:
   		//Member Functions
   		//BankAccount MyAccountInfo[100];
   		void CreateAccount();
        void StoreData(string, int, long double);
        void PrintCustomerData();
        void Deposit();
        void Withdrawal();
        BankType();
     //   BankAccount MyBankArray[];
   };//end class 


main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
 #include <cstdlib>
 #include "Assignment2.h"


int main()
 {
     int choice;
     //MyAccountInfo[100];
    BankAccount MyAccountInfo[100];
 	 cout << "*****Pro-Bank Extreme*****" << endl;
 	 MainMenu();
 	 system ("pause");
	return 0;
 
 }//end main 


functions
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
 void CreateAccount()
 {
      
	  BankAccount MyBankData;
	  BankAccount MyBankArray[100];
	  int AccountNumber = 0, index = 0;
	  long double Balance = 50.0;
	  string AccountHolder;
 cout << "Enter Account Holders Name" << endl;
 cin >> AccountHolder;
 cout << "Congratulations....Your NEW Pro-Bank Extreme account has been successfully created..." << endl;
 cout << "As a bonus for opening a new account, a $50.00 credit has been automatically deposited into your new account!!" << endl;
 cout << "Your Pro-Bank Extreme account number is : " << AccountNumber << endl;
 MyBankArray[index].StoreData (AccountHolder, AccountNumber, Balance);
// MyAccountInfo[index] = MyBankData;
// cout << MyBankArray[0]BankAccout.AccountHolder << endl;
//MyBankArray[0].PrintCustomerData();
  };//end CreateAccount
  void PrintData ()
 {
   for (int index = 0; index < 20; ++index)
 {
cout << MyBankArray[index].PrintCustomerData() << endl;
}//end for
  };//end PrintData
// void FillBankArray (BankAccount MyBankArray[]) 
//{
//	for (int index = 0; index < 20; ++index)
//	MyBankArray[index]MyBankData.StoreData (AccountHolder, AccountHolder, Balance);
//}//end FillBankArray
 int CreateAccountNumber()
 {
 	static int AccountNumber = 0;
 	return ++AccountNumber;
 }//end CreateAccountNumber
  void BankAccount::StoreData(string AccountHolder, int AccountNumber, long double Balance)
   {
    long double InterestRate = 10.0;
    int index = 0;
    AccountHolder = AccountHolder;
    AccountNumber = AccountNumber;
    Balance = Balance;
	BankAccount MyBankData;	
	MainMenu();   
   }//end StoreData
  void BankAccount::PrintCustomerData ()
  {
  	cout << "Primary Account Holder Name :" << AccountHolder << endl;
  	cout << "Pro-Bank Extreme Account Number :" << AccountNumber << endl;
  	cout << "Current Ballance :" << Balance << endl;
  	cout << "Interest Rate :" << InterestRate << endl;
  }//end PrintCustomerData 
  void BankAccount::Deposit()
  {
  	long double DepositAmount;
  cout << "Enter Primary Account Holder Name :" << endl;
  cout << "Enter Deposit Ammount :" << endl;
  cin >> DepositAmount;
  	
  }//end Deposit
  void BankAccount::Withdrawal()
  {
  	long double WithdrawalAmount;
  cout << "Enter Primary Account Holder Name :" << endl;
  cout << "Enter Withdrawal Ammount :" << endl;
  cin >> WithdrawalAmount;		
  }//end Withdrawal 


menu
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
 #include <iostream>
 #include <cstdlib>
 #include "Assignment2.h"
 
 
 void MainMenu()
{
    //=====Variable declarations
    BankAccount MyBankArray[20];
	int Choice;
    bool quit = false;
	while (!quit)
	{
	
	//=====body
    cout << "== MAIN MENU ==" << endl;
    cout << "1. ADD a customer" << endl;
    cout << "2. Print ALL customer data for ALL customers" << endl;
    cout << "3. UPDATE existing customer DATA" << endl;
    cout << "4. EXIT" << endl;
    
	cout << "Enter a menu choice: " << endl;
    cin >> Choice;
    cout << endl;
    
    
    switch(Choice)
    {
        case 1: CreateAccount(); //replace "Add a customer with CreateAccountNumber Function here
                  
            break;
        case 2: PrintData();
		//case 2: cout << "Print All customer DATA" << endl; //replace "Print ALL customer DATA" with PrintCustomerData function here
            break;
        case 3: cout << "UPDATE existing customer DATA: " << endl;
                cout << "a) Make a Deposit: " << endl;
                //(Deposit) 
                cout << "b) Withdrawal: " << endl;
                //(Withdrawal)
                cout << "c) Print Account Balance: " << endl;
                //(PrintAccountBallance)
                cout << "d) Update Account Balance WITH interest: " << endl;
                //(UpdateAccountBalance)
                cout << "e) Exit to Main Menu" << endl;
                //(ExitToMainMenu)
            break;
        case 4: cout << "EXIT" << endl;
                quit = true;
            break;
    }        
    //return(Choice);
}//end while
}//end MainMenu 
main
-----
line 10: You define MyAccountInfo, but you never reference it.

line 12: You want to pass MyAccountInfo to MainMenu as a parameter.

functions
---------
line 5: You define MyBankArray as a local array. It goes out of scope when CreateAccount exists.

line 14: You're storing data into the local array, but the data will be lost when the local array goes out of scope.

line 23: You reference MyBankArray, but MyBankArray is not in scope.

line 43: Why are you declaring myBankData? You don't reference it.

line 44: Why does StoreData call MainMenu? Calling a menu has nothing to do with storing data.

line 58: You cin DepositAmount to a local variable, but don't do anything with it.

line 66: Ditto for WithdrawalAmount.

menu
-----

line 9: Why are you declaring MyBankArray as a local array. Again, it's going to go out of scope when MainMenu exits.

edit:
Here's a cleanup of your code. It compiles, but has not been tested.
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <string>
#include <iostream>
#include <cstdlib>
//  #include "Assignment2.h"
using namespace std;

class BankAccount
{   string AccountHolder;
   	int AccountNumber;
   	long double Balance;
   	long double InterestRate;

public:
    void CreateAccount();
    void StoreData (const string &, int, long double);
    void PrintCustomerData();
    void Deposit();
    void Withdrawal();
};

void CreateAccount(BankAccount accts[], int & numaccts)
{   int AccountNumber = 0, index = 0;
	long double Balance = 50.0;
	string AccountHolder;
 
    cout << "Enter Account Holders Name" << endl;
    cin >> AccountHolder;
    cout << "Congratulations....Your NEW Pro-Bank Extreme account has been successfully created..." << endl;
    cout << "As a bonus for opening a new account, a $50.00 credit has been automatically deposited into your new account!!" << endl;
    cout << "Your Pro-Bank Extreme account number is : " << AccountNumber << endl;
    accts[numaccts].StoreData (AccountHolder, AccountNumber, Balance);
    numaccts++;
}  

void PrintData (BankAccount accts[], int numaccts)
{   for (int index = 0; index < numaccts; ++index)
    {   accts[index].PrintCustomerData();
    }
}

int CreateAccountNumber()
{   static int AccountNumber = 0;
 	return ++AccountNumber;
}   

void BankAccount::StoreData (const string & AcctHolder, int AcctNumber, long double Bal)
{   long double IntRate = 10.0;
    
    AccountHolder = AcctHolder;
    AccountNumber = AcctNumber;
    Balance = Bal;
    InterestRate = IntRate;
	BankAccount MyBankData;	 
}   

void BankAccount::PrintCustomerData ()
{   cout << "Primary Account Holder Name :" << AccountHolder << endl;
  	cout << "Pro-Bank Extreme Account Number :" << AccountNumber << endl;
  	cout << "Current Balance :" << Balance << endl;
  	cout << "Interest Rate :" << InterestRate << endl;
} 
  
void BankAccount::Deposit()
{   long double DepositAmount;
    
    cout << "Enter Deposit Ammount :" << endl;
    cin >> DepositAmount; 	
    Balance += DepositAmount;
}

void BankAccount::Withdrawal()
{   long double WithdrawalAmount;
    
    cout << "Enter Withdrawal Ammount :" << endl;
    cin >> WithdrawalAmount;	
    if (WithdrawalAmount > Balance)
    {   cout << "Widthdrawal exceeeds balance" << endl;
        return;
    }	
    Balance -= WithdrawalAmount;
}

 void MainMenu (BankAccount accts[], int numaccts)
{   int Choice;
    bool quit = false;

	while (!quit)
	{   cout << "== MAIN MENU ==" << endl;
        cout << "1. ADD a customer" << endl;
        cout << "2. Print ALL customer data for ALL customers" << endl;
        cout << "3. UPDATE existing customer DATA" << endl;
        cout << "4. EXIT" << endl;
    
	    cout << "Enter a menu choice: " << endl;
        cin >> Choice;
        cout << endl;
    
        switch(Choice)
        {
        case 1: CreateAccount (accts, numaccts); //replace "Add a customer with CreateAccountNumber Function here                  
                break;
                
        case 2: PrintData (accts, numaccts);
		        break;
		        
        case 3: cout << "UPDATE existing customer DATA: " << endl;
                cout << "a) Make a Deposit: " << endl;
                //(Deposit) 
                cout << "b) Withdrawal: " << endl;
                //(Withdrawal)
                cout << "c) Print Account Balance: " << endl;
                //(PrintAccountBallance)
                cout << "d) Update Account Balance WITH interest: " << endl;
                //(UpdateAccountBalance)
                cout << "e) Exit to Main Menu" << endl;
                //(ExitToMainMenu)
                break;
        case 4: cout << "EXIT" << endl;
                quit = true;
                break;
        }        
    }
} 

int main()
{   BankAccount MyAccountInfo[100];
    int numaccts = 0;
    
 	cout << "*****Pro-Bank Extreme*****" << endl;
 	MainMenu (MyAccountInfo, numaccts);
 	system ("pause");
	return 0;
}

Last edited on
Thank you so much for your help. I will use this as an example to make the rest better.....Please stand by in case I have a few more questions over the next gew days. Thanks again!
Ok, so how come when passing an empty array as a parameter in multiple functions does it keep its info? specifically in lines 35 & 83....
Note sure what you mean by an "empty array". 1) Do you mean the array has nothing in it, or 2) the array dimensions are not specified (e.g. [])?

1) We pass numaccts in the second argument to tell PrintData() and MainMenu() how many entries exist in the array.

2) We don't need to pass the actual dimension of the array to those two functions. The compiler knows how big one instance of BankAccount is and can easily calculate the address of any occurrence within the array. When an array is passed like that, a pointer to the base of the array is what is actually passed.

ok, thanks that makes sense...:)

well explained!!!
I kinda think that I need to include some sort of way to query the user to specify which account to add to or withdraw from. I can quite get it straight in my mind how I would do that though. I know that there must be a way to get an account number from the user, search the array for that number, verify that it is a good number and then operate on it. Any help would be great...
I kinda think that I need to include some sort of way to query the user to specify which account to add to or withdraw from.

Yes, I had meant to mention that but hoped you would figure it out for yourself.

I know that there must be a way to get an account number from the user, search the array for that number, verify that it is a good number and then operate on it.

You have the right steps. Now it's just a matter of writing the code to perform those steps.

Your steps suggest two new functions.

1
2
3
4
string prompt_for_account_name()
{}
BankAccount * find_account (const string & name)
{}

Note that find_account() returns a pointer to a BankAccount entry. If you don't find the account, then you should return null.
yes, I am learning, slowly.....

I have been working on that part here at work (pseudo code). I am thinking that I want to either allow the user to find the account by either Account Number or by Name. probably use a switch.

1
2
3
string SearchByName (SearchCriteria) {};

int SearchByNumber (SearchCriteria) {};


I am stuck on the idea of how, once I find my account, do I operate on it without knowing the index position in the array?

Since it is stored in an array, the only way of accessing it is through its index...right?
Since it is stored in an array, the only way of accessing it is through its index...right?

Not necessarily. You can also access the elements of the array using pointers. However, to keep things simple, use an index to access the array, then when you find the element you want, return the address of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
BankAccount * find_account_by_name (BankAccount accts[], int numaccts, const string & name)
{   for (int i=0; i<numaccts; i++)
     if (accts[i].AccountHolder == name)
    {  return &accts[i];  // return address of entry found
    }
    return NULL;  // Not found
}

//  In your Deposit and Withdrawal functions
  BankAccount *  acct;   // Pointer to account entry.  Not initialized yet
  string acct_name;

   cout << "Enter account name to search for: ";
   cin >> acct_name;
  acct = find_account_by_name (accts, numaccts, acct_name);
  if (acct == NULL)
  { cout << "Account not found" << endl;
    return;  // get out of what you are doing
  }
  //  Now we have a valid pointer to the account we were looking for 
  acct->PrintCustomerData();  // Or whatever you want to do with the account at this point 

Search by number works exactly the same way, except find_account_by_number matches on account number rather than name.

that looks kinda like the example I found earlier except that it is using a pointer.

Questions about pointers....
Why use a pointer? isn't it a just "link" to the address? I guess I wonder why, unless the data at that address is large, would pointers be useful?

In the function find_account_by_name...to what does the pointer belong? BankAccount?

We started looking at pointers a few weeks ago and I just don't understand all the hubbub...I'm a "why" kinda guy...

Thanks again for teaching me!!
Last edited on
We could have done find_account_by_name() a couple of ways. An alternate way would have been for find_account_by_name to return the index of the entry or -1 if not found. That's probably easier to get you head around initially.

Why use a pointer? isn't it a just "link" to the address?

A pointer is simply a type of variable that contains an address. Pointers are generally more efficient than indexing. With indexing, the compiler has to calculate the address of the element for you (base address + (index * sizeof(element)). With a pointer, you have the address of the element directly and no further address calculation is required. C++ provides the -> operator so that you can reference members of a struct through a pointer as I did at line 21.

In the function find_account_by_name...to what does the pointer belong? BankAccount?


Note that find_account_by_name() returns a pointer of type BankAccount *. This is a temporary value stored on the stack. At line 10, we've declared a local pointer to an object of type BankAccount. This pointer doesn't "belong" to anything. It's not a member of BankAccount. We simply use that variable to store the address returned by find_account_by_name() at line 15.


This pointer doesn't "belong" to anything. It's not a member of BankAccount. We simply use that variable to store the address returned by find_account_by_name() at line 15.

ah...I see said the blind man....lol

Thanks!!
so the function, if that is what it is considered, "find_account_by_name" is producing an error stating that "AccountHolder" is private. I am thinking it is because no scope operator :: is provided to operate on the class. The other functions created as such void BankAccount::Deposit() have the scope operators. How do I add the operators?
Sorry. I was thinking that find_account_by_name was a member of the class, but that would not make sense. BankAccount represents one account. We need to search the array of BankAccounts, so find_account_by_name() really should be a global function, not a member function.

To get around accessing a private member, the easiest thing to do is to provide a getter member of the class which returns the name.

1
2
3
4
5
6
7
8
class BankAccount
{
class public: 
    string get_name() const
    { return AccountHolder;
    }
...
};


Then change line 3 of find_account_by_name as follows:
 
  if (accts[i].get_name() == name)


You will need to do the same thing for account number.
Last edited on
Topic archived. No new replies allowed.