Ok, so I'm making a program in c++ that keeps track of the number of objects you make for a specific class. It works, but I added a part to change the screen output at a specific number of objects and that's where it broke.... heres the program:

#include <iostream.h>

class Cat
     {
     public:
          Cat(int age):itsAge(age){HowManyCats++; }
          virtual ~Cat() { HowManyCats--; }
          virtual int GetAge() { return itsAge; }
          virtual void SetAge(int age) { itsAge = age; }
          static int HowManyCats;

     private:
          int itsAge;
     };

//static member data defined
int Cat::HowManyCats = 0;

int howManyCatsFunction()
     {
     char phrase1;
     
     if(Cat::HowManyCats == 1)
          {
          phrase1 = 'There is';
          }
     else
          {
          phrase1 = 'There are';
          }
                         
     cout << phrase1
          << Cat::HowManyCats << " cats alive!"
          << endl;
     }

int main()
     {
     const int MaxCats = 5; int i;
     Cat *CatHouse[MaxCats];
     for (i = 0; i<MaxCats; i++)
          {
          CatHouse[i] = new Cat(i);
          }

     for (i = 0; i<MaxCats; i++)
          {
          howManyCatsFunction();
          cout << "Deleting the one which is ";
          cout << CatHouse[i]->GetAge();
          cout << " years old\n";
          delete CatHouse[i];
          CatHouse[i] = 0;
          }
    system("PAUSE");
    return 0;
    }
int howManyCatsFunction()
{
     cout << "there " << (Cat::HowManyCats == 1 ? "is " : "are " )
          << Cat::HowManyCats << " cats alive!" << endl;
}

you also need to write a copy constructor which increments Cat::HowManyCats to get the right number always.

> phrase1 = 'There is';
This needs to be a std::string, not a single char.

Then you'll be able to write phrase1 = "There is"; // note double quotes "", not single '' > << (Cat::HowManyCats == 1 ? "is " : "are "
While this seems like a good idea, it really makes it much harder to translate into other natural languages, which do not have such simplistic rules for plurality.

>
> << (Cat::HowManyCats == 1 ? "is " : "are "
While this seems like a good idea, it really makes it much harder to translate into other natural languages, which do not have such simplistic rules for plurality.

true. and it does not resolve between " cats alive!" / " cat alive!". it is not all that simplistic even in english; a general solution is not an easy one (cat/cats, child/children etc.). perhaps the easy way out is something like
number of cats: 23 (or 0 or 1).

Member Avatar for iamthwee

>#include <iostream.h>

I presume you are following an old book/tutorial. hmm

Yeah, I'm following a tutorial. And thanks for the help guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.