2

I am working on client/server project. i am struggling with how to find vector's information in database to match client's account number that requested.

header file

struct storeData
{
    int iAccountNumber;
    int iPin;
    double dBalance;
    string sFirstName;
    string sLastName;
    string sMiddleInitial;
};
vector <storeData> storeDataArray;
storeData dataObj;

in server file..

int MyThread::findAccountNumberInStore(int iAccountNumber)
{
    int iIndex = -1;

    for(int unsigned i = 0; i <= storeDataArray.size(); i++)
    {
         //i got error message in if statement. i dont know how to fix it.
        if(iAccountNumber == storeDataArray.at(dataObj.iAccountNumber))
        {
              return i;
        }

    }
    return iIndex; //no account is found...
}

Also how can I store the struct's data in vector (all data in one element)?

Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
Daniel
  • 17
  • 2

2 Answers2

1

From vector.at description:

Returns a reference to the element at position n in the vector.

The function automatically checks whether n is within the bounds of valid elements in the vector, throwing an out_of_range exception if it is not (i.e., if n is greater or equal than its size). This is in contrast with member operator[], that does not check against bounds.

You can read more about this function here: vector.at

also you can read a discution about it here: vector::at vs. vector::operator[]

to your practical problem i suggest using it like this:

if(iAccountNumber == storeDataArray.at(i).iAccountNumber)
Community
  • 1
  • 1
Ravid Goldenberg
  • 2,119
  • 4
  • 39
  • 59
  • i tried that code and i got error. the error said "terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check" – Daniel Dec 08 '14 at 00:21
  • That simply means you tried accessing an index bigger than your actual vector size. – Ravid Goldenberg Dec 08 '14 at 00:24
  • Again from the function description: If this is greater than or equal to the vector size, an exception of type out_of_range is thrown. – Ravid Goldenberg Dec 08 '14 at 00:25
  • 1
    Right but its <= vec.at(vec.size()) will always throw – Adrian Cornish Dec 08 '14 at 00:28
  • Yes this is true, again from the description: Notice that the first element has a position of 0 (not 1). – Ravid Goldenberg Dec 08 '14 at 00:30
  • If I replace <= with < then index must be 1 or more. i tried that and my program wont run that loop. – Daniel Dec 08 '14 at 00:33
  • 1
    Wrong - it is because your vector is empty - trust me - after 20 years of programming vectors are 0 index based ;-) – Adrian Cornish Dec 08 '14 at 00:37
  • I tested my program. I saved the data in storeDataArray from client's data and the data should be saved successful. When I reconnect to server and send exactly same account numbers and pin numbers (server have data in database). the data is not found. i am not sure if it skipped for loop or failed if statement such as if(iAccountNumber == storeDataArray.at(i).iAccountNumber) – Daniel Dec 08 '14 at 00:42
  • What do you mean by client/server - I have a feeling it has nothing to do with what I know is a client/server app – Adrian Cornish Dec 08 '14 at 00:48
  • Sorry for confusion. My project is client/server. The server received data and stored them successful. When the client requested server to find account number to match (codes that I posted here). The codes are skipped even storeDataArray have data in it. My project is bank teller. The client creates account and server saved these information successful. I makes client disconnect with server. then, the client reconnect with server and the client send data (account number and pin number only). suddenly, server sends "not found" error code. it supposed be approved because data is matched – Daniel Dec 08 '14 at 01:00
0

You almost have it - the bracket is in the wrong place, and you didn't index the vector correctly, you have

if(iAccountNumber == storeDataArray.at(dataObj.iAccountNumber))

Should be

if(iAccountNumber == storeDataArray.at(i).iAccountNumber)

Also I see you use <= on array size check - this is incorrect. Vector bounds are from 0 to size()-1

Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
  • i tried that code and i got error. the error said "terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check" – Daniel Dec 08 '14 at 00:20