Question #108899

Explain what is wrong with the following code fragment and write code to correct it:

int main()

{

PersonType family[20],

newBaby("Anny Dube", 20180912, "2 Sept");

//Assume the array family has been initialised

for (int i = 0; i < 20; i++)

if (family.birthday[5] == newBaby.birthday)

cout << family.name[5] << " share a birthday with "

<<newBaby.name;

return 0;

}

Expert's answer

// Explanations in the comments

int main()
{

PersonType family[20],
      newBaby("Anny Dube", 20180912, "2 Sept");

//Assume the array family has been initialised


for (int i = 0; i < 20; i++)

    if (family[i].birthday == newBaby.birthday) // You need to refer to a specific element 
                                                // in the array with the number "i"
                                                // Change: family.birthday[5] --> family[i].birthday

        cout << family[i].name << " share a birthday with " // You need to access the array
                                                            // element with the number "i"
                                                            // Change: family.name[5] --> family[i].name
        << newBaby.name;

return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS