I have initialized nM (an array of MagicNumber type) in the GuessMachine constructor using the setter in the MagicNumber, but when I refer to the array, it seems to be empty.
GuessMachine::GuessMachine()
{
MagicNumber nM[6];
string prizeItem = "";
for (int i = 0; i<6; i++)
{
cout << "Please enter an item: ";
cin >> prizeItem;
cin.ignore();
nM[i].setNumber(i+1);
nM[i].setItem(prizeItem);
}
cout << endl;
cout << "checking 1st index is: " << nM[0].getNumber() << " "
<< nM[0].getItem() << endl;
}
void GuessMachine::displayPrizes()
{
cout << "checking 1st index is: " << nM[0].getNumber() << " "
<< nM[0].getItem() << endl;
return;
}
Line 25 & 26 is giving me problem. nM is an array of MagicNumber type. Why is it when I refer to nm[0] in displayPrizes(), it gives me some funny numbers, like as if it has never been initialize? But I have checked that it has been properly initialized.
In the constructor on line 4 you create a new array called nM. That is not the same array that you access in displayPrizes(). Removing line 4 probably solves the problem.