Issue with my linear search program

This linear search program is suppose to say if the account number is valid or not and it does it job until you get get past the second number in the first row? Then it keeps saying invalid number even though I typed in the correct number. So I'm a little confused why it is going smooth for the first 2 numbers in the array but the rest it is not picking up on.
Btw this is just to help me understand algorithms better program. I want to get better at problem solving. Any help in this stump I have would be greatly appreciated.
Thanks!

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

using namespace std;

int searchList(const int[], int, int);

int main()
{
    string a;
    int usrInput;
    const int SIZE = 18;
    int list[SIZE] = {5658845,8080152,1005231,4520125,4562555,6545231,
                    7895122,5552012,3852085,8777541,5050552,7576651,
                    8451277,7825877,7881200,1302850,1250255,4581002};

    cout << "Enter an account number: ";
    cin >> usrInput;

    //search list for the users input
    usrInput = searchList(list, SIZE, usrInput);

    //check to see whats what
    for(int i = 0; i < 9999999; i++){
    if(usrInput == list[i]){
        cout << "Your number is valid" << endl;
    }else{
        cout << "Invalid number" << endl;
    }

    return 0;
 }
}

//using linear search, this will help find the users account number
int searchList(const int list[], int numElems, int value){
    int index = 0;
    int position = -1;
    bool found = false;

    while(index < numElems && !found){
        if(list[index] == value){
            found = true;
            position = value;
        }
        index++;
    }
    return position;
}
On line 23, you do not need a for loop.
On line 24, you need to compare the user input with -1 (-1 is not a valid subscript in the array)

For instance:

1
2
3
4
5
6
		if (usrInput == -1){
			cout << "Invalid number" << endl;
		}
		else{
			cout << "Your number is valid" << endl;
		}


I hope it helps.
Should go:
if(usrInput != -1)

or, with so-called 'defensive programming'
if(usrInput>=0 && usrInput<SIZE)

Thank you guys for the quick response! I switched it to:

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

using namespace std;

int searchList(const int[], int, int);

int main()
{
    string a;
    int usrInput;
    const int SIZE = 18;
    int list[SIZE] = {5658845,8080152,1005231,4520125,4562555,6545231,
                    7895122,5552012,3852085,8777541,5050552,7576651,
                    8451277,7825877,7881200,1302850,1250255,4581002};

    cout << "Enter an account number: ";
    cin >> usrInput;

    //search list for the users input
    usrInput = searchList(list, SIZE, usrInput);

    //check to see whats what
    if(usrInput != -1){
        cout << "Your number is valid" << endl;
    }else{
        cout << "Invalid number" << endl;
    }

    return 0;
 }


//using linear search, this will help find the users account number
int searchList(const int list[], int numElems, int value){
    int index = 0;
    int position = -1;
    bool found = false;

    while(index < numElems && !found){
        if(list[index] == value){
            found = true;
            position = value;
        }
        index++;
    }
    return position;
}


Now it works perfect.
Topic archived. No new replies allowed.