Using private variable from class without getters and setters

Pages: 12
Ok so I think I have everything working except the most important part, the part that gets the pin number. The program seems to be just loading the first 4 things and not the other accounts details. Its not looping through to find them.

main.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
#include <iostream>
#include <fstream>
#include <limits>
#include "Account.h"

int main()
{
    int choice = 0;

    while(choice != -1)
    {
        std::cout << "Welcome to the bank, what would you like to do?\n" << std::endl;

        std::cout << "1) Create Account" << std::endl;
        std::cout << "2) Login to existing account" << std::endl;

        std::cin >> choice;

        Account account;

        switch(choice)
        {
            case 1:
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                account.CreateAccount();
                break;
            case 2:
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                account.LoginToAccount();
                break;
            default:
                std::cout << "Error" << std::endl;
        }
    }

    return 0;
}



Account.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
28
29
30
31
#ifndef ACCOUNT_H_INCLUDED
#define ACCOUNT_H_INCLUDED

#include <iostream>

class Account
{
    public:
        Account() = default;
        ~Account() = default;
        void CreateAccount();
        void LoginToAccount();
        float Deposit();
        float Withdraw();
        float ShowBalance(float) const;
        void SaveGame();
        bool RestoreGame();
        void UserAccount();

    private:
        std::string firstName = "Default_First";
        std::string lastName = "Default_Last";
        short unsigned int userPinNumber = 0;
        float accountBalance = 0;
        float depositAmount = 0;
        float withdrawAmount = 0;
        short unsigned int pinNumberToFind = 0;
};


#endif // ACCOUNT_H_INCLUDED 



AccountCode.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <fstream>
#include <limits>
#include "Account.h"

/*
===============================================================================

Account::CreateAccount()

===============================================================================
*/

void Account::CreateAccount()
{

    std::cout << "Please enter your first name" << std::endl;
    getline(std::cin, firstName);

    std::cout << "\nNow please enter your last name" << std::endl;
    getline(std::cin, lastName);

    std::cout << "\nThank you " << firstName << " " << lastName << " Now please enter your pin" << std::endl;
    std::cin >> userPinNumber;

    SaveGame();
}


/*
===============================================================================

Account::LoginToAccount()

===============================================================================
*/

void Account::LoginToAccount()
{
    std::cout << "Please enter your pin number to access your account" << std::endl;
    std::cin >> pinNumberToFind;

    if(RestoreGame() == true)
    {
        UserAccount();
    }
    else if(RestoreGame() == false)
    {
        std::cout << "Sorry the credentials you have entered are incorrect." << std::endl;
    }
}


/*
===============================================================================

Account::UserAccount()

===============================================================================
*/

void Account::UserAccount()
{
    std::cout << "Hello " << firstName << " " << lastName << std::endl;
    std::cout << "What would you like to do?" << std::endl;
    std::cout << "1) Deposit" << std::endl;
    std::cout << "2) Withdraw" << std::endl;
    std::cout << "3) Check balance" << std::endl;

    int choice = 0;

    std::cin >> choice;

    switch(choice)
    {
        case 1:
            accountBalance += Deposit();
            std::cout << "Your current account balance is: $" << accountBalance << std::endl;
            break;
        case 2:
            accountBalance -= Withdraw();
            break;
        case 3:
            std::cout << "Your balance is $" << accountBalance << std::endl;
    }
}


/*
===============================================================================

Account::Deposit()

===============================================================================
*/

float Account::Deposit()
{
    std::cout << "How much would you like to deposit?" << std::endl;
    std::cin >> depositAmount;

    return depositAmount;
}


/*
===============================================================================

Account::Withdraw()

===============================================================================
*/

float Account::Withdraw()
{
    std::cout << "How much would you like to withdraw?" << std::endl;
    std::cin >> withdrawAmount;

    if(withdrawAmount <= accountBalance)
    {
        withdrawAmount -= accountBalance;
        std::cout << "You withdrew " << withdrawAmount << " your new balance is " << accountBalance << std::endl;
    }
    else if(withdrawAmount > accountBalance)
    {
        std::cout << "You cannot withdraw more money than you have in your account" << std::endl;
    }

    return withdrawAmount;
}


/*
===============================================================================

Account::SaveGame()

===============================================================================
*/

void Account::SaveGame()
{
    std::ofstream saveFile("File.txt", std::ios::app);

    saveFile << userPinNumber << std::endl;
    saveFile << firstName << std::endl;
    saveFile << lastName << std::endl;
    saveFile << accountBalance << std::endl;

    saveFile.close();
}


/*
===============================================================================

Account::RestoreGame()

===============================================================================
*/

bool Account::RestoreGame()
{
    std::ifstream restoreFile("File.txt");

    while(userPinNumber != pinNumberToFind)
    {
        restoreFile >> userPinNumber;
        restoreFile >> firstName;
        restoreFile >> lastName;
        restoreFile >> accountBalance;

        if(userPinNumber == pinNumberToFind)
        {
            return true;
            restoreFile.close();
        }
        else if(userPinNumber != pinNumberToFind)
        {
            return false;
            break;
        }
    }
}
Last edited on
Also, the account balance keeps resetting to 0, im unsure if I should make this a static variable or a reference pointer.
else if(userPinNumber != pinNumberToFind)
{
return false;
break;
}


Keep looking until you find the right account. This doesn't keep looking. It just checks the first account it reads, and if it's not the right one, the function returns.
Last edited on
1
2
3
4
5
   if(RestoreGame() == true)
    {
        UserAccount();
    }
    else if(RestoreGame() == false)

This calls the RestoreGame() twice. That's not right.
Got it! I did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool Account::RestoreGame()
{
    std::ifstream restoreFile("File.txt");

    while(userPinNumber != pinNumberToFind)
    {
        restoreFile >> userPinNumber;
        restoreFile >> firstName;
        restoreFile >> lastName;
        restoreFile >> accountBalance;

        if(userPinNumber == pinNumberToFind)
        {
            return true;
            restoreFile.close();
        }
        else if(userPinNumber != pinNumberToFind)
        {
            continue;
        }
    }
}


and that worked. I have 3 different accounts and can successfully load them.
Last edited on
as for the other thing could i do:

1
2
3
4
5
6
7
8
if(RestoreGame() == true)
    {
        UserAccount();
    }
    else if(false)
    {
        std::cout << "Sorry the credentials you have entered are incorrect." << std::endl;
    }


1
2
      return true;
      restoreFile.close();


Will that second line EVER happen?
Whoops, my mistake. I fixed it and switched them around.
is there a site somewhere that has practice problems to solve? when you had me extend the program to accept additional accounts it really helped me. Anything that doesnt involve doing math problems though, I'm aweful at math. any problems like the one above are fine though.
Bump
You could use this site. Just look for posts that describe the whole problem that they are trying to solve. Try to solve it yourself and then return to the question a few days later to see the solution that the asker or answerers gave.
Registered users can post here. Sign in or register to post.
Pages: 12