PROGRAM WEIRD EXIT.

I'm new to programming on a whole and im stuck on a weird place where my program just exits when it reaches a part of it.
The .h and main file will be in the reply section

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "ATM.h"
#include <iostream>
using std::cout;//needed for printing to the screen
using std::cin;//needed for reading from the keyboard
using std::endl;//needed to create a newline
#include <cstdlib>
#include<string>
using std::getline;//function getline comes from the string library
using std::string;//needed to declare string variables/objects


    ATM::ATM(int pinNum)
    {
        if(!isValidPin(pinNum))//calling function isValidPin and passing it pinNum
        {
            exit(-1);/*function exit() comes from library cstdlib causes any program to quit immediately*/
        }
        else
            pin = pinNum;/*if we get here, then the pinNum was valid and we assign it to
                           our data member, pin */


        cout<<"Please enter your date of birth in the format: dd/mm/yyyy ";

        /*function getline grabs an entire line of text INCLUDING spaces;
          stops reading when the user hits enter or when it reaches the end of line;
          getline grabs text using cin which as you know, is connected to the keyboard;
          getline saves the text it grabs inside of a string variable, in our case,
          inside of the variable, dateOfBirth.
        */
        string dateOfBirth;//we need to declare a string to store the date of birth
        getline(cin, dateOfBirth);//grabs a string in the format dd/mm/yyyy
        parseDOB(dateOfBirth);

        string fullName;
        cout<<"\nPlease enter your full name: ";
        getline(cin, fullName);//grabs a first and last name separated by a space and stores it in fullName
        parseName(fullName);//passes fullname to function parseName() that will separate and assign to firstName and lastName respectively


        cout<<"\nPlease enter your address.  It must not exceed 25 characters.\n";
        getline(cin, address);//grabs the full address from the keyboard, including spaces up until enter is hit or newline encountered
        setAddress(address);//passing address to function verifyAddress()
        balance = 0.0;//initializing the balance to zero
        loggedIn = false;//User must login using their new pin before they can do anything
    }

    ATM::ATM(int pinNum, string dob, string name, string add)
    {
        pin = pinNum;
        ATM::parseDOB(dob);
        ATM::parseName(name);
        address = add;
        balance = 0.0;

    }
    ATM::ATM(int pinNum, string dob, string name)
    {
        pin = pinNum;
        ATM::parseDOB(dob);
        ATM::parseName(name);
    }
    ATM::ATM(int pinNum, string dob, string name, double startBal)
    {
        pin = pinNum;
        ATM::parseDOB(dob);
        ATM::parseName(name);
        if (startBal<0){
            balance = 0.0;
        }
            else{
                balance = startBal;
        }
    }

    void ATM::printStatement()
    {
       cout<<"Bank of the People"<<endl<<ATM::getFirstName()<<ATM::getLastName()<<endl<<ATM::getDay()<<" "<<ATM::getMonth()<<" "<<ATM::getYear()<<endl<<balance<<endl;


    }

    void ATM::setAddress(string add)
    {
        if(add.length() > 25)//checking if the address received is longer than 25 characters
        {
            cout<<add<<" exceeds 25 characters!  Only 25 characters will be used\n";
            string tempaddress = add.substr(0,25);
            setAddress(tempaddress);//assigns the shortened address to data member, address

        }
        else//this code only runs if the condition above fails
        {
            setAddress(add);
        }
    }

    string ATM::getAddress()
    {
        return address;
    }

    //prints customer's name, dob, address and current balance
    void ATM::deposit(double amount)
    {if (loggedIn == 0){
            cout<<"Error. You are not logged in."<<endl;
        }

  else if (amount<0){
            cout<<"Error: Amount entered is negative."<<endl;
        }
    else {
        balance = balance + amount;



    }
    }
    void ATM::printWelcomeScreen()
    {
        cout<<"Welcome to the Bank of the People"<<endl;



    }
    void ATM::setDay(int d)
    {
        if ((d<=31)&&(d>0)){
            day = d;
        }
            else {
            day = 1;
        }

    }
    void ATM::setMonth(int m)
    {

        if ((m<=12)&&(m>0)){
            month = m;
        }
            else {
            month = 1;
        }

    }
    void ATM::setYear(int y)
    {
        if ((y>0)&&(y<=2015)){
            year = y;
        }
            else{
            year = 2015;
        }

    }
    int ATM::getDay()
    {
        return day;


    }
    int ATM::getMonth()
    {
        return month;


    }
    int ATM::getYear()
    {
        return year;


    }
    void ATM::setFirstName(string fname)
    {

        firstName = fname;

    }
    void ATM::setLastName(string lname)
    {

        lastName = lname;

    }
    string ATM::getFirstName()
    {
        return firstName;


    }
    string ATM::getLastName()
    {
        return lastName;


    }
    bool ATM::isLoggedIn()
    {

        return loggedIn;

    }
    bool ATM::withdraw(double amount)
    {
        ATM::isLoggedIn();

        if (loggedIn == 0){
                cout<<"Error. You are not logged in."<<endl;
                return 0;
    }
            else if (balance<amount){
                    cout<<"Error: Insufficient funds"<<endl;
                    return 0;
    }
                 else if(amount<=0){
                     cout<<"Error: amount entered is negative"<<endl;
                     return 0;
        }
                        else {

                            balance = balance - amount;
                            return 1;
        }
    }
    void ATM::login(int pinNum)
    {
        if (pinNum == pin){
            loggedIn = 1;
            cout<<"You are now logged in"<<endl;
        }
            else{
                loggedIn = 0;
                cout<<"Status: login failed"<<endl;
        }


    }
    bool ATM::changePin(int newPin)
    {
          ATM::isLoggedIn();

          if (loggedIn == 0){
              cout<<"Error. You are not logged in."<<endl;
              return 0;
          }
                else {
                    pin = newPin;
                    return 1;

    }
    }

    bool ATM::isValidPin(int pinNum)
    {
        if(pinNum < 1000 || pinNum > 9999)
        {
            cout<<"Pin number must be 4 digits long.";
            return false;//returning false means that pinNum's value was inappropriate
        }
        else //now check whether all 4 digits are unique
        {
            short int firstDigit = pinNum/1000;//int divided by int returns an int
            pinNum = pinNum%1000;//pinNum will now get the remainder from the division
            short int secondDigit = pinNum/100;
            pinNum = pinNum%100;
            short int thirdDigit = pinNum/10;
            short int fourthDigit = pinNum%10;


            return(firstDigit != secondDigit && firstDigit!=thirdDigit && firstDigit!=fourthDigit && secondDigit!=thirdDigit && secondDigit!=fourthDigit && thirdDigit!=fourthDigit);
        }

    }

    void ATM::parseDOB(string dob)
    {
        string day = dob.substr(0,2);

        int d = stoi(day);
        ATM::setDay(d);

        string month = dob.substr(3,2);
        int m = stoi(month);
        ATM::setMonth(m);

        string year = dob.substr(6,4);

        int y = stoi(year);
        ATM::setYear(y);


    }

    void ATM::parseName(string fullName)
    {

        int position = fullName.find(" ");
        int position2 = fullName.find("\n");

        string firstname = fullName.substr(0,position);
        ATM::setFirstName(firstname);

        string lastname = fullName.substr(position,position2);
        ATM::setLastName(lastname);

    }
