Sams Teach Yourself C++ in 21 Days

(singke) #1
Templates 681

19


119: bool Stop = false;
120: int offset, value;
121: while (Stop == false)
122: {
123: cout << “Enter an offset (0-9) “;
124: cout << “and a value. (-1 to stop): “ ;
125: cin >> offset >> value;
126: if (offset < 0)
127: break;
128: if (offset > 9)
129: {
130: cout << “***Please use values between 0 and 9.***\n”;
131: continue;
132: }
133: theArray[offset] = value;
134: }
135: }
136:
137:
138: void AnimalFillFunction(Array<Animal>& theArray)
139: {
140: Animal * pAnimal;
141: for (int i = 0; i < theArray.GetSize(); i++)
142: {
143: pAnimal = new Animal;
144: pAnimal->SetWeight(i*100);
145: theArray[i] = *pAnimal;
146: delete pAnimal; // a copy was put in the array
147: }
148: }

LISTING19.5 continued


If you are using a Microsoft compiler, uncomment line 65. Based on the C++
standards, this line should not be necessary; however, it is needed to com-
pile with the Microsoft compiler.

NOTE


Enter an offset (0-9) and a value. (-1 to stop): 1 10
Enter an offset (0-9) and a value. (-1 to stop): 2 20
Enter an offset (0-9) and a value. (-1 to stop): 3 30
Enter an offset (0-9) and a value. (-1 to stop): 4 40
Enter an offset (0-9) and a value. (-1 to stop): 5 50
Enter an offset (0-9) and a value. (-1 to stop): 6 60
Enter an offset (0-9) and a value. (-1 to stop): 7 70
Enter an offset (0-9) and a value. (-1 to stop): 8 80
Enter an offset (0-9) and a value. (-1 to stop): 9 90
Enter an offset (0-9) and a value. (-1 to stop): 10 10

OUTPUT

Free download pdf