My Linear-Search isn't working

This program is supposed to print out deposits and withdrawal of autos, but the problem is that it uses parallel arrays to link the car number (0,1,2,etc) with a car id.
When the program runs, it prints out all the car ids (for depositing only since I haven't adjusted withdrawals for ids yet) as -1, which would mean the id doesn't exists.
I tried to see if the files were at fault but when I cout the data from the files, the information is correct.
I think it has something to do with the function findautoid.
Thanks for any help.

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
#include <iostream>
#include <fstream>
#include <iomanip>

#define N 10
#define CARVALUE 10000

using namespace std;

void printStock (float [], int);
float sales (float [], int);
int findautoid (float [], int , int);

int main(){
ifstream myfile;
ifstream myfile2;
myfile.open("/Users/Soul/Documents/School/CISC/Assignment6/file.txt");
myfile2.open("/Users/Soul/Documents/School/CISC/Assignment6/file2.txt");

float inv[N], cmt;
int autoid[N];
for(int n = 0; n<N; n++){
    myfile2 >> autoid[n];
}
int acct, type;
int temp;
printStock(inv,N);

myfile>>type>>acct>>cmt;
while(myfile){
    if (type == 2){
        inv[acct] += cmt;
        autoid[acct] += cmt;
        cout << "Deposit of auto " << findautoid(invl,N,autoid[acct]) << ": " << cmt << endl;
        }

    if (type == 1)
        if (inv[acct] < cmt)
                cout << "ERROR: Not enough on stock" << endl;
        else{
            inv[acct] -= cmt;
            cout << "Withdrawal of auto " << acct << ": " << cmt << endl;
            }

    if (type != 2 && type !=1)
        cout << "ERROR: Invalid Transation number" << endl;

    myfile>>type>>acct>>cmt;
    cout << endl;
    }
myfile.close();

printStock(inv,N);
cout << "Total Income: $" << setprecision(100) << sales(inv,N) << endl;
return 0;
}


void printStock(float inv[], int nelts){
cout << "Car#" << setw(10) << "On Stock" << endl;
for (int n = 0; n<nelts; n++)
    cout << setw(0) << n << setw(9) << inv[n] << endl;
cout << endl << endl;
return;
}

float sales(float inv[], int nelts){
float sum = 0;
for (int n = 0; n<nelts; n++)
    sum += inv[n];
sum *= CARVALUE;
return sum;
}


int findautoid (float numb[], int n, int newnumber){
for(int position = 0; position < n; position++)
    if(numb[position] == newnumber){
        return position;
    }
        return -1;
}
On line 34, where does invl come from? If it's a typo, you should have gotten an obvious compilation error.
Last edited on
That was a typo which was fixed, I honestly have no idea how it got there. Even without the typo, the program still has difficulty being the findautoid will always return -1
Any help here?
Have you tried debugging?

If you don't have time to learn how to use your debugger, at the very least try outputting what the parameters are at the beginning of the function (between lines 76 and 77 in your currently posted code).
Topic archived. No new replies allowed.