Last edited on
THIS IS THE .H FILE.

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
#include <iostream>
using std::cout;//needed for printing to the screen
using std::cin;//needed for reading from the keyboard
using std::endl;//needed to create a newline
#include <cstdlib>
#include<string>
using std::getline;//function getline comes from the string library
using std::string;//needed to declare string variables/objects


class ATM
{
    public:
        /*note that this class has no default constructor.
        A default constructor can be recognized because it has no paramaters.
        A class can only have one default constructor but it can have as many
        alternate constructors.  An alternate constructor is simply any constructor
        with one or more parameters.
        */
        ATM(int pinNum);
        ATM(int pinNum, string dob, string name, string add);
        ATM(int pinNum, string dob, string name);
        ATM(int pinNum, string dob, string name, double startBal);
        void printStatement();//prints customer's name, dob, address and current balance
        void deposit(double amount);
        void printWelcomeScreen();
        void setDay(int d);
        void setMonth(int m);
        void setYear(int year);
        void setAddress(string add);
        void setFirstName(string fname);
        void setLastName(string lname);
        int getDay();
        int getMonth();
        int getYear();
        string getAddress();
        string getFirstName();
        string getLastName();       
        bool isLoggedIn();
        bool withdraw(double amount);
        void login(int pinNum);
        bool changePin(int newPin);

