How to loop through an Array in a Class

I am having a very hard time getting my getCustomer() function to work. It's supposed to loop over an array and determine if there is a matching customerID value. The value is entered in from the console, and that function searches an array that has read in a file filled with customer data (the get customerlist function) Any help is greatly appreciated. I've included all code in case there is a problem elsewhere that is influencing my function not working.

As of now, the error says I need to create a pointer using & in the .getCustomerId line...but nothing seems to work.

MyDataStore * MyDataStore::iInstance;
//declare private customer array
Customer customers[5];
//customer list prototype
void getCustomerList(Customer[], int);

MyDataStore::MyDataStore()
{
cout << "In constructor.." << endl;
getCustomerList(customers, 5);
}

MyDataStore * MyDataStore::GetInstance()
{
if (iInstance == NULL) {
iInstance = new MyDataStore();
}
return iInstance;
}

Customer MyDataStore::getCustomer(string customerId) {
//search customer array for a customer id, instantiate a customer if found
bool found_id = true;
customers[5];
int a = 0;
const int MAX = 6;
string error = "Error: ID not found";

for (a; a != MAX; ++a) {
if (customers[a].getCustID == customerId) {
return customers[a];
}
else cout << error;
}
}

void getCustomerList(Customer cust[], int size) {
//load customer array
ifstream input_customer;
input_customer.open("Customers.txt");
string customerId;
string firstName, lastName, c_street, c_city, c_state, c_zip;
size = 6;

int a = 0;

while (!input_customer.eof()) {
input_customer >> customerId;
input_customer >> firstName;
input_customer >> lastName;
input_customer >> c_street;
input_customer >> c_city;
input_customer >> c_state;
input_customer >> c_zip;
Customer c = cust[a];
if (a <= size) {
cust[a] = c;
a++;
}
}
}
Hello charper,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

The first five line have no context with out the code that is missing, i.e., the class or header file that contains the class, main and the header fils needed to compile the program.

It is more difficult for me to take part of a program and guess at what ight be wrong without the rest of the program.

One art I did see is while (!input_customer.eof()). It is a good idea to stay away from this because it does not work the way you are thinking. Usually the last read is processed twice before the while condition sees "eof". The more preferred way if reading a file is while (input_customer >> customerId). This way when the read fails so will the while loop.

Also you open a file for input, but never check to see if the file opened. Always best to check if an input file is open because this could fail, but the rest of the program would run without reading anything from the file.

What I can not determine is if the "getCustomerList" function should be part of the class or not.

I do see a potential problem with first name, last name, street and city. Each of these could contain a space in the name and the way you are reading the file would miss the second part and mes up the rest of the read. Using "std::getline()" would be a better choice for these variables.

The last thing I will ask for is a sample of the input file say three to five records. It helps to know what the file looks like to understand how the program is reading the file.

Hope that helps,

Andy
> I am having a very hard time getting my getCustomer() function to work. It's
> supposed to loop over an array and determine if there is a matching
> customerID value.
you tell us what you want to do, but not the problem that you're having.
¿what's wrong with your implementation? (compile error/crash/wrong value)

> I've included all code in case there is a problem elsewhere
no, you haven't included all your code.
you copy-pasted random snips that you think are relevant. we can't even compile that.

if you knew what's relevant to your problem, you probably would have solve it already.
so paste all your code.
if you've got several files, upload to github or similar.


> As of now, the error says I need to create a pointer using & in the .getCustomerId line
you don't understand the error, no problem. some messages are confusing, or you may lack the knowledge. again, no problem.
but don't go paraphrasing it then.

1
2
3
4
5
6
//Customer customers[5];
const int MAX = 6;
string error = "Error: ID not found";

for (a; a != MAX; ++a) {
   if (customers[a].getCustID == customerId) {

out of bounds

1
2
3
Customer c = cust[a];
if (a <= size) {
   cust[a] = c;
¿what? ¿what was your intention there?
you are assigning A to B, and then in some cases write it back to A.
also, ¿where does cust[a] came from? and you don't do anything with all those variables that you read from the file
Topic archived. No new replies allowed.