No user input program

I have to write a program for class. The problem is posted below:

Buddy, lives in the backyard. He barks at night when he sees a cat or a squirrel that has come to visit. If he sees a frog, and he is hungry, he eats it. If he sees a frog and he isn't hungry, he plays with it. If he has eaten 2 frogs already, and is still hungry, he will let it go. If he sees a coyote, he crys for help. Sometime his friend Spot stops by, and they chase each other. If he sees any other animal, he simply watches it.

Write one test program and a set of classes that keeps track of all of the backyard activity and stores the results into a file for a given night. You would need to keep track of how many frogs he has eaten, how many animals of each type has come and visited, how often he has played and other such details.

You will also need to write a test program that will read the file that was generated from the other test program, and print out how many animals of each type that he has seen, what he has done with them on a particular day. The user will need to enter in the date, and the information from the file for that date will be read in, and displayed.

I don't understand what type of human input is necessary to start the program in the first place.

I know I can have a base class for animal for which all other animal classes can be derived from. I can use Case statements to determine what action to take based on the animal encountered.

Could anyone help give he a push in the right direction?
It looks like choice of human input is up to you.

You could just have a random number generator run your entire program.

Or you could ask the human to select what kind of animal enters the yard next.


Polymorphism obviates the need for case statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class animal
  {
  public: virtual string type() const = 0;
  };

class dog: public animal
  {
  public: string type() const { return "dog"; }
  };

class cat: public animal
  {
  public: string type() const { return "cat"; }
  };

int main()
  {
  vector <animal*> animals;

  animals.push_back( new dog );
  animals.push_back( new cat );

  for (unsigned n = 0; n < animals.size(); n++)
    {
    cout << "Animal number " << (n + 1) << " is a " << animals[ n ]->type() << ".\n";
    }

  return 0;
  }

Using virtual functions removes the need to have a giant case statement that knows stuff about all your objects... Now each object only needs to know about itself.

Hope this helps.
Thank you. You are using some stuff we haven't learned yet, but this definitely helps.
Topic archived. No new replies allowed.