    private:
        bool isValidPin(int pinNum);//this function is declared in the private section
        void parseDOB(string dob);
        void parseName(string fullName);


        /* private functions are also called helper functions or utility functions.
        helper functions are only visible within the class and cannot be called from
        anywhere outside of the class, not in main() or anywhere else.  Their job is to help other public functions do something.
        */


        int pin;//represents the person's 4-digit pin number
        int month;//represents person's month of birth
        int day;//represents person's day of birth
        int year;//represents person's year of birth
        double balance;//represents person's current balance
        string firstName;//represents person's first and last name
        string lastName;
        string address;//represents person's permanent address


        /*keeps track of whether a person is logged in.  When it is set to true,
        the person is logged in.  False means the person needs to login.
        */
        bool loggedIn;
};
THE 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
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
#include "ATM.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;

int main()
{
    /*
     *atm1, atm2 and atm3 all attempt to create an ATM object containing duplicate digits.
     *Uncomment each line below, one at a time.  Each line should cause the program to exit
     *
     */



    //ATM atm1(1335);
    //ATM atm2(1231);
    //ATM atm3(1155);

    /*
     *After testing the three statements above, comment them all again to test the code below.
     */

    cout<<"\n\n----------------------------atm5----------------------------\n";
    cout<<"\nCreating ATM object atm5\n";

    cout<<"atm5 initialized with: 5139,\"09/05/2000\",\"Mary  Challow\", \"19-55 Krangton Avenue Calimari\"\n";
    ATM atm5(5139,"09/05/2000","Mary Challow", "19-55 Krangton Avenue Calimari");
    cout<<"atm5's first name: "<<atm5.getFirstName()<<endl;
    cout<<"atm5's last name: "<<atm5.getLastName()<<endl;
    cout<<"atm5's address: "<<atm5.getAddress()<<endl;
    cout<<"atm5's dob day: "<<atm5.getDay()<<endl;
    cout<<"atm5's dob month: "<<atm5.getMonth()<<endl;
    cout<<"atm5's dob year: "<<atm5.getYear()<<endl;

    cout<<"\nPrinting atm5's bank statement:\n";
    atm5.printStatement();

    cout<<"\nAttempting to change atm5's pin number without first logging in.\n";
    atm5.changePin(1234);

    cout<<"\nAttempting to make atm5 do a deposit without first logging in.\n";
    atm5.deposit(1000);

    cout<<"\nAttempting to make atm5 do a withdrawal without first logging in.\n";
    atm5.withdraw(500.00);

    cout<<"\nAttempting to login for atm5 using wrong pin number\n";
    atm5.login(5133);

    cout<<"\nAttempting to login for atm5 using valid pin number\n";
    atm5.login(5139);

    cout<<"\nAttempting AGAIN to change atm5's pin number after successful login.\n";
    if(atm5.changePin(1234))
        cout<<"Status: pin successfully changed.\n";
    else
        cout<<"Status: pin number failed to be changed\n";

    cout<<"\nAttempting AGAIN to make atm5 do a deposit of $1000 after successful login.\n";
    atm5.deposit(1000);

    cout<<"\nPrinting atm5's bank statement after $1000.00 deposit.\n";
    atm5.printStatement();

    cout<<"\nAttempting AGAIN to make atm5 do a $300 withdrawal after successful login.\n";
    atm5.withdraw(300.00);

    cout<<"\nPrinting atm5's bank statement after a $300.00 withdrawal.\n";
    atm5.printStatement();

    cout<<"\nAttempting to make atm5 do a deposit of -$200.\n";
    atm5.deposit(-200);

    cout<<"\nPrinting atm5's bank statement after -$200 deposit.";
    atm5.printStatement();

    cout<<"\nAttempting to make atm5 do a -$50 withdrawal.\n";
    atm5.withdraw(-50.00);

    cout<<"\nPrinting atm5's bank statement after a -$50.00 withdrawal.\n";
    atm5.printStatement();

    cout<<"\nAttempting to make atm5 withdraw $5000 (exceeding current balance).\n";
    atm5.withdraw(5000);

    cout<<"\nPrinting atm5's bank statement after a withdrawal of $5000.00 withdrawal.\n";
    atm5.printStatement();

    cout<<"\n\n----------------------------atm4----------------------------\n";
    cout<<"\nCreating ATM object atm4\n";
    cout<<"atm4 initialized with: 1357.  Other values defined by user.\n";
    ATM atm4(1357);

    cout<<"atm4's first name: "<<atm4.getFirstName()<<endl;
    cout<<"atm4's last name: "<<atm4.getLastName()<<endl;
    cout<<"atm4's address: "<<atm4.getAddress()<<endl;
    cout<<"atm4's dob day: "<<atm4.getDay()<<endl;
    cout<<"atm4's dob month: "<<atm4.getMonth()<<endl;
    cout<<"atm4's dob year: "<<atm4.getYear()<<endl;

    cout<<"\nPrinting atm4's bank statement:\n";
    atm4.printStatement();



    return 0;
}
You've written your setAddress member function with infinite recursion. How about:
1
2
3
4
5
6
7
8
9
    void ATM::setAddress(string add)
    {
        if(add.length() > 25)
        {
            cout<<"Address exceeds 25 characters! Only 25 characters will be used\n";
            add = add.substr(0,25);
        }
        address = add;
    }

