having trouble with referencing an array

Wrote a small banking program for my intro class. Everything seems to be fine except for the fact that when I use the new_acct function, the new elements added are not recognized by any of the other functions.Please help, ive been sitting on this problem all day!

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

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;
const int MAX_NUM = 30;

int main(){


    int acctnum_array[MAX_NUM];
    double balance_array[MAX_NUM];
    int read_accts(int[], double[], int);
    void print_accts(int[], double[],int);
    void menu ();
    int findacct(int[],int,int);
    int new_acct(int [], double [],int);
   


    int numaccts;

    char selection; // type of transaction selected
    bool cond=false; //used as condition for the do-while loop

    cout.setf(ios::fixed,ios::floatfield);
    cout.precision(2);              //set decimal precision

    //reads in data
    numaccts=read_accts(acctnum_array,balance_array,MAX_NUM);
    print_accts(acctnum_array,balance_array,numaccts);
    do{
        menu();
        cin>>selection;
        switch (selection)
        {
            case 'W':
            case 'w':
                withdrawal(acctnum_array,balance_array,numaccts);
                break;
            case 'D':
            case 'd':
                deposit(acctnum_array,balance_array,numaccts);
                break;
            case 'N':
            case 'n':
                numaccts=new_acct(acctnum_array,balance_array,numaccts);
                break;
            case 'B':
            case 'b':
                balance(acctnum_array,balance_array,numaccts);
                break;
            case 'Q':
            case 'q':
                print_accts(acctnum_array,balance_array,numaccts);
                cond=true;
                break;
            default:
                cout<<"\nThis selection was not valid, Please try again "<<endl<<endl;
                break;
         }
    }while (cond!=true);
     return 0;
    }





int read_accts(int acctnum_array[], double balance_array[], int max_accts)
{
    ifstream infile;
    infile.open("c:\\c++\\prg6input.txt");

    int num_accts=0,x;
    double y;

    infile>>x;

    while(!infile.eof()&& num_accts< max_accts){
        acctnum_array[num_accts]=x;
        infile>>y;
        balance_array[num_accts]=y;
        num_accts++;
        infile>>x;
    }
    infile.close();
    return num_accts;
}

void print_accts(int acctnum_array[], double balance_array[], int num_accts)
{
    for ( int i=0; i<num_accts;i++){
        cout<<"Account number: "<<acctnum_array[i]<<" has a balance of ";
        cout<<balance_array[i]<<endl<<endl;
    }
    return;
}

void menu()
{
    cout<<"\nSelect one of the following options"<<endl;
    cout<<"W - Withdrawal"<<endl;
    cout<<"D - Deposit"<<endl;
    cout<<"N - New Account"<<endl;
    cout<<"B - Balance"<<endl;
    cout<<"Q - Quit"<<endl;
    return;
}

int findacct(int acctnum_array[], int num_accts, int account)
{
    cout<<account<<endl;
    cout<<num_accts<<endl;
    int check=-1;
    for(int i=0;i < num_accts;i++){
        if (acctnum_array[i]==account)
            check=i; //returns index number if account is valid
    }
    return check; //returns -1 if account is not valid
}


int new_acct(int acctnum_array[], double balance_array [], int num_accts)
{
    int accountnum,index,newacct;
    newacct=num_accts;
    cout<<"\nPlease enter a new account number"<<endl;
    cin>>accountnum;
    index=findacct(acctnum_array,num_accts,accountnum);
    if (index!=-1)
        cout<<"\nThis account number already exists"<<endl;
    else{
        newacct=num_accts+1;
        acctnum_array[newacct]=accountnum;
        cout<<acctnum_array[newacct]<<endl;
        balance_array[newacct]=5;
        cout<<balance_array[newacct]<<endl;
        cout<<"\nThis account has been processed and now has a balance of 0"<<endl;
        print_accts(acctnum_array,balance_array,newacct);
    }
    return newacct;
}




1
2
3
4
5
6
7
8
// assert: array has Num elements, indices [0..Num-1]
newacct = Num + 1;
array[newacct] = accountnum; // This has the error
return newacct;
// assert: array has Num+1 elements, indices [0..Num]

// However, array[Num] was not filled with data
// (but array[Num+1] was) 
So how do I fix the problem? I am still a bit confused.
Look at the array indexing.

Lets run a very simple example the way you do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Preface
int array[5];
array[0] = 7;
array[1] = 4;
int Num = 2;
// ok, array has 2 elements, and it contains { 7,4,_,_,_ }
// I use '_' to mean unknown random value
// When you now do other operations, you pass (array,2),
// and they see { 7,4 }

// Now call new_acct:
int next = Num+1; // next==3
array[next] = 8; // array[3] = 8;
return next; // you store return value to Num, so Num==3

// After new_acct
// array == { 7,4,_,8,_ }
// Num == 3
// When you now do other operations with (array,3),
// they see the { 7,4,_ } 

In this example the next available index was 2, but your code wrote to index 3.
Topic archived. No new replies allowed.