unexpected output

Hi people,I'm getting an unexpected output when I call this program just for example I add 200 to the bank account then it asks me if I want to enter more I say yes and add another 200 but the output is only 200 not 400 it seems if its avoiding whats in the If block of code.

(main)
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
  #include <iostream>
#include "bank.h"

using namespace std;

int main()
{
    int enter;
    cout << "enter an amount you want to put in" << endl;
    cin >> enter;
    bank will(enter);
    cout << "do you want to add any money" << endl;
    char add;
    cin >> add;
    if (add == 'y'){

       cout << "how much do you want to add" << endl;
       int more;
       cin >> more;
       will.addAmount(more);

    }
    cout << will.getAmount();
    return 0;
}



(bank.h)
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

#ifndef BANK_H
#define BANK_H
#include <iostream>

using namespace std;

class bank
{
    public:
        bank();
        bank(int amount);
        virtual ~bank();
        static void printInfo();
        void addAmount(int y);
        int getAmount();
        void withdraw(int x);

    private:
    static int users;
    int balance = 0;
    int amount = 0;

};

#endif // BANK_H


(bank.cpp)

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

#include "bank.h"
#include <iostream>

using namespace std;

int bank::users = 0;

bank::bank()
{
    users++;
}

bank::bank(int x){

   amount = x;
   balance += amount;
}

bank::~bank()
{

}

void bank:: addAmount(int y){

   balance +=y;
}

int bank:: getAmount(){

     return balance;
}

void bank:: printInfo(){

   cout << users;

}

void bank:: withdraw(int x){

   // not finished

}

Works fine for me.
1
2
3
4
5
6
7
8
enter an amount you want to put in
500
do you want to add any money
y
how much do you want to add
200
700
Press any key to continue . . .


BTW, your naming of getAmount() is confusing. One would think that getAmount() returns the amount variable, but noooooooooo, it returns the balance.
that's strange maybe it's my codeblocks? or something because for some reason it gives me the unexpected output,

and lol yeah I really should of used amount instead of balance
Topic archived. No new replies allowed.