Is There anything wrong with this code?

I have two derived classes from a parent class and I'm trying to do an array of both and inputting a file with information of each bankAccount. Could anyone see what is wrong with this code?

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
  void getMemberData(ifstream& infile, savingsAccount saccounts[], checkingAccount caccounts[]) {
    
    string firstName;
    string lastName;
    char AccountType;
    string accountBalance;
    string trash;
    int count;
    
    infile.open("members.txt");
    

    
    for(count = 0; count < 10; count++){
        int k = 0;
        int b = 0;
        
        infile >> firstName;
        infile >> lastName;
        infile >> AccountType;
        infile >> accountBalance;
        infile >> trash;
        
        if(AccountType == 's'){
            saccounts[k].setFirstName(firstName);
            saccounts[k].setLastName(lastName);
            saccounts[k].setAccountType("Savings");
            saccounts[k].setAccountBalance(stod(accountBalance));
            saccounts[k].setAccountNumberDefault();
            saccounts[k].setAccountNumber();
            k++;
        }
        
        if(AccountType == 'c'){
            caccounts[b].setFirstName(firstName);
            caccounts[b].setLastName(lastName);
            caccounts[b].setAccountType("Checking");
            caccounts[b].setAccountBalance(stod(accountBalance));
            caccounts[b].setAccountNumberDefault();
            caccounts[b].setAccountNumber();
            b++;
        }
        
    } // END FOR LOOP
    
    
} //END GET MEMBER FUNCTION 



Here's the file:

1
2
3
4
5
6
7
8
9
10
Alexandra Comaduran s 5000
Jesus Christ c 1000000
James Disciple c 50000
John TheBaptist c 8000
Paul Saul s 90000
Thomas Doubter c 70000
John Loved s 900000
Mary Magdalene c 300000
Inez Solis s 80000
Mary Wendt c 600000
Since you set k=0 and b=0 at the start of each loop, you will never set array elements other than saccounts[0] and baccounts[0], irrespective of what lines 31 and 41 say.

Declare and initialise k and b OUTSIDE (and before) the loop.

Given the level of repetition you need to vastly simplify your code. Lines 18 to 22 can be done in a single line. Lines 25-30 and 35-40 can each be done in one line if you define the constructor of an account.
Topic archived. No new replies allowed.