Oct 21, 2019 at 3:04pm UTC
This is my simple struct program
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
struct outlet
{
string name;
bool bankrupt;
int worth;
};
void isbankrupt(outlet shop)
{
if (shop.bankrupt)
{
cout<<"Name: " << shop.name<<endl;
cout<<"Worth: " << shop.worth<<endl;
}
}
int main(int argc, char *argv[]) {
outlet Asda{"Asda" , false , 400};
outlet Tesco{"Tesco" , false , 700};
outlet Lidl{"Lidl" , true , 600};
isbankrupt(Asda);
isbankrupt(Tesco);
isbankrupt(Lidl);
return 0;
}
How can I call the inbankrupt() function once?
Last edited on Oct 21, 2019 at 3:05pm UTC
Oct 21, 2019 at 3:30pm UTC
You could also use an array of structs, and then use a loop to call the isbankrupt() function once for each struct[i].
Oct 21, 2019 at 4:04pm UTC
If you delete those lines ... you will only be calling it once . Obviously. On line 21.
If you would like something else, please write a clearer question.
Oct 21, 2019 at 4:10pm UTC
"isbankrupt" as the name for a function that simply prints information is a bad name, imo.
Normally "isX" functions return a boolean or something of that nature.
Other than that, yes use a array/vector, as xmrfate said.
Oct 21, 2019 at 4:16pm UTC
Thanks @Ganado and @xmrfate
apologies for not having an elucidated question, @lastchance .
Oct 21, 2019 at 4:39pm UTC
No problem. I'm glad we could help!