J. Doe
J. Doe

Reputation: 13

Searching through struct vector and outputting the element in C++

Okay so my struct looks like this:

struct Account{
    int accountNumber;
    string lastName;
    string firstName;
    double accountBalance;
    bool active;
};

And I've hard coded accounts into this, i.e.

vector<Account> subAccount = *account;
Account newA;
newA.accountNumber = 2345;
newA.lastName = "test1";
newA.firstName = "test1";
newA.accountBalance = 100.00;
newA.active = true;

subAccount.push_back(newA);

And a second account:

Account newB;
newB.accountNumber = 1234;
newB.lastName = "test2";
newB.firstName = "test2";
newB.accountBalance = 178.1;
newB.active = true;

subAccount.push_back(newB);

*account = subAccount;

So now I need to be able to print out individual account in a type void function. What's the best way to search through the vector/struct when a user is prompted to enter an account number? I've tried to do it this way:

for (int i = 0; i < subAccount.size(); i++){

    if (TempActNum == subAccount[i].accountNumber) {
        cout << "Account Number: " << subAccount[i].accountNumber << "      Balance: " << subAccount[i].accountBalance
             << endl;
        cout << "Last Name: " << subAccount[i].lastName << "        First Name: " << subAccount[i].firstName << endl;
    break;
    }

    if (TempActNum != subAccount[i].accountNumber) {
            cout << "This account does not exist." << endl;
    }

} }

The issue with this method was that if I inputted the second account number (1234) it would print "This account does not exist." along with all the account details. So what would be a better way of searching and printing? Or what's maybe a fix for my issue? Thanks for your time.

Upvotes: 1

Views: 51

Answers (2)

Loki Astari
Loki Astari

Reputation: 264381

Your problem is that the test for non existance is in the middle of the loop.

for (int i = 0; i < subAccount.size(); i++){

    if (TempActNum == subAccount[i].accountNumber) {
        cout << "Account Number: "
             << subAccount[i].accountNumber 
             << "      Balance: " 
             << subAccount[i].accountBalance
             << endl;
        cout << "Last Name: "
             << subAccount[i].lastName 
             << "        First Name: " 
             << subAccount[i].firstName 
             << endl;
        break;
    }

    if (TempActNum != subAccount[i].accountNumber) {
            cout << "This account does not exist." << endl;
    }

}

You can only tell if the account has not been found after you have finished looping over all the accounts. So create a temp variable to say it was found.

bool found = false;
for (int i = 0; i < subAccount.size(); i++){

    if (TempActNum == subAccount[i].accountNumber) {
        cout << "Account Number: "
             << subAccount[i].accountNumber 
             << "      Balance: " 
             << subAccount[i].accountBalance
             << endl;
        cout << "Last Name: "
             << subAccount[i].lastName 
             << "        First Name: " 
             << subAccount[i].firstName 
             << endl;
        found == true; // You found it.
        break;
    }

}
// After you have checked all the accounts (or after breaking out)
// you can print this statement if it was not found.
if (!found) {
        cout << "This account does not exist." << endl;
}

Also note we can simplify your account creation:

vector<Account> subAccount = {
    { 2345, "test1", "test1", 100.00, true},
    { 1234, "test2", "test2", 178.1, true}
};

Upvotes: 1

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275350

auto it = std::find_if(
  begin(subAccount), end(subAccount),
  [&](auto&& account){ return TempActNum == account.accountNumber; }
);
if (it == end(subAccount)) {
  std::cout << "This account does not exist.\n";
} else {
  std::cout << "Account " << it->accountNumber << " exists.\n";
}

, but relatively easy to backport to .

Upvotes: 0

Related Questions