Problem With Arrays

Nov 25, 2015 at 3:25am
Hey Guys! I'm trying to get this program to output the balance of an account, when an account number is input. It compiles fine, but doesn't run properly. Thoughts?

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
//FindBal.cpp
//Arrays

#include <iostream>
using namespace std;

void FindBal( int id[5], float bal[5], float idtofind, int & found, float & result)
{
    int pos = 0;
    
    while(pos <= 6 || pos >= 0)
    {
        if(idtofind == id[pos])
        {
          bal = &bal[pos];
          found = true;
        }
       else
            pos++;
    }
}

int main()
{
    int id[5] = { 123, 234, 333, 401, 500 };
    float bal[5] = { 0.0, 100.0, 250.0, 50.0, 1225.0 };
    float result;
    int idtofind, found;
    
    cout << "ID to find? ";
    cin >> idtofind;
    
    while (idtofind > 0)
    {
        FindBal(id, bal, idtofind, found, result);
        if (found)
            cout << "The balance is " << result << endl;
        else
            cout << "Not found";
        
        cout << "ID to find? ";
        cin >> idtofind;
    }
}
Nov 25, 2015 at 3:55am
Just a quick look, but I think your issue is on line 15. bal s/b result?
Nov 25, 2015 at 4:04am
closed account (48T7M4Gy)
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
//FindBal.cpp
//Arrays

#include <iostream>

using namespace std;

int main()
{
    int id[5] = { 123, 234, 333, 401, 500 };
    float bal[5] = { 0.0, 100.0, 250.0, 50.0, 1225.0 };
    
    int idtofind = 0, index = 0, selected = 0;
    
    cout << "ID to find? ";
    cin >> idtofind;
    
    while ( index < 5 )
    {
        if ( id[index] == idtofind )
            cout << bal[index];
        
        index++;
    }
    
    return 0;
}
Nov 26, 2015 at 3:52am
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/179614/

Multiple posts are bad
Nov 26, 2015 at 4:00am
closed account (48T7M4Gy)
result = bal[pos];
Topic archived. No new replies allowed.