Here is my code

#include <iostream>
using namespace std;



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

int main()
{
    int numItems = 18;
	int position;
	int accountnumber;

	int accounts[numItems] = {5658845,  4520125,  7895122,8777541,  8451277,  1302850,8080152,  4562555,  5552012,
        5050552,  7825877,  1250255,1005231,  6545231,  3852085,7576651,  7881200,  4581002 };

	cout << "Please enter an account charge number: ";
	cin >> accountnumber;

	position = searchList( accounts, numItems, accountnumber);

	if(position == -1)
		cout << accountnumber "Is not a valid charge account number." << endl;
	else
		cout << accountnumber "Is a valid charge account number." << endl;
	
	return 0;
}

int searchList(int accounts[],int numItems,int accountnumber)
{
	int index = 0;
	int position = -1;
	bool found = false;

	while(!found && index < numItems)
	{
		if(accounts[index] == accountnumber)
		{
			found = true;
			position = index;
		}
		index++;
	}
	return position;
}

The Errors i get are

C:\Users\***\Desktop\prelab1.cpp In function `int main()':
15 C:\Users\***\Desktop\prelab1.cpp variable-sized object `accounts' may not be initialized
23 C:\Users\***\Desktop\prelab1.cpp expected `;' before string constant
25 C:\Users\***\Desktop\prelab1.cpp expected `;' before string constant

Any help would be much appreciated.

Recommended Answers

All 3 Replies

First problem:

int numItems = 18;
[...]
int accounts[numItems]

You can't do that. Variable array-sizes on declaration are not allowed in C++ (unless you use new-delete).
So you need to make numItems constant. : const int numItems = 18; Second problem:
You forgot a few << operators in your cout statements:

cout << accountnumber << "Is not a valid charge account number." << endl;
[...]
cout << accountnumber << "Is a valid charge account number." << endl;

thanks that was the problem.

thanks that was the problem.

I know, that's why I told you :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.