Mam taki problem z zadaniem, że chce zwrócić w funkcji bestClient wskażnik do pary osób która ma największe oszczędności. Jedna z nich musi być w banku podanym w argumencie, a oszczędności mogą być nawet na minusie, chodzi o to że mają najmniejszy dług, w tym przypadku byłaby to para Kenny i Suzy. Czy mógłby ktoś napisać jak prawidłowo przekazać wskażnik z wynikiem oraz czy istnieje jakiś lepszy sposób na warunek na określenie największych oszczędności.
#include <iostream>
using namespace std;
enum Banks { PKO, BGZ, BRE, BPH };
struct Account { Banks bank; int balance; };
struct Person { char name[20]; Account account; };
struct Couple { Person he; Person she; };
const Couple* bestClient(const Couple* cpls, int size, Banks bank) {
int biggestcash = -2147483648;
Couple richestcouple;
for (int i = 0; i < size; i++) {
if (cpls->he.account.bank == bank || cpls->she.account.bank == bank) {
if (cpls->he.account.balance + cpls->she.account.balance > biggestcash) {
richestcouple = cpls[i];
}
}
else { return nullptr; }
}
return *richestcouple;
}
int main() {
Couple cpls[4] = {
cpls[0] = { "Johny", PKO, 1200, "Mary", BGZ, 1400 },
cpls[1] = { "Peter", BGZ, 1400, "Suzy", BRE, -1500 },
cpls[2] = { "Kevin", PKO, 1600, "Suzy", BPH, 1500 },
cpls[3] = { "Kenny", BPH, 200, "Suzy", BRE, -201 }
};
const Couple * p = bestClient(cpls, 4, BRE);
if (p)
cout << p->he.name << " and " << p->she.name
<< ": " << p->he.account.balance +
p->she.account.balance << endl;
else
cout << "No such couple...\n";
}