saiz66 Posted June 8, 2004 Share Posted June 8, 2004 If I had a class called Cat and I wanted to make 25 cats with different ages how would I name them in a for loop? For example I want them to be cat1, cat2 and so on until cat25, but I don't want to do this Cat cat1; for 25 times. Thanks. Link to comment Share on other sites More sharing options...
0 John Veteran Posted June 8, 2004 Veteran Share Posted June 8, 2004 You have to declare them individually if you want them to have names like that. Otherwise create a Cat array and initialize each one of them with whatever age you want. Link to comment Share on other sites More sharing options...
0 caustiK Posted June 8, 2004 Share Posted June 8, 2004 Cat catarray[25]; for (int i = 0; i < 25; i++) { catarray[i].age = i; } something like this (as gameguy was referring to) Link to comment Share on other sites More sharing options...
0 Zirus Posted June 9, 2004 Share Posted June 9, 2004 Cat catarray[25]; for (int i = 0; i < 25; i++) { ? ? catarray[i].age = i; } something like this (as gameguy was referring to) Thats right, you would do something like that. One thing you might want to know about arrays, is that to access the first value stored in the array you'd have to go: cout << cat[0]; The first value in the array is stored in the 0 value. If you wanted 25 values, to get the 25th value, you'd have to go: cout << cat[24]; since the values start at zero. There are still 25 values in the array, even though the highest number you can use is 24. Link to comment Share on other sites More sharing options...
Question
saiz66
If I had a class called Cat and I wanted to make 25 cats with different ages how would I name them in a for loop? For example I want them to be cat1, cat2 and so on until cat25, but I don't want to do this Cat cat1; for 25 times. Thanks.
Link to comment
Share on other sites
3 answers to this question
Recommended Posts