Help with Linear search!

Hello!

I'm trying to complete so old homework assignments, and could use some pointers. When my program runs, it spits out both valid and invalid.

Here's the prompt:

Write a program that lets the user enter a charge account number. The program should determine if the number is valid by checking for it in the following list:

5658845 4520125 7895122 8777541 8451277 1302850
8080152 4562555 5552012 5050552 7825877 1250255
1005231 6545231 3852085 7576651 7881200 4581002
The list of numbers above should be initialized in a single-dimensional array. A simple linear search should be used to locate the number entered by the user. If the user enters a number that is in the array, the program should display a message saying that the number is valid. If the user enters a number that is not in the array, the program should display a message indicating that the number is invalid.

Could use a push in the right direction.

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
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
  #include <iostream>
using namespace std;

// Function prototype
int search_list(int [], int, int);

//need to add a function to get data from the user

const int num_count = 18; 

// main function

int main()
{
    
// declare variables

	int tests[num_count] = {5658845, 450125, 7895122, 8777541, 8451277,
		1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
		1005231, 654231, 3852085, 7576651, 7881200, 4581002};


	double answer;
	answer = 5658845, 450125, 7895122, 8777541, 8451277,
		1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
		1005231, 654231, 3852085, 7576651, 7881200, 4581002;
	
	int array_pause,  
	results;
	                   
	results = search_list(tests, num_count, array_pause);

// ask for account number

	cout<<" Enter a valid Charge Account Number "<<endl;
	cin >> results;
	
// if/else result is/ isnot correct 

	if (results != answer)
	{
		cout << "The number entered is Invalid." << endl; 
    }
    
	    else (results == answer);
	    {
	    	cout << "The number entered is valid." << endl; 
	    }
		
	system("pause");
	return 0;
}

// search list function function

int search_list(int list[], int numElems, int value)
{

// declare variables

	int index = 0; 
	int position = -1; 
	bool found = false; 

// statement if num is found or not

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

Err, you seem to be mixing things up for no reason. You gotta ask yourself which is supposed to happen first, the user enters a number and I search for the number, or I search for a number and the user enters the number I just searched for?

Also this:
1
2
3
4
double answer;
	answer = 5658845, 450125, 7895122, 8777541, 8451277,
		1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
		1005231, 654231, 3852085, 7576651, 7881200, 4581002;


Evaluates to:

double answer = 4581002;
Get the input, then search the list - not vice versa.
Topic archived. No new replies allowed.