-1
//program8.cpp
#include <iostream>
#include "species.h"
#include "reptilian.h"
#include "mammalian.h"
#include "insects.h"

using namespace std;

void VirtualPrint(species &typeofSpecies){
 typeofSpecies.printme();
}

void VirtualDanger(species &typeofSpecies){
 typeofSpecies.showDanger();
}

int main(int argv, char **args){

 reptilian rep;
 insects ins; 
 VirtualPrint(rep);
 VirtualPrint(ins);
 VirtualDanger(rep);
 VirtualDanger(ins);
 return 1;
}


//species.h
#ifndef SPECIES_H
#define SPECIES_H
#include <iostream>
#include <string>

using namespace std;

class species{
 public:
  species();
  virtual void printme();
  virtual void showDanger() = 0;

 protected:
  string name;
  string color;
  int lifeExp;
  bool thr;
};

#endif

//species.cpp
#include <iostream>
#include <string>
#include "species.h"

using namespace std;

species::species(){
 cout << "Please enter name of species:" << endl;
 getline(cin, name);
 cout << "Please enter color of species:" << endl;
 getline(cin, color);
 cout << "Please enter life expectancy of species in years:"  << endl;
 cin >> lifeExp;
 cout << "Please enter if species is threat:true(1) or false(0)" << endl;
 cin >> thr;
}

void species::printme(){
 cout << "Name: " << name << "  Color: " << color << "  Life Expectancy: " << lifeExp << "  Threat: " << thr << endl;
}

//reptilian.h
#ifndef REPTILIAN_H
#define REPTILIAN_H
#include <iostream>
#include <string>
#include "species.h"

using namespace std;

class reptilian : public species{
 public:
  reptilian();
  virtual void printme();
  virtual void showDanger(); 

 protected:
  int length;
  int lethality;
};

#endif


//reptilian.cpp
#include <iostream>
#include "reptilian.h"

using namespace std;

reptilian::reptilian(){
 cout << "Please enter length(inches): " << endl;
 cin >> length;
 cout << "Please enter lethality(0-100): " << endl;
 cin >> lethality;
}

void reptilian::printme(){
 species::printme();
 cout << "  Length: " << length << "  Lethality: " << lethality << endl;;
}

void reptilian::showDanger(){
 cout << endl;
 if(thr == true){
  cout << "This species is a threat" << endl;
  cout << "The name of the species is " << name << " has a color of " << color << ", has a life expectancy of " << lifeExp << " has a length of " << length << ", and a lethality of " << lethality << endl;
 }
}

This is my code for my program it runs fine if one reptilian object is made. However when two are made it will not take the name of the second object. The same happens when two insects objects are made. I have not tested the mammalian object yet as I am trying to solve this problem first

edit: output:

Please enter name of species:

sam

Please enter color of species:

orange

Please enter life expectancy of species in years:

100

Please enter if species is threat:true(1) or false(0)

0

Please enter length(inches):

60

Please enter lethality(0-100):

0

Please enter name of species:

Please enter color of species:

yellow

Please enter life expectancy of species in years:

20

Please enter if species is threat:true(1) or false(0)

0

Please enter length(inches):

10

Please enter lethality(0-100):

0

Please enter how venomous the species is(0-100):

0

Name: sam Color: orange Life Expectancy: 100 Threat: 0

Length: 60 Lethality: 0

Name: Color: yellow Life Expectancy: 20 Threat: 0

Length: 10 Lethality: 0

Venomous: 0

Matt
  • 27
  • 1
  • 3

1 Answers1

1

Your problem is mixing using getline with the >> operator. From http://en.cppreference.com/w/cpp/string/basic_string/getline:

When used immediately after whitespace-delimited input, e.g. after int n; std::cin >> n;, getline consumes the endline character left on the input stream by operator>>, and returns immediately. A common solution is to ignore all leftover characters on the line of input with cin.ignore(std::numeric_limits::max(), '\n'); before switching to line-oriented input.

So after the cin >> lethality a newline is left in the cin stream. The getline in the second instance of species sees this newline and immediately returns.

Also refer to this: std::cin:: and why a newline remains

To fix this, change your getline calls to use the cin >> method.

Community
  • 1
  • 1
Michael Albers
  • 3,721
  • 3
  • 21
  • 32