[try Beta version]
Not logged in

 
Can't search an array of objects for a matching string

Nov 6, 2017 at 1:15am
I'm trying to search an array of objects to find a match for a string that a user types in, however no matter what the user types it still displays an error, even if the string matches exactly.

if string doesnt match any in array, print the if, else print the info

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
void accountInfo(Bank MaqBank, ofstream &c_out)
{
   Account Account; string AcctType, AcctNum; double AcctBal;
   Depositor Depstr; string Social;
   Name DepstrName; string First, Last;
   int Index;

   //Get user input for an SSN
   cout << "Enter the social security number of a depositor whose account you wish "
   "to check(INCLUDE DASHES): "; cin >> Social;
   //Send SSN to MaqBank member function findAccountSSN to find a match
   Index = MaqBank.findAccountSSN(Social);
   if(Index != -1)   //If a match is not found
   {
      c_out << "Depositor not found OR invalid social security number entered." <<
         endl;
      cout << "Returning to main menu." << endl;
   }
   else  //If a match is found
   {
      Account = MaqBank.getAccount(Index);
      AcctType = Account.getAcctType();
      AcctNum = Account.getAcctNum();
      AcctBal = Account.getAcctBal();
      Depstr = Account.getDepstr();
      Social = Depstr.getSSN();
      DepstrName = Depstr.getName();
      First = DepstrName.getFirst();
      Last = DepstrName.getLast();

      c_out << "Depositor found - Information below" << endl;
      c_out << "Depositor name: " << First << " " << Last << endl;
      c_out << "Account Number: " << AcctNum << endl;
      c_out << "Account Type: " << AcctType << endl;
      c_out << "Account Balance: $" << AcctBal << endl;
   }
}


Bank.cpp findAccountSSN method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int Bank::findAccountSSN(string Social) const
{
   Depositor Depstr;
   int Found = 0;
   int Index;

   for(int i = 0; i < NumAcctsTotal; i++)
   {
      Depstr = account[i] -> getDepstr();
      if(Depstr.getSSN() == Social)
      {
         Found++;
         Index = i;
      }
   }

   if(Found)
      return(Index);

   return(-1);
}
Last edited on Nov 6, 2017 at 1:28am
Nov 6, 2017 at 7:18am
On line 13: Index != -1 is the positive case. You have it backwards.
Topic archived. No new replies allowed.