title: Perform an input check and initialise an object in C++
body: <p>I have an example code for asking the user for making an animal by user input but the check is in the if-else construction and the compiler gives me error "error: 'animal' was not declared in the scope". Here is my code. Please suggest how can I work it around. </p>
  
  <pre><code>class Animal{
  public:
      string getName(){
          return myName;
      }
       void setName(string name){
          myName = name;
      }
      virtual string Sound()
      {
  
      }
  private:
      string myName;
  };
  class Cat : public Animal{
  public:
      string Sound(){
          return "Myau!\n";
      }
  };
  class Dog : public Animal{
  public:
      string Sound(){
          return "Bark!\n";
      }
  };
  int main() {
  
      string type, name;
      cout&lt;&lt;"Vyvedete jivotno:\n 1. Cat\n2. Dog\nJivotno:";
      cin&gt;&gt;type;
      cout&lt;&lt;"\nVyvedete ime:";
      cin&gt;&gt;name;
      if(type == "Cat")
      {
          Cat animal;
      }
      else if(type == "Dog")
      {
          Dog animal;
      }
  
      animal.setName(name);
      cout&lt;&lt;"My name is "&lt;&lt;animal.getName()&lt;&lt;endl;
      cout&lt;&lt;animal.Sound()&lt;&lt;endl;
  
      return 0;
  }
  </code></pre>