Afish pisze:A jak wyciągnąć pojedynczy element z tablicy i jakiego jest on typu?
Couple couple = { "John", { PKO, 1200 }, {"Marry", {BGZ, 1500}} };
znaczy to samo co to:
Couple couple;
couple.he.name = "John";
couple.he.account.bank = PKO;
couple.he.account.balance = 1200;
couple.she.name = "Marry";
couple.she.account.bank = BGZ;
couple.she.account.balance = 1500;
więc dla tablicy masz tak samo tylko musisz wstawić [0] tak jak przy zwykłej tablicy np
tablica[0].he.account.bank
tablica[1].she.name itd
i przy wywoływaniu funkcji musisz dać (tablica,reszta warunków)
jeżeli dasz (tablica[], reszta warunków) to wyskoczy błąd i będzie kompilator wartości żądać.
typ danych masz taki sam jak w strukturze podałeś.
-- 5 sty 2018, o 12:07 --
Kod: Zaznacz cały
#include <iostream>
#include <string>
using namespace std;
int na = 0;
Couple cplss[1];
enum Banks { PKO, BGZ, BRE, BPH };
struct Account {
Banks bank;
int balance;
};
struct Person {
string name;
Account account;
};
struct Couple {
Person he;
Person she;
};
const Couple* bestClient(const Couple cpls[], int size, Banks bank)
{
//sprawdza czy para posiada w banku bank i sumuje konta
int i = 0;
int suma_kont[4];
for (int i = 0; i < size; i++)
{
if ((bank == cpls[i].he.account.bank) || (BGZ == cpls[i].she.account.bank)) //warunek przynajmniej jeden bank=przzypisany w wywołaniu
{
suma_kont[i] = cpls[i].he.account.balance + cpls[i].she.account.balance;
}
else
{
suma_kont[i] = 0;
}
}
int max = 0;
i = 0;
//wylicza ile ma najbogatrza para
for (int i = 0; i < size; i++)
{
while (suma_kont[i]>=max)
{
max = suma_kont[i];
}
}
//znajduje najbogatszą pare
for (int i = 0; i < 4; i++)
{
if (suma_kont[i] == max)
{
cplss[0] = cpls[i];
}
}
return cplss[0] ; //zwraca cpls by móc urzyc p->he.account.balance
}
int main() {
using std::cout; using std::endl;
Couple cpls[]= {
{ { "John",{ PKO,1200 } },{ "Marry",{ BGZ,1500 } } },
{ { "Peter",{ BGZ,1300 } },{ "Suzy",{ BRE,1300 } } },
{ { "Kevin",{ PKO,1600 } },{ "Katy",{ BPH,1500 } } },
{ { "Kenny",{ BPH,1800 } },{ "Lucy",{ BRE,1700 } } },
};
const Couple* p = bestClient(cpls, 4, BGZ);
cout << p->he.name << " and " << p->she.name;
cout << ": " << p->he.account.balance + p->she.account.balance << endl;
system("pause");
}
skończyłem tylko jeszcze nie rozumiem czemu mi funkcja const Couple* bestClient nie zwraca wartości.
-- 5 sty 2018, o 13:04 --
poprawiłem return cpls[0] na return cpls;
teraz tylko czemu nic nie wyświetla nie wiem ;[