Hey tpb, I fixed it right before I saw your reply. Can't believe it took me so long to realize that. But hey, if you don't mind can you help me with something else? I'm trying to make my parseDOB function convert the numbers it gets for month to the actual month, how may I do that?
Look up the names in a vector/array of month names?
Haven't learned that in my C++ class yet but I will do some research and get back with something.
I'm clueless as to how im going to make my set function get the values out of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string months[12];
        months[1] = "January";
        months[2] = "February";
        months[3] = "March";
        months[4] = "April";
        months[5] = "May";
        months[6] = "June";
        months[7] = "July";
        months[8] = "August";
        months[9] = "September";
        months[10] = "October";
        months[11] = "November";
        months[12] = "December";
        string month = dob.substr(3,2);
        int m = stoi(month);
        ATM::setMonth(m);
You should store the dates with numbers. Convert to words when you output the month for human consumption, and maybe when reading a date entered by a person if necessary. That brings up the question: what is the format of the date that parseDOB() gets in the parameter? You should add that as a comment.

Here is code and data to let you convert a month name to number and back again:
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
static const char *monthNames[13] = {
    "bad",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};

// Conver month name to a number. Returns -1 if the name doesn't match. Note that this
// is case sensitive.
int monthNameToNum(const string &name)
{
    for (unsigned i=1; i<13; ++i) {
        if (name == monthNames[i]) return i;
    }
    return -1;
}

const char *monthNumToName(int num)
{
    return monthsNames[num];
}

Topic archived. No new replies allowed.