[Help me] "float t" to set an initial value of 0

Hi, I'm having a little problem with a private variable in a class I am working on for practice.

I can't get this "float t" to set an initial value of 0 so when the program gets to the withdraw() function it doesn't pass it to the deposit() function for them to first deposit money before making a withdrawal. I hope that made sense.

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
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

class bank {
    public:
        float withdraw()
            {
                if(t == 0)
                {
                    cout << "You must deposit money first before you may withdraw money." << endl;
                    deposit();
                }

                cout << "You have a total of " << t << " in your bank." << endl;
                cout << "How much would like to withdraw?" << endl;
                cin >> w;
                t = t - w;
                cout << "Your account balance is now: " << t << endl;
                cout << "What would you like to do:" << endl;
                cout << "withdraw | deposit | balance | quit" << endl;
                cin >> r;


                if(r == "withdraw")
                {
                    withdraw();
                }
                else if(r == "balance")
                {
                    cout << "Your account balance is: ";
                    return t;
                }
                else if(r == "deposit")
                {
                    deposit();
                }
            }

        float deposit()
            {
                cout << "How much would you like to deposit?" << endl;
                cin >> d;

                t = t + d;
                cout << "Your account balance is now: " << t << endl;

                cout << "What would you like to do:" << endl;
                cout << "withdraw | deposit | balance | quit" << endl;
                cin >> r;
            }

    private:
        string r; //string variable for response.
        float t; //total account balance.
        float d; //total amount you are depositing.
        float w; //total amount you are withdrawing.

};
int main()
{
    bank one;
    one.deposit();
    return 0;
}


I keep getting an error if I try something like "float t = 0;" it keeps saying:

error: ISO c++ forbids initialization of member 't'
error: making 't' static
error: ISO c++ forbids in-class initialization of non-const static member 't'

**I know it has no return in the deposit() function, I haven't completed it yet so don't worry about that**

Anyone who can help, I really appreciate it
You want a constructor:

1
2
3
4
5
6
7
8
9
10
11
class bank
{
public:
  bank()
  {
    t = 0;
    d = 0;
    w = 0;
  }
//...
}


http://cplusplus.com/doc/tutorial/classes/ <- see section on constructor and destructors
Initialization is done in the constructor.

http://www.cplusplus.com/doc/tutorial/classes/

Scroll down to constructors and deconstructors.
create a constructor and initialize the variables in there
Topic archived. No new replies allowed.