Class problem

Hey guys, I'm having trouble compiling this code, I don't exactly know what I am doing wrong as I am only new to classes. Am I even close to setting out my function right? What am I doing wrong here? Any help would be greatly appreciated.

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
# include <iostream>
using namespace std;
const int maxAccounts = 10;

//savings account class 
class savingsAccount
{
      //private data kept encapsulated here (account numbers, balances and interest rate)
      private:
              int accountNum;
              float balance;
              float iRate;
      //accessor functions 
      public:
             void displayBalance () const;
};

int main ()
{  
    //variables
    float iRate = 0.05;
    int pos;
    
    //initializing struct savingsAccount
    savingsAccount accnts[maxAccounts] = 
                   {
                   123456,0.0,iRate,
                   246890,0.0,iRate,
                   937568,0.0,iRate,
                   337761,0.0,iRate,
                   846579,0.0,iRate,
                   293567,0.0,iRate,
                   917355,0.0,iRate
                   };
                   
     savingsAccount.displayBalance();
     
     system("pause");
return 0;
}

void savingsAccount::displayBalance()
{
     for (int count = 0; count < 7; count++)
     cout << "The account number is: " << accnts[count].accountNum << "The balance for this account is: $" << accnts[count].balance;
}
Yeah it looks like its a part of the same assignment, but this is only a small portion that I am trying to get working so I can understand whats going on and how to link my functions with the classes
The syntax:
1
2
3
4
5
6
7
8
9
10
	savingsAccount accnts[maxAccounts] = 
	{
		123456,0.0,iRate,
		246890,0.0,iRate,
		937568,0.0,iRate,
		337761,0.0,iRate,
		846579,0.0,iRate,
		293567,0.0,iRate,
		917355,0.0,iRate
	};
is a C shortcut for initialising an array of structs.

It really should be:
1
2
3
4
5
6
7
8
9
10
	savingsAccount accnts[maxAccounts] = 
	{
		{ 123456, 0.0F, iRate },
		{ 246890, 0.0F, iRate },
		{ 937568, 0.0F, iRate },
		{ 337761, 0.0F, iRate },
		{ 846579, 0.0F, iRate },
		{ 293567, 0.0F, iRate },
		{ 917355, 0.0F, iRate }
	};


You can't use it with a C++ class because class members are private and cannot be accessed outside of the class.
Yeah thanks for that. Originally when I ran the code I had multiple error messages regarding the curly braces, when I removed them the error went away but 1 still remained, but now I see that I need to have the initializing in the class itself. Thanks for the help :)
Topic archived. No new replies allowed.